[pygtk] Popup menu and accelerators
John Finlay
finlay at moeraki.com
Thu Mar 30 12:22:26 WST 2006
Sebastian Pölsterl wrote:
> Hi!
>
> I generate a popup menu with gtk.UIManager and want to add accellerators
> to the menu's items. If I press one of the key combinations the right
> function will be called, but the popup menu itsself doesn't show the
> combination next to the item. In the end it should look like
> http://developer.gnome.org/projects/gup/hig/2.0/images/file-menu.png
> But currently, Ctrl+N etc. on the right are missing.
>
> Here is my code:
> self.uidef = '''<ui>
> <popup>
> <menuitem name="view" action="View" />
> <separator />
> <menuitem name="edit" action="Edit" />
> <menuitem name="delete" action="Delete" />
> </popup>
> </ui>'''
>
> self.ui = gtk.UIManager()
>
> self.actiongroup = gtk.ActionGroup('UIManager')
> self.actiongroup.add_actions([('View', None, _('View'), None,
> None, self.on_treeview_browse_movies_row_activated),
> ('Edit', gtk.STOCK_EDIT, _('Edit'),
> None, None,
> self.on_toolbutton_edit_clicked),
> ('Delete', gtk.STOCK_DELETE,
> _('Delete'), None, None,
> self.on_toolbutton_delete_clicked)])
>
> self.ui.insert_action_group(self.actiongroup, 0)
>
> merge_id = self.ui.add_ui_from_string(self.uidef)
> self.popup_menu = self.ui.get_widget('/popup')
> (view_item, seperator, edit_item, delete_item) =
> self.popup_menu.get_children()
>
> new_accel_group = gtk.AccelGroup()
> self.popup_menu.set_accel_group(new_accel_group)
>
> view_item.add_accelerator('activate', new_accel_group,
> ord('V'), gtk.gdk.CONTROL_MASK,
> gtk.ACCEL_VISIBLE)
> edit_item.add_accelerator('activate', new_accel_group,
> ord('E'), gtk.gdk.CONTROL_MASK,
> gtk.ACCEL_VISIBLE)
> delete_item.add_accelerator('activate', new_accel_group,
> ord('D'), gtk.gdk.CONTROL_MASK,
> gtk.ACCEL_VISIBLE)
> self.window_main.add_accel_group(new_accel_group)
>
>
Try adding the accelerators when adding actions to the actiongroup, for
example:
self.actiongroup.add_actions([('View', None, _('View'), '<Control>V',
None, self.on_treeview_browse_movies_row_activated),
('Edit', gtk.STOCK_EDIT, _('Edit'),
None, None,
self.on_toolbutton_edit_clicked),
('Delete', gtk.STOCK_DELETE,
_('Delete'), None, None,
self.on_toolbutton_delete_clicked)])
You also have to get the uimanager accelgroup and add it to the toplevel window before inserting the actiongroup in the uimanager. See the uimanager.py example program in the tutorial:
http://www.pygtk.org/pygtk2tutorial/sec-UIManager.html#sec-SimpleUIManagerExample
John
More information about the pygtk
mailing list