Treeview in tree mode
The code below will populate the treeview widget with a tree like structure which you can expand and collapse, double clicking will retrieve the selected node in the tree.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
#!/usr/bin/env python
from gi.repository import Gtk, GLib
class application_gui:
"""Tutorial 10 text input, display a treeview in expandable tree format."""
count = 0
def __init__(self):
#load in our glade interface
xml = Gtk.Builder()
xml.add_from_file('tut10.glade')
#grab our widget using get_object this is the name of the widget from glade, window1 is the default name
self.window = xml.get_object('window1')
self.text = xml.get_object('entry1')
#load our widgets from the glade file
self.widgets = {}
self.widgets['treeview'] = xml.get_object('treeview1')
treeview(self.widgets['treeview'], self.text)
#connect to events, in this instance just quit our application
self.window.connect('delete_event', Gtk.main_quit)
self.window.connect('destroy', lambda quit: Gtk.main_quit())
#show the window else there is nothing to see :)
self.window.show()
class treeview:
treeview = None
treemodel = None
selected = 'workspace'
def __init__(self, treeview, entry):
self.entry = entry
self.treeview = treeview
self.treeview.connect('row-activated', self.selection)
self.treeview.connect('button_press_event', self.mouse_click)
#create a storage model in this case a treemodel
self.treemodel = Gtk.TreeStore(str)
self.treeview.set_model(self.treemodel)
#add columns usually only one in case of the treeview
column = Gtk.TreeViewColumn("Objects")
self.treeview.append_column(column)
#add in a text renderer so we can see the items we add
cell = Gtk.CellRendererText()
column.pack_start(cell, False)
column.add_attribute(cell, "text", 0)
self.populate()
self.menu()
def populate(self):
self.treemodel.clear()
#populate the treeview with a largish tree
for item1 in range(0, 5):
iter_level_1 = self.append_tree('Item ' + str(item1))
for item2 in range(0, 5):
iter_level_2 = self.append_tree('Sub Item ' + str(item2), iter_level_1)
for item3 in range(0, 5):
self.append_tree('Sub Sub Item ' + str(item3), iter_level_2)
def append_tree(self, name, parent=None):
"""
append to the treeview if parent is null append to root level.
if parent is a valid iter (possibly returned from previous append) then append under the parent
"""
myiter = self.treemodel.insert_after(parent, None)
self.treemodel.set_value(myiter, 0, name)
return myiter
def menu(self):
"""
popover menu shown on right clicking a treeview item.
"""
self.treeview_menu = Gtk.Menu()
for item in range(0, 5):
menu_item = Gtk.MenuItem("Menu " + str(item))
self.treeview_menu.append(menu_item)
def mouse_click(self, tv, event):
if event.button == 3:
# right mouse button pressed popup the menu
self.treeview_menu.show_all()
self.treeview_menu.popup(None, None, None, None, 1, 0)
def selection(self, tv, treepath, tvcolumn):
"""
on double click get the value of the item we clicked
"""
model = tv.get_model()
treeiter = model.get_iter(treepath)
self.selected = model.get_value(treeiter, 0)
self.entry.set_text(self.selected)
application = application_gui()
Gtk.main()
|