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 character, to allow for Alt-F-S
(File->Save)?
Many keyboards have a Windows key which will bring up the start menu. The keyboard combination Ctrl+Esc also brings up this menu. The up and down arrow keys allow you to move through the menu items. Those items with a submenu are visually indicated with a small black triangle/arrowhead.
Let us suppose we want to create a dropdown menu of a list in an application using tkinter. In this case, we can use the Tkinter OptionMenu(win, menu_to_set, options) function. First, we will instantiate an object of StringVar(), then we will set the initial value of the dropdown menu.
OptionMenu in Python Tkinter is used to create a drop-down menu in the application. It consumes less space and displays multiple options to the user. Users can select only one item out of the list of items.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With