tkinter 应用程序添加右键单击上下文菜单? [英] tkinter app adding a right click context menu?
问题描述
我有一个 python-tkinter gui 应用程序,我一直试图找到一些方法来添加一些功能.我希望有一种方法可以右键单击应用程序列表框区域中的项目并调出上下文菜单.tkinter 能够做到这一点吗?我最好研究一下 gtk 或其他一些 gui-toolkit 吗?
您将创建一个 Menu 实例并编写一个调用
的函数它的 post()
或 tk_popup()
方法.
tkinter 文档 目前没有关于 的任何信息tk_popup()
.
阅读 Tk 文档以获取说明或来源:
library/menu.tcl
在 Tcl/Tk 源代码中一个>:
tkinter/__init__.py
在 Python 源代码中:
def tk_popup(self, x, y, entry=""):"""将菜单张贴在位置 X,Y 并输入 ENTRY."""self.tk.call('tk_popup', self._w, x, y, entry)
您可以通过以下方式将上下文菜单调用功能与右键单击相关联:the_widget_clicked_on.bind("
.
但是,与右键单击相关的数字在每个平台上都不相同.
library/tk.tcl
在 Tcl/Tk 源代码中一个>:
这是我写的一个向列表框添加上下文菜单的示例:
import tkinter # Tkinter ->Python 3 中的 tkinter类 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="删除",命令=self.delete_selected)self.popup_menu.add_command(label="全选",命令=self.select_all)self.bind("", self.popup) # Button-2 on Aqua定义弹出(自我,事件):尝试:self.popup_menu.tk_popup(event.x_root, event.y_root, 0)最后:self.popup_menu.grab_release()def delete_selected(self):对于 self.curselection()[::-1] 中的 i:self.delete(i)def select_all(self):self.selection_set(0, 'end')根 = tkinter.Tk()flb = FancyListbox(root, selectmode='multiple')对于范围内的 n(10):flb.insert('end', n)flb.pack()root.mainloop()
在一个例子中观察到了grab_release()
的使用在 effbot 上.
它对所有系统的影响可能并不相同.
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?
You would create a Menu instance and write a function that calls
its post()
or tk_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.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
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)
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.tcl
in the Tcl/Tk source:
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屋!