3
# [SNIPPET_NAME: UI Manager]
4
# [SNIPPET_CATEGORIES: PyGTK]
5
# [SNIPPET_DESCRIPTION: Using gtk.UIManager]
11
class UIManagerExample:
13
<menubar name="MenuBar">
15
<menuitem action="Quit"/>
18
<menuitem action="Mute"/>
20
<menu action="RadioBand">
21
<menuitem action="AM"/>
22
<menuitem action="FM"/>
23
<menuitem action="SSB"/>
26
<toolbar name="Toolbar">
27
<toolitem action="Quit"/>
29
<toolitem action="Mute"/>
31
<placeholder name="RadioBandItems">
32
<toolitem action="AM"/>
33
<toolitem action="FM"/>
34
<toolitem action="SSB"/>
40
# Create the toplevel window
42
window.connect('destroy', lambda w: gtk.main_quit())
43
window.set_size_request(300, -1)
47
# Create a UIManager instance
48
uimanager = gtk.UIManager()
50
# Add the accelerator group to the toplevel window
51
accelgroup = uimanager.get_accel_group()
52
window.add_accel_group(accelgroup)
54
# Create an ActionGroup
55
actiongroup = gtk.ActionGroup('UIManagerExample')
56
self.actiongroup = actiongroup
58
# Create a ToggleAction, etc.
59
actiongroup.add_toggle_actions([('Mute', None, '_Mute', '<Control>m',
60
'Mute the volume', self.mute_cb)])
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')
70
# Create some RadioActions
71
actiongroup.add_radio_actions([('AM', None, '_AM', '<Control>a',
73
('FM', None, '_FM', '<Control>f',
75
('SSB', None, '_SSB', '<Control>s',
77
], 0, self.radioband_cb)
79
# Add the actiongroup to the uimanager
80
uimanager.insert_action_group(actiongroup, 0)
82
# Add a UI description
83
uimanager.add_ui_from_string(self.ui)
86
menubar = uimanager.get_widget('/MenuBar')
87
vbox.pack_start(menubar, False)
90
toolbar = uimanager.get_widget('/Toolbar')
91
vbox.pack_start(toolbar, False)
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
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)
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)
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)
128
def quit_cb(self, b):
129
print 'Quitting program'
132
def toggle_sensitivity(self, b):
133
self.actiongroup.set_sensitive(b.get_active())
136
def toggle_visibility(self, b):
137
self.actiongroup.set_visible(b.get_active())
140
if __name__ == '__main__':
141
ba = UIManagerExample()