GTK+ 3: Listen-Widgets
Gtk.TreeView
► GTK+ 3 Dokumentation: Gtk.TreeView Gtk.TreeViewColumn Gtk.CellRendererText Gtk.ListStore
Das Widget Gtk.TreeView kann dazu verwendet werden, Datensätze tabellenartig darzustellen. Dazu werden in dem Gtk.TreeView die einzelnen Spalten mit Gtk.TreeViewColumn festgelegt und mit append_column() dem Gtk.TreeView hinzugefügt.
Um die einzelnen Zellen der Tabelle mit Textinhalten füllen zu können, müssen diese mit Gtk.CellRendererText konfiguriert werden.
Die konkreten Inhalte der Tabellenzeilen werden mit append() in ein Gtk.ListStore geschrieben (s. Zeile 73).
self.liststore01 = Gtk.ListStore(int, str, str, str, str, str, str, str)
self.treeview01 = Gtk.TreeView(self.liststore01)
self.treeview01.set_rules_hint(True) # Zeilenfarbe wechselnd
self.box03.pack_start(self.treeview01, True, True, 0)
self.treeviewcolumn01 = Gtk.TreeViewColumn("ID")
self.cellrenderertext01 = Gtk.CellRendererText()
self.treeviewcolumn01.pack_start(self.cellrenderertext01, True)
self.treeviewcolumn01.add_attribute(self.cellrenderertext01, 'text', 0)
self.treeviewcolumn01.set_resizable(True)
self.treeview01.append_column(self.treeviewcolumn01)
self.treeviewcolumn02 = Gtk.TreeViewColumn("Vorname")
self.cellrenderertext02 = Gtk.CellRendererText()
self.treeviewcolumn02.pack_start(self.cellrenderertext02, True)
self.treeviewcolumn02.add_attribute(self.cellrenderertext02, 'text', 1)
self.treeviewcolumn02.set_resizable(True)
self.treeview01.append_column(self.treeviewcolumn02)
self.treeviewcolumn03 = Gtk.TreeViewColumn("Nachname")
self.cellrenderertext03 = Gtk.CellRendererText()
self.treeviewcolumn03.pack_start(self.cellrenderertext03, True)
self.treeviewcolumn03.add_attribute(self.cellrenderertext03, 'text', 2)
self.treeviewcolumn03.set_resizable(True)
self.treeview01.append_column(self.treeviewcolumn03)
self.treeviewcolumn04 = Gtk.TreeViewColumn("Geburtsdatum")
self.cellrenderertext04 = Gtk.CellRendererText()
self.treeviewcolumn04.pack_start(self.cellrenderertext04, True)
self.treeviewcolumn04.add_attribute(self.cellrenderertext04, 'text', 3)
self.treeviewcolumn04.set_resizable(True)
self.treeview01.append_column(self.treeviewcolumn04)
self.treeviewcolumn05 = Gtk.TreeViewColumn("PLZ")
self.cellrenderertext05 = Gtk.CellRendererText()
self.treeviewcolumn05.pack_start(self.cellrenderertext05, True)
self.treeviewcolumn05.add_attribute(self.cellrenderertext05, 'text', 4)
self.treeviewcolumn05.set_resizable(True)
self.treeview01.append_column(self.treeviewcolumn05)
self.treeviewcolumn06 = Gtk.TreeViewColumn("Ort")
self.cellrenderertext06 = Gtk.CellRendererText()
self.treeviewcolumn06.pack_start(self.cellrenderertext06, True)
self.treeviewcolumn06.add_attribute(self.cellrenderertext06, 'text', 5)
self.treeviewcolumn06.set_resizable(True)
self.treeview01.append_column(self.treeviewcolumn06)
self.treeviewcolumn07 = Gtk.TreeViewColumn("Straße")
self.cellrenderertext07 = Gtk.CellRendererText()
self.treeviewcolumn07.pack_start(self.cellrenderertext07, True)
self.treeviewcolumn07.add_attribute(self.cellrenderertext07, 'text', 6)
self.treeviewcolumn07.set_resizable(True)
self.treeview01.append_column(self.treeviewcolumn07)
self.treeviewcolumn08 = Gtk.TreeViewColumn("Hausnummer")
self.cellrenderertext08 = Gtk.CellRendererText()
self.treeviewcolumn08.pack_start(self.cellrenderertext08, True)
self.treeviewcolumn08.add_attribute(self.cellrenderertext08, 'text', 7)
self.treeviewcolumn08.set_resizable(True)
self.treeview01.append_column(self.treeviewcolumn08)
personen = []
personen.append(['Hans', 'Meier', '21.04.1987', '10001', 'Musterstadt', 'Hafenstraße', '34'])
personen.append(['Monika', 'Müller', '03.11.1956', '10002', 'Musterdorf', 'Am Acker', '3'])
personen.append(['Paul', 'Schmidt', '17.09.1972', '10001', 'Musterstadt', 'Bahnhofstraße', '129'])
personen.append(['Paula', 'Lehmann', '11.06.1999', '10002', 'Musterdorf', 'Dorfstraße', '17'])
self.liststore01.clear()
self.PersonsRowIter = []
count = 0
for person in personen:
count += 1
self.PersonsRowIter += [self.liststore01.append([count, person[0], person[1], person[2], person[3], person[4], person[5], person[6]])]