[pygtk] Popup menu and accelerators

Sebastian Pölsterl marduk at k-d-w.org
Thu Mar 30 05:08:11 WST 2006


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)

-- 
Greetings,
Sebastian Pölsterl


More information about the pygtk mailing list