How to select a MenuItem programatically

Posted by Shaung on Stack Overflow See other posts from Stack Overflow or by Shaung
Published on 2010-10-20T06:22:51Z Indexed on 2010/12/30 4:54 UTC
Read the original article Hit count: 233

Filed under:
|
|

I am trying to add a global shortcut to a gtk.MenuItem which has a sub menu.

Here is my code:

import pygtk, gtk
import keybinder

dlg = gtk.Dialog('menu test')
dlg.set_size_request(200, 40)

menubar = gtk.MenuBar()
menubar.show()
menuitem = gtk.MenuItem('foo')
menuitem.show()
menubar.append(menuitem)

mitem = gtk.MenuItem('bar')
mitem.show()
menu = gtk.Menu()
menu.add(mitem)
menu.show()
menuitem.set_submenu(menu)

def show_menu_cb():
    menubar.select_item(menuitem)

keybinder.bind('<Super>i', show_menu_cb)

dlg.vbox.pack_start(menubar)
dlg.show()
dlg.run()

When I press the key menu pops up, I can then select items in the sub menu or press Esc to make it disappear. But after that the menuitem keeps selected and other windows never get input focus again. I have to click on the menuitem twice to get everything back normal.

© Stack Overflow or respective owner

Related posts about python

Related posts about gtk