9. Button Widgets

9.1. Knopf

The Button widget is another commonly used widget. It is generally used to attach a function that is called when the button is pressed.

The Gtk.Button widget can hold any valid child widget. That is it can hold most any other standard Gtk.Widget. The most commonly used child is the Gtk.Label.

Usually, you want to connect to the button’s „clicked“ signal which is emitted when the button has been pressed and released.

9.1.1. Beispiel

_images/button_example.png
 1import gi
 2
 3gi.require_version("Gtk", "3.0")
 4from gi.repository import Gtk
 5
 6
 7class ButtonWindow(Gtk.Window):
 8    def __init__(self):
 9        Gtk.Window.__init__(self, title="Button Demo")
10        self.set_border_width(10)
11
12        hbox = Gtk.Box(spacing=6)
13        self.add(hbox)
14
15        button = Gtk.Button.new_with_label("Click Me")
16        button.connect("clicked", self.on_click_me_clicked)
17        hbox.pack_start(button, True, True, 0)
18
19        button = Gtk.Button.new_with_mnemonic("_Open")
20        button.connect("clicked", self.on_open_clicked)
21        hbox.pack_start(button, True, True, 0)
22
23        button = Gtk.Button.new_with_mnemonic("_Close")
24        button.connect("clicked", self.on_close_clicked)
25        hbox.pack_start(button, True, True, 0)
26
27    def on_click_me_clicked(self, button):
28        print('"Click me" button was clicked')
29
30    def on_open_clicked(self, button):
31        print('"Open" button was clicked')
32
33    def on_close_clicked(self, button):
34        print("Closing application")
35        Gtk.main_quit()
36
37
38win = ButtonWindow()
39win.connect("destroy", Gtk.main_quit)
40win.show_all()
41Gtk.main()

9.2. ToggleButton

A Gtk.ToggleButton is very similar to a normal Gtk.Button, but when clicked they remain activated, or pressed, until clicked again. When the state of the button is changed, the „toggled“ signal is emitted.

To retrieve the state of the Gtk.ToggleButton, you can use the Gtk.ToggleButton.get_active() method. This returns True if the button is „down“. You can also set the toggle button’s state, with Gtk.ToggleButton.set_active(). Note that, if you do this, and the state actually changes, it causes the „toggled“ signal to be emitted.

9.2.1. Beispiel

_images/togglebutton_example.png
 1import gi
 2
 3gi.require_version("Gtk", "3.0")
 4from gi.repository import Gtk
 5
 6
 7class ToggleButtonWindow(Gtk.Window):
 8    def __init__(self):
 9        Gtk.Window.__init__(self, title="ToggleButton Demo")
10        self.set_border_width(10)
11
12        hbox = Gtk.Box(spacing=6)
13        self.add(hbox)
14
15        button = Gtk.ToggleButton(label="Button 1")
16        button.connect("toggled", self.on_button_toggled, "1")
17        hbox.pack_start(button, True, True, 0)
18
19        button = Gtk.ToggleButton(label="B_utton 2", use_underline=True)
20        button.set_active(True)
21        button.connect("toggled", self.on_button_toggled, "2")
22        hbox.pack_start(button, True, True, 0)
23
24    def on_button_toggled(self, button, name):
25        if button.get_active():
26            state = "on"
27        else:
28            state = "off"
29        print("Button", name, "was turned", state)
30
31
32win = ToggleButtonWindow()
33win.connect("destroy", Gtk.main_quit)
34win.show_all()
35Gtk.main()

9.3. CheckButton

Gtk.CheckButton inherits from Gtk.ToggleButton. The only real difference between the two is Gtk.CheckButton’s appearance. A Gtk.CheckButton places a discrete Gtk.ToggleButton next to a widget, (usually a Gtk.Label). The „toggled“ signal, Gtk.ToggleButton.set_active() and Gtk.ToggleButton.get_active() are inherited.

9.4. Auswahlknopf

Like checkboxes, radio buttons also inherit from Gtk.ToggleButton, but these work in groups, and only one Gtk.RadioButton in a group can be selected at any one time. Therefore, a Gtk.RadioButton is one way of giving the user a choice from many options.

Radio buttons can be created with one of the static methods Gtk.RadioButton.new_from_widget(), Gtk.RadioButton.new_with_label_from_widget() or Gtk.RadioButton.new_with_mnemonic_from_widget(). The first radio button in a group will be created passing None as the group argument. In subsequent calls, the group you wish to add this button to should be passed as an argument.

When first run, the first radio button in the group will be active. This can be changed by calling Gtk.ToggleButton.set_active() with True as first argument.

Changing a Gtk.RadioButton’s widget group after its creation can be achieved by calling Gtk.RadioButton.join_group().

9.4.1. Beispiel

_images/radiobutton_example.png
 1import gi
 2
 3gi.require_version("Gtk", "3.0")
 4from gi.repository import Gtk
 5
 6
 7class RadioButtonWindow(Gtk.Window):
 8    def __init__(self):
 9        Gtk.Window.__init__(self, title="RadioButton Demo")
10        self.set_border_width(10)
11
12        hbox = Gtk.Box(spacing=6)
13        self.add(hbox)
14
15        button1 = Gtk.RadioButton.new_with_label_from_widget(None, "Button 1")
16        button1.connect("toggled", self.on_button_toggled, "1")
17        hbox.pack_start(button1, False, False, 0)
18
19        button2 = Gtk.RadioButton.new_from_widget(button1)
20        button2.set_label("Button 2")
21        button2.connect("toggled", self.on_button_toggled, "2")
22        hbox.pack_start(button2, False, False, 0)
23
24        button3 = Gtk.RadioButton.new_with_mnemonic_from_widget(button1, "B_utton 3")
25        button3.connect("toggled", self.on_button_toggled, "3")
26        hbox.pack_start(button3, False, False, 0)
27
28    def on_button_toggled(self, button, name):
29        if button.get_active():
30            state = "on"
31        else:
32            state = "off"
33        print("Button", name, "was turned", state)
34
35
36win = RadioButtonWindow()
37win.connect("destroy", Gtk.main_quit)
38win.show_all()
39Gtk.main()

9.5. LinkButton

A Gtk.LinkButton is a Gtk.Button with a hyperlink, similar to the one used by web browsers, which triggers an action when clicked. It is useful to show quick links to resources.

The URI bound to a Gtk.LinkButton can be set specifically using Gtk.LinkButton.set_uri(), and retrieved using Gtk.LinkButton.get_uri().

9.5.1. Beispiel

_images/linkbutton_example.png
 1import gi
 2
 3gi.require_version("Gtk", "3.0")
 4from gi.repository import Gtk
 5
 6
 7class LinkButtonWindow(Gtk.Window):
 8    def __init__(self):
 9        Gtk.Window.__init__(self, title="LinkButton Demo")
10        self.set_border_width(10)
11
12        button = Gtk.LinkButton.new_with_label(
13            uri="https://www.gtk.org",
14            label="Visit GTK+ Homepage"
15        )
16        self.add(button)
17
18
19win = LinkButtonWindow()
20win.connect("destroy", Gtk.main_quit)
21win.show_all()
22Gtk.main()

9.6. Einstellfeld

A Gtk.SpinButton is an ideal way to allow the user to set the value of some attribute. Rather than having to directly type a number into a Gtk.Entry, Gtk.SpinButton allows the user to click on one of two arrows to increment or decrement the displayed value. A value can still be typed in, with the bonus that it can be checked to ensure it is in a given range. The main properties of a Gtk.SpinButton are set through Gtk.Adjustment.

To change the value that Gtk.SpinButton is showing, use Gtk.SpinButton.set_value(). The value entered can either be an integer or float, depending on your requirements, use Gtk.SpinButton.get_value() or Gtk.SpinButton.get_value_as_int(), respectively.

When you allow the displaying of float values in the spin button, you may wish to adjust the number of decimal spaces displayed by calling Gtk.SpinButton.set_digits().

By default, Gtk.SpinButton accepts textual data. If you wish to limit this to numerical values only, call Gtk.SpinButton.set_numeric() with True as argument.

We can also adjust the update policy of Gtk.SpinButton. There are two options here; by default the spin button updates the value even if the data entered is invalid. Alternatively, we can set the policy to only update when the value entered is valid by calling Gtk.SpinButton.set_update_policy().

9.6.1. Beispiel

_images/spinbutton_example.png
 1import gi
 2
 3gi.require_version("Gtk", "3.0")
 4from gi.repository import Gtk
 5
 6
 7class SpinButtonWindow(Gtk.Window):
 8    def __init__(self):
 9        Gtk.Window.__init__(self, title="SpinButton Demo")
10        self.set_border_width(10)
11
12        hbox = Gtk.Box(spacing=6)
13        self.add(hbox)
14
15        adjustment = Gtk.Adjustment(upper=100, step_increment=1, page_increment=10)
16        self.spinbutton = Gtk.SpinButton()
17        self.spinbutton.set_adjustment(adjustment)
18        self.spinbutton.connect("value-changed", self.on_value_changed)
19        hbox.pack_start(self.spinbutton, False, False, 0)
20
21        check_numeric = Gtk.CheckButton(label="Numeric")
22        check_numeric.connect("toggled", self.on_numeric_toggled)
23        hbox.pack_start(check_numeric, False, False, 0)
24
25        check_ifvalid = Gtk.CheckButton(label="If Valid")
26        check_ifvalid.connect("toggled", self.on_ifvalid_toggled)
27        hbox.pack_start(check_ifvalid, False, False, 0)
28
29    def on_value_changed(self, scroll):
30        print(self.spinbutton.get_value_as_int())
31
32    def on_numeric_toggled(self, button):
33        self.spinbutton.set_numeric(button.get_active())
34
35    def on_ifvalid_toggled(self, button):
36        if button.get_active():
37            policy = Gtk.SpinButtonUpdatePolicy.IF_VALID
38        else:
39            policy = Gtk.SpinButtonUpdatePolicy.ALWAYS
40        self.spinbutton.set_update_policy(policy)
41
42
43win = SpinButtonWindow()
44win.connect("destroy", Gtk.main_quit)
45win.show_all()
46Gtk.main()

9.7. Schalter

A Gtk.Switch is a widget that has two states: on or off. The user can control which state should be active by clicking the empty area, or by dragging the handle.

You shouldn’t use the „activate“ signal on the Gtk.Switch which is an action signal and emitting it causes the switch to animate. Applications should never connect to this signal, but use the „notify::active“ signal, see the example here below.

9.7.1. Beispiel

_images/switch_example.png
 1import gi
 2
 3gi.require_version("Gtk", "3.0")
 4from gi.repository import Gtk
 5
 6
 7class SwitcherWindow(Gtk.Window):
 8    def __init__(self):
 9        Gtk.Window.__init__(self, title="Switch Demo")
10        self.set_border_width(10)
11
12        hbox = Gtk.Box(spacing=6)
13        self.add(hbox)
14
15        switch = Gtk.Switch()
16        switch.connect("notify::active", self.on_switch_activated)
17        switch.set_active(False)
18        hbox.pack_start(switch, True, True, 0)
19
20        switch = Gtk.Switch()
21        switch.connect("notify::active", self.on_switch_activated)
22        switch.set_active(True)
23        hbox.pack_start(switch, True, True, 0)
24
25    def on_switch_activated(self, switch, gparam):
26        if switch.get_active():
27            state = "on"
28        else:
29            state = "off"
30        print("Switch was turned", state)
31
32
33win = SwitcherWindow()
34win.connect("destroy", Gtk.main_quit)
35win.show_all()
36Gtk.main()