I would like to have a Dropdown Menu in Tkinter, that includes the shortcut key associated with this command. Is this possible?
我想在Tkinter中有一个Dropdown Menu,其中包含与此命令关联的快捷键。这可能吗?
How would I also add the underline under a certain character, to allow for Alt-F-S
(File->Save)?
我如何在特定字符下添加下划线,以允许Alt-F-S(文件 - >保存)?
2 个解决方案
#1
36
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()
#2
-1
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)
#1
36
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()
#2
-1
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)