Kadjo

SoftDev Journal

About This Journal

24 June 2007 by Jojo Makiling

Technical concerns tend to find a solution as long as there are good people working on them. And Linux has the very best. - Linus Torvalds

PyGTK Context Menu Problem

December 30th, 2006 by Administrator

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
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()

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)
submenu.append(two)
one.set_submenu(submenu)

Posted in Python | 2 Comments »