I have a python-tkinter gui app that I’ve been trying to find some way to add in some functionality. I was hoping there would be a way to right-click on an item in the app’s listbox area and bring up a context menu. Is tkinter able to accomplish this? Would I be better off looking into gtk or some other gui-toolkit?
Share
You would create a Menu instance and write a function that calls
its
post()ortk_popup()method.The tkinter documentation doesn’t currently have any information about
tk_popup().Read the Tk documentation for a description, or the source:
library/menu.tclin the Tcl/Tk source:::tk_popup -- This procedure pops up a menu and sets things up for traversing the menu and its submenus. Arguments: menu - Name of the menu to be popped up. x, y - Root coordinates at which to pop up the menu. entry - Index of a menu entry to center over (x,y). If omitted or specified as {}, then menu's upper-left corner goes at (x,y).tkinter/__init__.pyin the Python source:You associate your context menu invoking function with right-click via:
the_widget_clicked_on.bind("<Button-3>", your_function).However, the number associated with right-click is not the same on every platform.
library/tk.tclin the Tcl/Tk source:Here is an example I wrote that adds a context menu to a Listbox:
The use of
grab_release()was observed in an example on effbot.Its effect might not be the same on all systems.