decocode decocode deco    

GTK+ 3: Listen-Widgets #

GtkTreeView #

► GTK+ 3 Dokumentation: GtkTreeView GtkTreeViewColumn GtkCellRendererText GtkListStore

Das Widget GtkTreeView kann dazu verwendet werden, Datensätze tabellenartig darzustellen. Dazu werden in dem GtkTreeView die einzelnen Spalten mit GtkTreeViewColumn festgelegt und mit append_column() dem GtkTreeView hinzugefügt.

Um die einzelnen Zellen der Tabelle mit Textinhalten füllen zu können, müssen diese mit GtkCellRendererText konfiguriert werden.

Die konkreten Inhalte der Tabellenzeilen werden mit append() in ein GtkListStore geschrieben (s. Zeile 73).

Quelltext auswählen
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
        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]])]