rss_feed

GTK-3 Treeview in liststore mode

homeHome
pagesGTK-3
pagespython
pageswidgets

Treeview in list mode

The below example demonstrates using the treeview to display table style data, it generates a few rows and shows row selection from a mouse click.")

  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
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python
from gi.repository import Gtk, GLib, GObject


class application_gui:
    """Tutorial 09 treeview in list mode."""
    count = 0

    def __init__(self):
        #load in our glade interface
        xml = Gtk.Builder()
        xml.add_from_file('tut09.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.ListStore(str, str, str)
        self.treeview.set_model(self.treemodel)

        #add columns usually only one in case of the treeview
        column1 = Gtk.TreeViewColumn("Column 01")
        self.treeview.append_column(column1)
        
        column2 = Gtk.TreeViewColumn("Column 02")
        self.treeview.append_column(column2)
        
        column3 = Gtk.TreeViewColumn("Column 03")
        self.treeview.append_column(column3)

        #add in a text renderer so we can see the items we add 
        cell = Gtk.CellRendererText()
        column1.pack_start(cell, False)
        column1.add_attribute(cell, "text", 0)
        
        cell = Gtk.CellRendererText()
        column2.pack_start(cell, False)
        column2.add_attribute(cell, "text", 0)
        
        cell = Gtk.CellRendererText()
        column3.pack_start(cell, False)
        column3.add_attribute(cell, "text", 0)
        
        self.populate()
        self.menu()

    def populate(self):
        self.treemodel.clear()
        #populate the treeview with some list items
        for item1 in range(0, 5):
            iter_level_1 = self.append_tree('Item ' + str(item1))

    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()