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

« back to all changes in this revision

Viewing changes to pygtk/radiobuttons.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: Radio Buttons]
 
4
# [SNIPPET_CATEGORIES: PyGTK]
 
5
# [SNIPPET_DESCRIPTION: Using Radio Buttons]
 
6
 
 
7
# example radiobuttons.py
 
8
 
 
9
import pygtk
 
10
pygtk.require('2.0')
 
11
import gtk
 
12
 
 
13
class RadioButtons:
 
14
    def callback(self, widget, data=None):
 
15
        print "%s was toggled %s" % (data, ("OFF", "ON")[widget.get_active()])
 
16
 
 
17
    def close_application(self, widget, event, data=None):
 
18
        gtk.main_quit()
 
19
        return False
 
20
 
 
21
    def __init__(self):
 
22
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
 
23
  
 
24
        self.window.connect("delete_event", self.close_application)
 
25
 
 
26
        self.window.set_title("radio buttons")
 
27
        self.window.set_border_width(0)
 
28
 
 
29
        box1 = gtk.VBox(False, 0)
 
30
        self.window.add(box1)
 
31
        box1.show()
 
32
 
 
33
        box2 = gtk.VBox(False, 10)
 
34
        box2.set_border_width(10)
 
35
        box1.pack_start(box2, True, True, 0)
 
36
        box2.show()
 
37
 
 
38
        button = gtk.RadioButton(None, "radio button1")
 
39
        button.connect("toggled", self.callback, "radio button 1")
 
40
        box2.pack_start(button, True, True, 0)
 
41
        button.show()
 
42
 
 
43
        button = gtk.RadioButton(button, "radio button2")
 
44
        button.connect("toggled", self.callback, "radio button 2")
 
45
        button.set_active(True)
 
46
        box2.pack_start(button, True, True, 0)
 
47
        button.show()
 
48
 
 
49
        button = gtk.RadioButton(button, "radio button3")
 
50
        button.connect("toggled", self.callback, "radio button 3")
 
51
        box2.pack_start(button, True, True, 0)
 
52
        button.show()
 
53
 
 
54
        separator = gtk.HSeparator()
 
55
        box1.pack_start(separator, False, True, 0)
 
56
        separator.show()
 
57
 
 
58
        box2 = gtk.VBox(False, 10)
 
59
        box2.set_border_width(10)
 
60
        box1.pack_start(box2, False, True, 0)
 
61
        box2.show()
 
62
 
 
63
        button = gtk.Button("close")
 
64
        button.connect_object("clicked", self.close_application, self.window,
 
65
                              None)
 
66
        box2.pack_start(button, True, True, 0)
 
67
        button.set_flags(gtk.CAN_DEFAULT)
 
68
        button.grab_default()
 
69
        button.show()
 
70
        self.window.show()
 
71
 
 
72
def main():
 
73
    gtk.main()
 
74
    return 0        
 
75
 
 
76
if __name__ == "__main__":
 
77
    RadioButtons()
 
78
    main()