6.2. Toggle Buttons

Toggle buttons are derived from normal buttons and are very similar, except they will always be in one of two states, alternated by a click. They may be depressed, and when you click again, they will pop back up. Click again, and they will pop back down.

Toggle buttons are the basis for check buttons and radio buttons, as such, many of the calls used for toggle buttons are inherited by radio and check buttons. I will point these out when we come to them.

Creating a new toggle button:
toggle_button = GtkToggleButton()

toggle_button = GtkToggleButton(label)

As you can imagine, these work identically to the normal button widget calls. The first creates a blank toggle button, and the second, a button with a label widget already packed into it.

To retrieve the state of the toggle widget, including radio and check buttons, we use a construct as shown in our example below. This tests the state of the toggle, by accessing the active attribute of the toggle button object. The signal of interest to us emitted by toggle buttons (the toggle button, check button, and radio button widgets) is the "toggled" signal. To check the state of these buttons, set up a signal handler to catch the toggled signal, and access the object attributes to determine its state. The callback will look something like:
def toggle_button_callback(widget, data):
    if widget.active:
        # If control reaches here, the toggle button is down
        # If control reaches here, the toggle button is up

To force the state of a toggle button, and its children, the radio and check buttons, use this method:

The above call can be used to set the state of the toggle button, and its children the radio and check buttons. Passing in your created button as the first argument, and a TRUE or FALSE for the state argument to specify whether it should be down (depressed) or up (released). Default is up, or FALSE.

Note that when you use the set_active() method, and the state is actually changed, it causes the "clicked" and "toggled" signals to be emitted from the button.

This method returns the current state of the toggle button as a boolean TRUE/FALSE value. It returns the same result as using the active attribute.

The togglebutton.py program provides a simple example of toggle buttons. Figure 6.2 illustrates the resulting window with the second toggle button active:

Figure 6.2 Toggle Button Example

The source code for the program is:
    1   #!/usr/bin/env python
    3   # example togglebutton.py
    5   import gtk
    7   class ToggleButton:
    8       # Our callback.
    9       # The data passed to this method is printed to stdout
   10       def callback(self, widget, data=None):
   11           print "%s was toggled %s" % (data, ("OFF", "ON")[widget.active])   12   
   13       # This callback quits the program
   14       def delete_event(self, widget, event, data=None):
   15           gtk.mainquit()
   16           return gtk.FALSE
   18       def __init__(self):
   19           # Create a new window
   20           self.window = gtk.GtkWindow(gtk.WINDOW_TOPLEVEL)
   22           # Set the window title
   23           self.window.set_title("Toggle Button")
   25           # Set a handler for delete_event that immediately
   26           # exits GTK.
   27           self.window.connect("delete_event", self.delete_event)
   29           # Sets the border width of the window.
   30           self.window.set_border_width(20)
   32           # Create a vertical box
   33           vbox = gtk.GtkVBox(gtk.TRUE, 2)
   35           # Put the vbox in the main window
   36           self.window.add(vbox)
   38           # Create first button
   39           button = gtk.GtkToggleButton("toggle button 1")
   41           # When the button is toggled, we call the "callback" method
   42           # with a pointer to "button" as its argument
   43           button.connect("toggled", self.callback, "toggle button 1")
   46           # Insert button 1 into the upper left quadrant of the table
   47           vbox.pack_start(button, gtk.TRUE, gtk.TRUE, 2)
   49           button.show()
   51           # Create second button
   53           button = gtk.GtkToggleButton("toggle button 2")
   55           # When the button is toggled, we call the "callback" method
   56           # with a pointer to "button 2" as its argument
   57           button.connect("toggled", self.callback, "toggle button 2")
   58           # Insert button 2 into the upper right quadrant of the table
   59           vbox.pack_start(button, gtk.TRUE, gtk.TRUE, 2)
   61           button.show()
   63           # Create "Quit" button
   64           button = gtk.GtkButton("Quit")
   66           # When the button is clicked, we call the mainquit function
   67           # and the program exits
   68           button.connect("clicked", gtk.mainquit)
   70           # Insert the quit button into the both lower quadrants of the table
   71           vbox.pack_start(button, gtk.TRUE, gtk.TRUE, 2)
   73           button.show()
   74           vbox.show()
   75           self.window.show()
   77   def main():
   78       gtk.mainloop()
   79       return 0       
   81   if __name__ == "__main__":
   82       ToggleButton()
   83       main()

The interesting lines are 10-11 which define the callback() method that prints the toggle button label and it's state when it is toggled. Lines 43 and 57 connect the "toggled" signal of the toggle buttons to the callback() method.