Debian Codegraph @

docs/tutorials/gtk4/examples/layout_listbox.py

raw
1 import gi
2
3 gi.require_version("Gtk", "4.0")
4 from gi.repository import Gtk
5
6
7 class ListBoxRowWithData(Gtk.ListBoxRow):
8 def __init__(self, data):
9 super().__init__()
10 self.data = data
11 self.set_child(Gtk.Label(label=data))
12
13
14 class ListBoxWindow(Gtk.ApplicationWindow):
15 def __init__(self, **kargs):
16 super().__init__(**kargs, default_width=400, title="ListBox Demo")
17
18 # Main box of out window
19 box_outer = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=24)
20 box_outer.props.margin_start = 24
21 box_outer.props.margin_end = 24
22 box_outer.props.margin_top = 24
23 box_outer.props.margin_bottom = 24
24 self.set_child(box_outer)
25
26 # Let's create our first ListBox
27 listbox = Gtk.ListBox()
28 listbox.props.selection_mode = Gtk.SelectionMode.NONE
29 listbox.props.show_separators = True
30 box_outer.append(listbox)
31
32 # Let's create our first ListBoxRow
33 row = Gtk.ListBoxRow()
34 hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=24)
35 row.set_child(hbox) # We set the Box as the ListBoxRow child
36 vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
37 hbox.append(vbox)
38 label1 = Gtk.Label(label="Automatic Date & Time", xalign=0)
39 label2 = Gtk.Label(label="Requires internet access", xalign=0)
40 vbox.append(label1)
41 vbox.append(label2)
42
43 switch = Gtk.Switch()
44 switch.props.hexpand = True # Lets make the Switch expand to the window width
45 switch.props.halign = Gtk.Align.END # Horizontally aligned to the end
46 switch.props.valign = Gtk.Align.CENTER # Vertically aligned to the center
47 hbox.append(switch)
48
49 listbox.append(row) # Add the row to the list
50
51 # Our second row. We will omit the ListBoxRow and directly append a Box
52 hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=24)
53 label = Gtk.Label(label="Enable Automatic Update", xalign=0)
54 check = Gtk.CheckButton()
55 check.props.hexpand = True
56 check.props.halign = Gtk.Align.END
57 hbox.append(label)
58 hbox.append(check)
59 listbox.append(hbox) # Add the second row to the list
60
61 # Let's create a second ListBox
62 listbox_2 = Gtk.ListBox()
63 box_outer.append(listbox_2)
64 items = ["This", "is", "a", "sorted", "ListBox", "Fail"]
65
66 # Populate the list
67 for item in items:
68 listbox_2.append(ListBoxRowWithData(item))
69
70 # Set sorting and filter functions
71 listbox_2.set_sort_func(self.sort_func)
72 listbox_2.set_filter_func(self.filter_func)
73
74 # Connect to "row-activated" signal
75 listbox_2.connect("row-activated", self.on_row_activated)
76
77 def sort_func(self, row_1, row_2):
78 return row_1.data.lower() > row_2.data.lower()
79
80 def filter_func(self, row):
81 return row.data != "Fail"
82
83 def on_row_activated(self, _listbox, row):
84 pass
85
86
87 def on_activate(app):
88 # Create window
89 win = ListBoxWindow(application=app)
90 win.present()
91
92
93 app = Gtk.Application(application_id="com.example.App")
94 app.connect("activate", on_activate)
95
96 app.run(None)