File: dropdown.py

package info (click to toggle)
pygobject 3.54.5-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,864 kB
  • sloc: ansic: 40,281; python: 26,363; sh: 477; makefile: 81; xml: 35; cpp: 1
file content (38 lines) | stat: -rw-r--r-- 1,015 bytes parent folder | download
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
import gi

gi.require_version("Gtk", "4.0")
from gi.repository import Gtk


class DropDownWindow(Gtk.ApplicationWindow):
    def __init__(self, **kargs):
        super().__init__(**kargs, title="DropDown Demo")

        dropdown = Gtk.DropDown()
        dropdown.connect("notify::selected-item", self.on_string_selected)
        self.set_child(dropdown)

        strings = Gtk.StringList()
        dropdown.props.model = strings
        items = "This is a long list of words to populate the dropdown".split()

        # Populate the list
        for item in items:
            strings.append(item)

    def on_string_selected(self, dropdown, _pspec):
        # Selected Gtk.StringObject
        selected = dropdown.props.selected_item
        if selected is not None:
            print("Selected", selected.props.string)


def on_activate(app):
    win = DropDownWindow(application=app)
    win.present()


app = Gtk.Application(application_id="com.example.App")
app.connect("activate", on_activate)

app.run(None)