win32: Popup menus do not close when user clicks elewhere
Submitted by Chris Billington
I've been writing a program with a gtk.StatusIcon, which when clicked, pops up a menu.
The problem is that the popup menu doesn't disappear when the user clicks away. It does in Ubuntu, but not in Windows XP or 7, using the pygtk 2.22.6.win32-py2.7 all in one installer.
I've tried to catch signals that are emitted when the user clicks elsewhere, and on Ubuntu I see the 'deactivate' and 'selection-done' signals emitted when I click off, but not in Windows. It appears that the menu never has focus in the first place, so clicking off doesn't cause it to lose it.
I would be filing this bug against gtk instead of pygtk, but it was apparently fixed there back in 2005:
There is a workaround that uses 'leave-notify' signals to make the menu popdown, since enter and leave signals are still being emitted:
Here is code to reproduce:
class Popup: def init(self): self.icon = gtk.status_icon_new_from_stock(gtk.STOCK_OPEN) self.menu = gtk.Menu() self.quit = gtk.MenuItem('Quit') self.quit.connect('activate',gtk.main_quit) self.menu.append(self.quit) for item in [gtk.MenuItem('Menu item %d'%i) for i in range(5)]: self.menu.append(item) self.menu.show_all() self.icon.connect('popup-menu',self.on_icon_right_clicked) self.menu.connect('deactivate',self.on_menu_deactivate) self.menu.connect('selection-done',self.on_menu_selection_done)
def on_icon_right_clicked(self,widget,button,time,data=None): self.menu.popup(None,None,None,0,time,self.icon) def on_menu_deactivate(self,*args): print 'menu deactivate signal!' def on_menu_selection_done(self,*args): print 'menu selection-done signal!'
if name == 'main': popup = Popup() gtk.main()
Any help would be appreciated!