Today's episode is Gtk RadioButtons. How to get them to work and how to use them.
First, here's the picture of where we are going (the glade file is attached below): And, here's the python code
The really important part of getting RadioButtons to work is to define them as members of a group. This is done by selecting each (except the first one) in turn and clicking on the General > Group property icon to display the property dialog. Therein, select the first RadioButton and click ok. Now, they all belong to the same group as defined (by default) by the first RadioButton.
Code: Select all
#! /usr/bin/env python3 from gi.repository import Gtk class RadioButton(object): def __init__(self): self.builder = Gtk.Builder() self.builder.add_from_file('RadioButton.glade') # We need to create an handle for our window so # that we can show. self.window = self.builder.get_object('window1') # Connect the signals that we have defined in our glade # project. self.builder.connect_signals(self) self.window.show() # Close the GUI when the user clicks on the # window close widget in the upper right corner. def on_window1_destroy_event(self, *argc): Gtk.main_quit() # The parameter 'widget' is our RadioButton that # tripped the event handler. def on_radiobutton_toggled_event(self, widget): if widget.get_active(): state = "on" else: state = "off" print((Gtk.Buildable.get_name(widget) + ' was turned ' + state)) if __name__ == '__main__': try: RadioButton() Gtk.main() except KeyboardInterrupt: pass
The program lets you know what is going on as you click one RadioButton after another.
Code: Select all
$ python ./RadioButton.py radiobutton1 was turned off radiobutton2 was turned on radiobutton2 was turned off radiobutton3 was turned on radiobutton3 was turned off radiobutton1 was turned on