3
# [SNIPPET_NAME: Toggle Button]
4
# [SNIPPET_CATEGORIES: PyGTK]
5
# [SNIPPET_DESCRIPTION: Using a toggle button]
7
# example togglebutton.py
15
# The data passed to this method is printed to stdout
16
def callback(self, widget, data=None):
17
print "%s was toggled %s" % (data, ("OFF", "ON")[widget.get_active()])
19
# This callback quits the program
20
def delete_event(self, widget, event, data=None):
26
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
28
# Set the window title
29
self.window.set_title("Toggle Button")
31
# Set a handler for delete_event that immediately
33
self.window.connect("delete_event", self.delete_event)
35
# Sets the border width of the window.
36
self.window.set_border_width(20)
38
# Create a vertical box
39
vbox = gtk.VBox(True, 2)
41
# Put the vbox in the main window
45
button = gtk.ToggleButton("toggle button 1")
47
# When the button is toggled, we call the "callback" method
48
# with a pointer to "button" as its argument
49
button.connect("toggled", self.callback, "toggle button 1")
53
vbox.pack_start(button, True, True, 2)
57
# Create second button
59
button = gtk.ToggleButton("toggle button 2")
61
# When the button is toggled, we call the "callback" method
62
# with a pointer to "button 2" as its argument
63
button.connect("toggled", self.callback, "toggle button 2")
65
vbox.pack_start(button, True, True, 2)
69
# Create "Quit" button
70
button = gtk.Button("Quit")
72
# When the button is clicked, we call the main_quit function
73
# and the program exits
74
button.connect("clicked", lambda wid: gtk.main_quit())
76
# Insert the quit button
77
vbox.pack_start(button, True, True, 2)
87
if __name__ == "__main__":