| FAQ Index - Search - Recent Changes - Everything - Add entry |
11.2. How do I pop up a menu with a button click?
You need to attach a callback to "button_press_event" and catch its event object (which is the second parameter). This handler should be something like:
def popup(widget, event): popup_menu.popup(None, None, None, event.button, event.time)this results in a menu that is floating and you have to call popup_menu.destroy() or reuse the popup_menu.
You are also advised to call popup_menu.attach_to_widget() or at least .set_screen() so your application does not suffer in multiple screens environments.
Callling attach_to_widget() also means that your menu will be destroyed when widget that is being attached to, is destroyed. Popdown that happens by GTK does NOT destroy the menu, so if you want this behaviour, connect to 'deactivate' signal and in callback do widget.destroy()
Don't ask me what the None's are supposed to be. The API dox say:
GtkWidget *parent_menu_shell, GtkWidget *parent_menu_item, GtkMenuPositionFunc func, gpointer data, guint button, guint32 activate_timeBut gtk.py does:
def popup(self, pms, pmi, func, button, time)So data is probably gone. Everybody uses None, why not you?
