Contents Menu Expand Light mode Dark mode Auto light/dark, in light mode Auto light/dark, in dark mode Skip to content
PyGObject
PyGObject
  • Getting Started
  • Tutorials
    • GObject
      • Basics
      • Subclassing
      • Interfaces
      • Weak References
    • GTK4
      • Getting Started
      • GTK4 Basics
      • Application
      • Layout Containers
      • Controls
        • Entries
        • Buttons
        • Check & Radio Buttons
        • SpinButton
        • Switch
        • DropDown
        • Scale
      • Display Widgets
        • Label
        • Image
        • Picture
        • ProgressBar
        • Spinner
      • Popovers
      • Text View
      • Clipboard
      • Drag and Drop
    • Adwaita
      • Adwaita Application
    • GTK3
  • Changelog
  • Bug Tracker / Git / Source
  • User Guide
    • Imports
    • API Reference
      • GI Documentation
      • Basic Types
      • Flags & Enums
      • GObject.Object
        • Signals
        • Properties
        • Weak References
      • Error Handling
    • Cairo Integration
    • Gtk.Template
    • Threads & Concurrency
    • Debugging & Profiling
    • Application Deployment
    • Testing and Continuous Integration
    • Porting from Static Bindings
    • System Dependencies
    • Flatpaking
    • Frequently Asked Questions
  • Development Guide
    • Overview
    • Creating a Development Environment
    • Style Guidelines
    • Python Override Guidelines
  • Packaging Guide
  • Maintainer Guide
  • Further Resources
  • Contact
Back to top
View this page
Edit this page

DropDown¶

Gtk.DropDown allows the user to choose an item from a list of options. They are preferable to having many radio buttons on screen as they take up less room.

To populate its options Gtk.DropDown uses a Gio.ListModel. Gio.ListModel it’s an interface that represents a mutable list of GObject.Objects. For text-only uses cases GTK provides Gtk.StringList, a list model that wraps an array of strings wrapped on Gtk.StringObject and Gtk.DropDown knows how to use it.

Gtk.DropDown can optionally allow search in the popup, which is useful if the list of options is long. To enable the search entry, use Gtk.DropDown.props.enable_search.

Attention

Currently Gtk.DropDown search only works fine in Python if you are using a Gtk.StringList. If you use custom list models with custom gobjects you must provide a Gtk.Expression through Gtk.DropDown.props.expression so Gtk.DropDown can know how to filter the gobjects. The problem is Gtk.Expression is broken in PyGObject for now (see bug report). So search won´t work for at all.

Gtk.DropDown stores the selected item from the list model in Gtk.DropDown.props.selected_item, and the position of that item on Gtk.DropDown.props.selected. To know when the selection has changed just connect to notify::selected-item or notify::selected.

Example¶

We are creating a simple Gtk.DropDown using Gtk.StringList.

../../../_images/dropdown.png
 1import gi
 2
 3gi.require_version('Gtk', '4.0')
 4from gi.repository import Gtk
 5
 6
 7class 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
30def on_activate(app):
31    win = DropDownWindow(application=app)
32    win.present()
33
34
35app = Gtk.Application(application_id='com.example.App')
36app.connect('activate', on_activate)
37
38app.run(None)
Next
Scale
Previous
Switch
Made with Sphinx and @pradyunsg's Furo
On this page
  • DropDown
    • Example