1

我正在使用 TKinter Menu Widet,并且我想将其他键盘键绑定到已经绑定到其他键的系统事件。我的意思是当执行下面的代码片段时,会显示一个简单的菜单。当我按下“Key-F10”时,“文件”标签会突出显示,当我按下任何“向上”或“向下”箭头时,菜单就会展开(张贴)。这至少是 Windows 8 操作系统上的行为。

我希望能够使用“Space”键扩展菜单,一旦它用“”突出显示<Key-F10>,使用“Up”“Down”键的功能回调 - 有点将“Space”搭载到相同的绑定。但是我无法找到如何做到这一点。我的代码如下所示。

from Tkinter import *
from tkFileDialog import askopenfilename


def popup(event):
   filemenu.post(filemenu.winfo_rootx(), filemenu.winfo_rooty())


root = Tk()
menu = Menu(root)
root.config(menu=menu)
filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New")
filemenu.add_command(label="Open...")
filemenu.add_separator()
filemenu.add_command(label="Exit")

root.bind('<Button-3>', popup)
mainloop()

我能够自己找到的打印root.bind_all('<Key-F10>')内容是:tk::WinMenuKey %W %N这导致我在这里定义了以下内容:

 *--------------------------------------------------------------
 *
 * TkWinMenuKeyObjCmd --
 *
 *  This function is invoked when keys related to pulling down menus is
 *  pressed. The corresponding Windows events are generated and passed to
 *  DefWindowProc if appropriate. This cmd is registered as tk::WinMenuKey
 *  in the interp.
 *
 * Results:
 *  Always returns TCL_OK.
 *
 * Side effects:
 *  The menu system may take over and process user events for menu input.
 *
 *--------------------------------------------------------------

现在我可以如何做,例如root.bind('<Key-Up>', tk::WinMenuKey)将 Up 键绑定到同一个事件以拉动菜单。从我所看到的这些事件不经过Tkinter.Menu.post/tk_popup方法。

作为替代方案,我尝试在我为 Muse 按钮​​ 3 注册的回调中使用 Tkinter.Menu.post ,使用filemenu.winfo_rootx()x filemenu.winfo_rooty(),y 坐标,但是菜单始终在屏幕的左上角展开,如屏幕截图所示。当我使用filemenu.winfo_x(),时,行为是相同的filemenu.winfo_y()

左上角弹出 Tkinter 文件菜单

4

0 回答 0