tkinter应用程序添加右键单击上下文菜单? [英] tkinter app adding a right click context menu?
问题描述
我有一个python-tkinter gui应用程序,我一直在努力寻找添加一些功能的方法。我希望有一种方法可以右键单击该应用程序的列表框区域中的某个项目,并弹出一个上下文菜单。 tkinter能够做到这一点吗?我最好去研究gtk或其他GUI工具包吗?
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?
推荐答案
您将创建一个 Menu 实例和编写一个调用
其 post()$ 的函数c $ c>
或 tk_popup( )
方法。
You would create a Menu instance and write a function that calls
its post()
or tk_popup()
method.
tkinter文档当前没有有关 tk_popup()
的任何信息。
阅读 Tk文档以获得描述或来源:
The tkinter documentation doesn't currently have any information about tk_popup()
.
Read the Tk documentation for a description, or the source:
library / menu.tcl
在Tcl / Tk源代码中:
library/menu.tcl
in 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 __。py
在Python源代码中:
tkinter/__init__.py
in the Python source:
def tk_popup(self, x, y, entry=""):
"""Post the menu at position X,Y with entry ENTRY."""
self.tk.call('tk_popup', self._w, x, y, entry)
您关联右键单击可通过以下菜单调用上下文菜单功能:
the_widget_clicked_on.bind(< Button-3>,your_function)
。
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.
< Tcl / Tk源代码中的href = http://core.tcl.tk/tk/artifact/4ff6154cc3cca965 rel = noreferrer> library / tk.tcl
:
On Darwin/Aqua, buttons from left to right are 1,3,2.
On Darwin/X11 with recent XQuartz as the X server, they are 1,2,3;
other X servers may differ.
下面是我写的一个向列表框添加上下文菜单的示例:
Here is an example I wrote that adds a context menu to a Listbox:
import tkinter # Tkinter -> tkinter in Python 3
class FancyListbox(tkinter.Listbox):
def __init__(self, parent, *args, **kwargs):
tkinter.Listbox.__init__(self, parent, *args, **kwargs)
self.popup_menu = tkinter.Menu(self, tearoff=0)
self.popup_menu.add_command(label="Delete",
command=self.delete_selected)
self.popup_menu.add_command(label="Select All",
command=self.select_all)
self.bind("<Button-3>", self.popup) # Button-2 on Aqua
def popup(self, event):
try:
self.popup_menu.tk_popup(event.x_root, event.y_root, 0)
finally:
self.popup_menu.grab_release()
def delete_selected(self):
for i in self.curselection()[::-1]:
self.delete(i)
def select_all(self):
self.selection_set(0, 'end')
root = tkinter.Tk()
flb = FancyListbox(root, selectmode='multiple')
for n in range(10):
flb.insert('end', n)
flb.pack()
root.mainloop()
在
其效果可能在所有系统上都不相同。
The use of grab_release()
was observed in an example on effbot.
Its effect might not be the same on all systems.
这篇关于tkinter应用程序添加右键单击上下文菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!