简体   繁体   English

带键盘快捷键的Tkinter下拉菜单?

[英]Tkinter dropdown Menu with keyboard shortcuts?

I would like to have a Dropdown Menu in Tkinter, that includes the shortcut key associated with this command. 我想在Tkinter中有一个Dropdown Menu,其中包含与此命令关联的快捷键。 Is this possible? 这可能吗?

How would I also add the underline under a certain character, to allow for Alt-FS (File->Save)? 如何在特定字符下添加下划线,以允许Alt-FS (文件 - >保存)?

import tkinter as tk
import sys

class App(tk.Tk):

    def __init__(self):
        tk.Tk.__init__(self)
        menubar = tk.Menu(self)
        fileMenu = tk.Menu(menubar, tearoff=False)
        menubar.add_cascade(label="File", underline=0, menu=fileMenu)
        fileMenu.add_command(label="Exit", underline=1,
                             command=quit, accelerator="Ctrl+Q")
        self.config(menu=menubar)

        self.bind_all("<Control-q>", self.quit)

    def quit(self, event):
        print("quitting...")
        sys.exit(0)

if __name__ == "__main__":
    app = App()
    app.mainloop()

Maybe 也许

from tkinter import *
import tkinter.filedialog as filed

root = Tk()
root.title("My Python Tkinter Application")
root.minsize(800,600)

def openfile():
    fn = filed.askopenfilename(filetypes=[("Text Files","*.txt")], title="Open File")
    f = open(fn, "r").read()
    print(f)

def init():
    menu = Menu(root)
    filemenu = Menu(menu)
    filemenu.add_command(label="Open (⌘O)", command=openfile)
    menu.add_cascade(label="File", menu=filemenu)
    root.config(menu=menu)
def key():
    print("Key Pressed: "+repr(event.char))
root.bind("<Key>", key)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM