Somebody posted in the Pygtk mailing list and said he found a bug on the context menu of pygtk. The test code really confirms the alleged bug but someone also replied that that particular problem didn’t occur when he test the code.
.
Below is the problem:

When creating a context menu with a submenu, the submenu doesn’t get focus until the menu item it’s attached to is clicked (even though the submenu appears when the mouse is over the menu item.)

The upshot is that items in the sub menu don’t emit the activate signal unless the parent menu item is clicked first.

Here is some test code demonstrating this problem:

[python]
import pygtk
pygtk.require(’2.0′)
import gtk

class bugtest:
def hello(self, widget, data=None):
print “hello”
return False

def contextMenu(self, widget, event, data=None):
menu = gtk.Menu()

one = gtk.MenuItem(“One”)
menu.append(one)

submenu = gtk.Menu()
two = gtk.MenuItem(“Two”)
two.connect(“activate”, self.hello)
submenu.append(two)
one.set_submenu(submenu)

three = gtk.MenuItem(“Three”)
three.connect(“activate”, self.hello)
menu.append(three)

menu.show_all()

menu.popup(None, None, None, event.button, event.get_time())
return True

def __init__(self):
win = gtk.Window()
label = gtk.Label(‘Submenu bug test’)
evbox = gtk.EventBox()
evbox.add(label)
win.add(evbox)
win.resize(200,200)
evbox.connect(‘button-release-event’, self.contextMenu)
win.show_all()

def main(self):
gtk.main()

if __name__ == “__main__”:
a = bugtest()
a.main()
[/python]

My proposed workaround on that particular problem is that i’ve just changed that “activate” signal to “button-press-event” signal.

[python]
submenu = gtk.Menu()
two = gtk.MenuItem(“Two”)
two.connect(“button-press-event”, self.hello)
submenu.append(two)
one.set_submenu(submenu)
[/python]

2 Responses


  1. Pento on 16 Jul 2009

    Thanks for workaround! I really didn’t understand why this f*cked activate don’t work for submenu.

  2. Adam on 03 Nov 2009

    Gah! I was sitting here trying to figure out why my submenu items were not activating. How is this bug still around three years later? Thanks for posting this.


Leave your comment