[pygtk] How to select a menuitem

BJörn Lindqvist bjourne at gmail.com
Tue Nov 28 00:27:34 WST 2006


> Hi,
>
> how can you select a menuitem with python code?
>
> I want to remember the last menuitem the user took,
> so that I can focus/select it the next time the menu
> pops up.
>
> something like this:
>
> self.flagsmenu.popup(....)
> if self.flagsmenu_lastitem:
>     self.flagsmenu_lastitem.select() # menuitem

GTK remembers the last selected item itself with the method
get_active(). Therefore, you don't need to remember it yourself. A
callback like this should do what you want.

def event_cb(menu, button_ev):
    if button_ev.button == 3:
        for item in menu:
            item.deselect()
        menu.get_active().select()
        menu.popup(None, None, None, button_ev.button, button_ev.time)


-- 
mvh Björn


More information about the pygtk mailing list