| PyGTK Tutorial | ||
|---|---|---|
| <<< Previous | Chapter 6. The Button Widget | Next >>> |
The two creation methods are similar to
those of the normal button.
check_button = GtkCheckButton() check_button = GtkCheckButton(label) |
The second method creates a check button with a label beside it.
Checking the state of the check button is identical to that of the toggle button.
The checkbutton.py program provides an example of the use of the check buttons. Figure 6.3 illustrates the resulting window:

1 #!/usr/bin/env python
2
3 # example checkbutton.py
4
5 import gtk
6
7 class CheckButton:
8 # Our callback.
9 # The data passed to this methods 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
17
18 def __init__(self):
19 # Create a new window
20 self.window = gtk.GtkWindow(gtk.WINDOW_TOPLEVEL)
21
22 # Set the window title
23 self.window.set_title("Check Button")
24
25 # Set a handler for delete_event that immediately
26 # exits GTK.
27 self.window.connect("delete_event", self.delete_event)
28
29 # Sets the border width of the window.
30 self.window.set_border_width(20)
31
32 # Create a vertical box
33 vbox = gtk.GtkVBox(gtk.TRUE, 2)
34
35 # Put the vbox in the main window
36 self.window.add(vbox)
37
38 # Create first button
39 button = gtk.GtkCheckButton("check button 1")
40
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, "check button 1")
44
45
46 # Insert button 1 into the upper left quadrant of the table
47 vbox.pack_start(button, gtk.TRUE, gtk.TRUE, 2)
48
49 button.show()
50
51 # Create second button
52
53 button = gtk.GtkCheckButton("check button 2")
54
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, "check button 2")
58 # Insert button 2 into the upper right quadrant of the table
59 vbox.pack_start(button, gtk.TRUE, gtk.TRUE, 2)
60
61 button.show()
62
63 # Create "Quit" button
64 button = gtk.GtkButton("Quit")
65
66 # When the button is clicked, we call the mainquit function
67 # and the program exits
68 button.connect("clicked", gtk.mainquit)
69
70 # Insert the quit button into the both lower quadrants of the table
71 vbox.pack_start(button, gtk.TRUE, gtk.TRUE, 2)
72
73 button.show()
74 vbox.show()
75 self.window.show()
76
77 def main():
78 gtk.mainloop()
79 return 0
80
81 if __name__ == "__main__":
82 CheckButton()
83 main()
|
| <<< Previous | Home | Next >>> |
| Toggle Buttons | Up | Radio Buttons |