Debian Codegraph @

docs/tutorials/gtk4/controls/examples/dropdown.py

raw
1 import gi
2
3 gi.require_version("Gtk", "4.0")
4 from gi.repository import Gtk
5
6
7 class DropDownWindow(Gtk.ApplicationWindow):
8 def __init__(self, **kargs):
9 super().__init__(**kargs, title="DropDown Demo")
10
11 dropdown = Gtk.DropDown()
12 dropdown.connect("notify::selected-item", self.on_string_selected)
13 self.set_child(dropdown)
14
15 strings = Gtk.StringList()
16 dropdown.props.model = strings
17 items = "This is a long list of words to populate the dropdown".split()
18
19 # Populate the list
20 for item in items:
21 strings.append(item)
22
23 def on_string_selected(self, dropdown, _pspec):
24 # Selected Gtk.StringObject
25 selected = dropdown.props.selected_item
26 if selected is not None:
27 print("Selected", selected.props.string)
28
29
30 def on_activate(app):
31 win = DropDownWindow(application=app)
32 win.present()
33
34
35 app = Gtk.Application(application_id="com.example.App")
36 app.connect("activate", on_activate)
37
38 app.run(None)