Answer a question

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)?

Answers

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()
Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐