PyGTK Context Menu Problem

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:

import pygtk
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”)

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

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


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.connect(‘button-release-event’, self.contextMenu)

def main(self):

if __name__ == “__main__”:
a = bugtest()

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

submenu = gtk.Menu()
two = gtk.MenuItem(“Two”)
two.connect(“button-press-event”, self.hello)

2 thoughts on “PyGTK Context Menu Problem

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

  2. 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 a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>