Derek Redfern
Derek Redfern

Reputation: 1009

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!

Upvotes: 3

Views: 2782

Answers (2)

Benn Malengier
Benn Malengier

Reputation: 2977

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

Upvotes: 4

Jon
Jon

Reputation: 10473

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)

Upvotes: 1

Related Questions