[pygtk] popup menu questions

Stephen Langer stephen.langer at nist.gov
Thu Aug 23 04:03:54 WST 2007


Hi --

I have two questions about popup menus.  I've attached a small  
program that displays a TextView and brings up a popup menu when a  
mouse button is clicked on it.

Question 1: If I want to rebuild the Menu each time the button is  
clicked, and destroy it when I'm done with it, when am I supposed to  
call popupmenu.destroy()?  The FAQ says to do it in the 'deactivate'  
callback (http://faq.pygtk.org/index.py?req=show&file=faq11.002.htp),  
but if I do that, then the MenuItems are destroyed before their  
'activate' signals are emitted, which makes them more or less  
useless.  Is the FAQ wrong, or am I missing something?

Question 2: If I *don't* destroy the Menu, then keypresses (not mouse  
button presses) in the TreeView cause the 'deactivate' and 'cancel'  
signals to be emitted from the Menu.  This occurs on a Macbook Pro  
and a PowerBook G4 with pygtk 2.6.10, but not on Linux machines with  
pygtk 2.6.4 or 2.10.x.  Is this a Mac bug?  A pygtk 2.6 bug?  Or am I  
doing something wrong again?

Here's my test code.  Any insight will be appreciated.

Thanks.
  -- Steve


import pygtk
pygtk.require('2.0')
import gtk
import gobject

def deactivate(menu):
     print "deactivate"
     ## menu.destroy() # This line prevents the 'activate' callback  
from being called!
def cancel(menu):
     print "cancel"
def activate(menuitem, itemname):
     print "activate", itemname

def popup(button, event):
     #global popupmenu
     popupmenu = gtk.Menu()
     popupmenu.set_screen(window.get_screen())
     popupmenu.connect('deactivate', deactivate)
     popupmenu.connect('cancel', cancel)
     for itemname in ("one", "two", "three"):
         menuitem = gtk.MenuItem(itemname)
         menuitem.connect('activate', activate, itemname)
         popupmenu.append(menuitem)
     popupmenu.show_all()
     popupmenu.popup(None, None, None, event.button, event.time)
     return True

if __name__ == '__main__':
     global window
     window = gtk.Window(gtk.WINDOW_TOPLEVEL)
     window.connect('delete-event', gtk.main_quit)

     liststore = gtk.ListStore(gobject.TYPE_STRING)
     liststore.append(["hello"])
     liststore.append(["world"])
     treeview = gtk.TreeView(liststore)
     col = gtk.TreeViewColumn("header")
     cell = gtk.CellRendererText()
     col.pack_start(cell, True)
     col.add_attribute(cell, 'text', 0)
     treeview.append_column(col)

     window.add(treeview)
     treeview.connect('button-press-event', popup)
     window.show_all()
     gtk.main()



--
-- EMail: stephen.langer at nist.gov                    Phone: (301)  
975-5423 --
-- WWW:  http://math.nist.gov/mcsd/Staff/SLanger/    Fax:   (301)  
975-3553 --
-- Mail: NIST; 100 Bureau Drive -- Stop 8910; Gaithersburg, Md   
20899-8910 --

-- "I don't think this will work.  That's why it's  
science."               --
--                              Naomi Langer (age 6),  17 Feb  
2003         --




More information about the pygtk mailing list