GTK popup menu not showing at mouse position

I am trying to create a Gtk popup menu when a button is clicked using a handler, as shown below:

def code_clicked(self,widget,event):
    newmenu=Gtk.Menu()
    newitem=Gtk.MenuItem('hello')
    newmenu.append(newitem)
    newitem1=Gtk.MenuItem('goodbye')
    newmenu.append(newitem1)
    newmenu.show_all()
    newmenu.popup(None,None,None,None,event.button,event.time)
    return True

The menu never appears. Theoretically, the third argument to popup, func sets the position of the cursor position if set to Null. I think the problem is that if I set func to lambda x,y: (event.x,event.y,True), it displays a popup menu 100 pixels above my cursor.

I would like to find a way to open this menu under my cursor. Any help would be appreciated!

+3
source share
2 answers

You need the root position of the window. So

        menu.popup(None, None, 
                   lambda menu, data: (event.get_root_coords()[0],
                                       event.get_root_coords()[1], True),
                   None, event.button, event.time)

should put the popup in the correct position. It works here.

, GTK3, , . http://developer.gnome.org/gtk3/3.4/GtkMenu.html#gtk-menu-popup pygtk ,

+4

event.time , , , func . :

newmenu.popup(None, None, None, event.button, event.time)
+1

All Articles