~doctormo/python-snippets/lp-merge-request-example

« back to all changes in this revision

Viewing changes to pygtk/togglebutton.py

  • Committer: Jono Bacon
  • Date: 2009-12-31 01:32:01 UTC
  • Revision ID: jono@ubuntu.com-20091231013201-0jqe2hpla824dafl
Initial import.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
#
 
3
# [SNIPPET_NAME: Toggle Button]
 
4
# [SNIPPET_CATEGORIES: PyGTK]
 
5
# [SNIPPET_DESCRIPTION: Using a toggle button]
 
6
 
 
7
# example togglebutton.py
 
8
 
 
9
import pygtk
 
10
pygtk.require('2.0')
 
11
import gtk
 
12
 
 
13
class ToggleButton:
 
14
    # Our callback.
 
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()])
 
18
 
 
19
    # This callback quits the program
 
20
    def delete_event(self, widget, event, data=None):
 
21
        gtk.main_quit()
 
22
        return False
 
23
 
 
24
    def __init__(self):
 
25
        # Create a new window
 
26
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
 
27
 
 
28
        # Set the window title
 
29
        self.window.set_title("Toggle Button")
 
30
 
 
31
        # Set a handler for delete_event that immediately
 
32
        # exits GTK.
 
33
        self.window.connect("delete_event", self.delete_event)
 
34
 
 
35
        # Sets the border width of the window.
 
36
        self.window.set_border_width(20)
 
37
 
 
38
        # Create a vertical box
 
39
        vbox = gtk.VBox(True, 2)
 
40
 
 
41
        # Put the vbox in the main window
 
42
        self.window.add(vbox)
 
43
 
 
44
        # Create first button
 
45
        button = gtk.ToggleButton("toggle button 1")
 
46
 
 
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")
 
50
 
 
51
 
 
52
        # Insert button 1
 
53
        vbox.pack_start(button, True, True, 2)
 
54
 
 
55
        button.show()
 
56
 
 
57
        # Create second button
 
58
 
 
59
        button = gtk.ToggleButton("toggle button 2")
 
60
 
 
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")
 
64
        # Insert button 2
 
65
        vbox.pack_start(button, True, True, 2)
 
66
 
 
67
        button.show()
 
68
 
 
69
        # Create "Quit" button
 
70
        button = gtk.Button("Quit")
 
71
 
 
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())
 
75
 
 
76
        # Insert the quit button
 
77
        vbox.pack_start(button, True, True, 2)
 
78
 
 
79
        button.show()
 
80
        vbox.show()
 
81
        self.window.show()
 
82
 
 
83
def main():
 
84
    gtk.main()
 
85
    return 0       
 
86
 
 
87
if __name__ == "__main__":
 
88
    ToggleButton()
 
89
    main()