| PyGTK Tutorial | ||
|---|---|---|
| <<< Previous | Chapter 6. The Button Widget | Next >>> |
Creating a new radio button is done with
one of these calls:
radio_button = GtkRadioButton(button) radio_button = GtkRadioButton(button, label) |
You'll notice the extra argument to these calls. Radio buttons require a group to operate properly. The first call to GtkRadioButton() should pass None as the first argument and a new radio button group will be created with the new radio button as its only member.
To add more radio buttons to a group, pass in a reference to a radio button in the group in subsequent calls to GtkRadioButton().
It is also a good idea to explicitly set
which button should be the default depressed button with:
radio_button.set_active(state) |
This is described in the section on toggle buttons, and works in exactly the same way. Once the radio buttons are grouped together, only one of the group may be active at a time. If the user clicks on one radio button, and then on another, the first radio button will first emit a "toggled" signal (to report becoming inactive), and then the second will emit its "toggled" signal (to report becoming active).
The example program radiobuttons.py creates a radio button group with three buttons. Figure 6.4 illustrates the resulting window:

1 #!/usr/bin/env python
2
3 # example radiobuttons.py
4
5 import gtk
6
7 class RadioButtons:
8 def callback(self, widget, data=None):
9 print "%s was toggled %s" % (data, ("OFF", "ON")[widget.active]) 10
11 def close_application(self, widget, event, data=None):
12 gtk.mainquit()
13 return gtk.FALSE
14
15 def __init__(self):
16 self.window = gtk.GtkWindow(gtk.WINDOW_TOPLEVEL)
17
18 self.window.connect("delete_event", self.close_application)
19
20 self.window.set_title("radio buttons")
21 self.window.set_border_width(0)
22
23 box1 = gtk.GtkVBox(gtk.FALSE, 0)
24 self.window.add(box1)
25 box1.show()
26
27 box2 = gtk.GtkVBox(gtk.FALSE, 10)
28 box2.set_border_width(10)
29 box1.pack_start(box2, gtk.TRUE, gtk.TRUE, 0)
30 box2.show()
31
32 button = gtk.GtkRadioButton(None, "radio button1")
33 button.connect("toggled", self.callback, "radio button 1")
34 box2.pack_start(button, gtk.TRUE, gtk.TRUE, 0)
35 button.show()
36
37 button = gtk.GtkRadioButton(button, "radio button2")
38 button.connect("toggled", self.callback, "radio button 2")
39 button.set_active(gtk.TRUE)
40 box2.pack_start(button, gtk.TRUE, gtk.TRUE, 0)
41 button.show()
42
43 button = gtk.GtkRadioButton(button, "radio button3")
44 button.connect("toggled", self.callback, "radio button 3")
45 box2.pack_start(button, gtk.TRUE, gtk.TRUE, 0)
46 button.show()
47
48 separator = gtk.GtkHSeparator()
49 box1.pack_start(separator, gtk.FALSE, gtk.TRUE, 0)
50 separator.show()
51
52 box2 = gtk.GtkVBox(gtk.FALSE, 10)
53 box2.set_border_width(10)
54 box1.pack_start(box2, gtk.FALSE, gtk.TRUE, 0)
55 box2.show()
56
57 button = gtk.GtkButton("close")
58 button.connect_object("clicked", self.close_application, self.window,
59 None)
60 box2.pack_start(button, gtk.TRUE, gtk.TRUE, 0)
61 button.set_flags(gtk.CAN_DEFAULT)
62 button.grab_default()
63 button.show()
64 self.window.show()
65
66 def main():
67 gtk.mainloop()
68 return 0
69
70 if __name__ == "__main__":
71 RadioButtons()
72 main()
|
| <<< Previous | Home | Next >>> |
| Check Buttons | Up | Adjustments |