简体   繁体   中英

GTK popup menu not appearing at mouse position

I'm trying to create a Gtk popup menu on button click using a handler as seen 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 in popup, func, sets the position to the cursor position if set to Null. I think the problem is there since if I set func to lambda x,y: (event.x,event.y,True) , it shows the popup menu some 100 pixels above my cursor.

I'd like to find some way to popup this menu at my cursor. Any help would be appreciated!

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 at correct position. Works here.

As to answer of Jon Black, in GTK3, the question is correct, see http://developer.gnome.org/gtk3/3.4/GtkMenu.html#gtk-menu-popup The deprecate pygtk bindings only work like you describe

You're passing event.time as the data parameter, which is in turn passed to func to determine the menu position. Changing your call to the following should fix it:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM