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

« back to all changes in this revision

Viewing changes to pygtk/uimanager.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: UI Manager]
 
4
# [SNIPPET_CATEGORIES: PyGTK]
 
5
# [SNIPPET_DESCRIPTION: Using gtk.UIManager]
 
6
 
 
7
import pygtk
 
8
pygtk.require('2.0')
 
9
import gtk
 
10
 
 
11
class UIManagerExample:
 
12
    ui = '''<ui>
 
13
    <menubar name="MenuBar">
 
14
      <menu action="File">
 
15
        <menuitem action="Quit"/>
 
16
      </menu>
 
17
      <menu action="Sound">
 
18
        <menuitem action="Mute"/>
 
19
      </menu>
 
20
      <menu action="RadioBand">
 
21
        <menuitem action="AM"/>
 
22
        <menuitem action="FM"/>
 
23
        <menuitem action="SSB"/>
 
24
      </menu>
 
25
    </menubar>
 
26
    <toolbar name="Toolbar">
 
27
      <toolitem action="Quit"/>
 
28
      <separator/>
 
29
      <toolitem action="Mute"/>
 
30
      <separator/>
 
31
      <placeholder name="RadioBandItems">
 
32
        <toolitem action="AM"/>
 
33
        <toolitem action="FM"/>
 
34
        <toolitem action="SSB"/>
 
35
      </placeholder>
 
36
    </toolbar>
 
37
    </ui>'''
 
38
      
 
39
    def __init__(self):
 
40
        # Create the toplevel window
 
41
        window = gtk.Window()
 
42
        window.connect('destroy', lambda w: gtk.main_quit())
 
43
        window.set_size_request(300, -1)
 
44
        vbox = gtk.VBox()
 
45
        window.add(vbox)
 
46
 
 
47
        # Create a UIManager instance
 
48
        uimanager = gtk.UIManager()
 
49
 
 
50
        # Add the accelerator group to the toplevel window
 
51
        accelgroup = uimanager.get_accel_group()
 
52
        window.add_accel_group(accelgroup)
 
53
 
 
54
        # Create an ActionGroup
 
55
        actiongroup = gtk.ActionGroup('UIManagerExample')
 
56
        self.actiongroup = actiongroup
 
57
 
 
58
        # Create a ToggleAction, etc.
 
59
        actiongroup.add_toggle_actions([('Mute', None, '_Mute', '<Control>m',
 
60
                                         'Mute the volume', self.mute_cb)])
 
61
 
 
62
        # Create actions
 
63
        actiongroup.add_actions([('Quit', gtk.STOCK_QUIT, '_Quit me!', None,
 
64
                                  'Quit the Program', self.quit_cb),
 
65
                                 ('File', None, '_File'),
 
66
                                 ('Sound', None, '_Sound'),
 
67
                                 ('RadioBand', None, '_Radio Band')])
 
68
        actiongroup.get_action('Quit').set_property('short-label', '_Quit')
 
69
 
 
70
        # Create some RadioActions
 
71
        actiongroup.add_radio_actions([('AM', None, '_AM', '<Control>a',
 
72
                                        'AM Radio', 0),
 
73
                                       ('FM', None, '_FM', '<Control>f',
 
74
                                        'FM Radio', 1),
 
75
                                       ('SSB', None, '_SSB', '<Control>s',
 
76
                                        'SSB Radio', 2),
 
77
                                       ], 0, self.radioband_cb)
 
78
 
 
79
        # Add the actiongroup to the uimanager
 
80
        uimanager.insert_action_group(actiongroup, 0)
 
81
 
 
82
        # Add a UI description
 
83
        uimanager.add_ui_from_string(self.ui)
 
84
 
 
85
        # Create a MenuBar
 
86
        menubar = uimanager.get_widget('/MenuBar')
 
87
        vbox.pack_start(menubar, False)
 
88
 
 
89
        # Create a Toolbar
 
90
        toolbar = uimanager.get_widget('/Toolbar')
 
91
        vbox.pack_start(toolbar, False)
 
92
 
 
93
        # Create and pack two Labels
 
94
        label = gtk.Label('Sound is not muted')
 
95
        vbox.pack_start(label)
 
96
        self.mutelabel = label
 
97
        label = gtk.Label('Radio band is AM')
 
98
        vbox.pack_start(label)
 
99
        self.bandlabel = label
 
100
 
 
101
        # Create buttons to control visibility and sensitivity of actions
 
102
        buttonbox = gtk.HButtonBox()
 
103
        sensitivebutton = gtk.CheckButton('Sensitive')
 
104
        sensitivebutton.set_active(True)
 
105
        sensitivebutton.connect('toggled', self.toggle_sensitivity)
 
106
        visiblebutton = gtk.CheckButton('Visible')
 
107
        visiblebutton.set_active(True)
 
108
        visiblebutton.connect('toggled', self.toggle_visibility)
 
109
        # add them to buttonbox
 
110
        buttonbox.pack_start(sensitivebutton, False)
 
111
        buttonbox.pack_start(visiblebutton, False)
 
112
        vbox.pack_start(buttonbox)
 
113
 
 
114
        window.show_all()
 
115
        return
 
116
 
 
117
    def mute_cb(self, action):
 
118
        # action has not toggled yet
 
119
        text = ('muted', 'not muted')[action.get_active()==False]
 
120
        self.mutelabel.set_text('Sound is %s' % text)
 
121
        return
 
122
 
 
123
    def radioband_cb(self, action, current):
 
124
        text = ('AM', 'FM', 'SSB')[action.get_current_value()]
 
125
        self.bandlabel.set_text('Radio band is %s' % text)
 
126
        return
 
127
 
 
128
    def quit_cb(self, b):
 
129
        print 'Quitting program'
 
130
        gtk.main_quit()
 
131
 
 
132
    def toggle_sensitivity(self, b):
 
133
        self.actiongroup.set_sensitive(b.get_active())
 
134
        return
 
135
 
 
136
    def toggle_visibility(self, b):
 
137
        self.actiongroup.set_visible(b.get_active())
 
138
        return
 
139
 
 
140
if __name__ == '__main__':
 
141
    ba = UIManagerExample()
 
142
    gtk.main()