10. Expander — Python GTK+ 3 Tutorial 3.4 documentation (original) (raw)
Expanders allow to dynamically hide or show information within a window or dialog. An expander can take a single widget that will be displayed when expanded.
Expanders remain expanded until clicked again. When the state of an expander is changed, the “activate” signal is emitted.
An expander can be programmatically expanded or collapsed by passing True orFalse to Gtk.Expander.set_expanded(). Note that doing so causes the “activate” signal to be emitted.
More than one widget, such as a Gtk.Label and Gtk.Button, can be added by appending them to a Gtk.Box.
10.1. Example
1import gi 2 3gi.require_version("Gtk", "3.0") 4from gi.repository import Gtk 5 6 7class ExpanderExample(Gtk.Window): 8 def init(self): 9 super().init(title="Expander Demo") 10 11 self.set_size_request(350, 100) 12 13 vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6) 14 self.add(vbox) 15 16 text_expander = Gtk.Expander( 17 label="This expander displays additional information" 18 ) 19 text_expander.set_expanded(True) 20 vbox.add(text_expander) 21 22 msg = """ 23This message is quite long, complicated even: 24 - It has a list with a sublist: 25 - of 3 elements; 26 - taking several lines; 27 - with indentation. 28""" 29 details = Gtk.Label(label=msg) 30 text_expander.add(details) 31 32 widget_expander = Gtk.Expander(label="Expand for more controls") 33 vbox.add(widget_expander) 34 35 expander_hbox = Gtk.HBox() 36 widget_expander.add(expander_hbox) 37 38 expander_hbox.add(Gtk.Label(label="Text message")) 39 expander_hbox.add(Gtk.Button(label="Click me")) 40 41 self.show_all() 42 43 44win = ExpanderExample() 45win.connect("destroy", Gtk.main_quit) 46win.show_all() 47Gtk.main()