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. Is this possible? How would I also add the underline under a certain c
Solution 1:
import tkinter as tk
import sys
classApp(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)
defquit(self, event):
print("quitting...")
sys.exit(0)
if __name__ == "__main__":
app = App()
app.mainloop()
Solution 2:
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)
definit():
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)
defkey():
print("Key Pressed: "+repr(event.char))
root.bind("<Key>", key)
Solution 3:
In this code press ctrl+e to exit the programmme
from tkinter import *
classMainApp:
def__init__(self, root):
self.root = root
self.menubar = Menu(self.root)
self.fileMenu = Menu(self.menubar, tearoff=False)
self.menubar.add_cascade(label="File", menu=self.fileMenu)
self.fileMenu.add_command(label="Exit", accelerator="Ctrl+E")
self.root.config(menu=self.menubar)
self.root.bind_all("<Control-e>", lambda event: self.root.quit())
self.root.mainloop()
if __name__ == "__main__":
MainApp(Tk())
Post a Comment for "Tkinter Dropdown Menu With Keyboard Shortcuts?"