How to select a MenuItem programatically
- by Shaung
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.