📜  python代码示例中的菜单

📅  最后修改于: 2022-03-11 14:46:20.751000             🧑  作者: Mango

代码示例1
from tkinter import (
    Tk,
    Label,
    Menu,
    messagebox,
)


# some example functions
def Res():
    root.geometry("333x222")


def B2N():
    root.geometry("444x444")


def Full():
    root.geometry("1000x700")


def custom_quit():
    answer = messagebox.askokcancel(
        "Are you Sure?", "Your Data will be lost if you exit"
    )
    if answer:
        quit()


# the main window
root = Tk()
root.geometry("444x444")
text = Label(root, text="*my first menu*").pack()
# the actual menu (used a big example for better understanding of menu and sub-menu)
menu1 = Menu(root)
root.configure(menu=menu1)

submenu1 = Menu(menu1)
menu1.add_cascade(label="File", menu=submenu1)
sub2menu = Menu(submenu1)
sub2menu.add_command(label="Recent")
sub2menu.add_command(label="Browse")

submenu1.add_command(label="New File")
submenu1.add_cascade(label="Open File", menu=sub2menu)
submenu1.add_command(label="Save As")
submenu1.add_command(label="Print")
submenu1.add_command(
    label="Quit", command=custom_quit
)  # adding command to menu elements

submenu2 = Menu(menu1)
menu1.add_cascade(label="Edit", menu=submenu2)
submenu2.add_command(label="Undo")
submenu2.add_cascade(label="Redo")
submenu2.add_command(label="Cut")
submenu2.add_command(label="Copy")
submenu2.add_command(label="Paste")
submenu2.add_command(label="Select All")

submenu3 = Menu(menu1)
menu1.add_cascade(label="Format", menu=submenu3)
submenu3.add_command(label="Indent Region")
submenu3.add_cascade(label="Comment")
submenu3.add_command(label="All")
submenu3.add_command(label="Selected")
submenu3.add_command(label="Format Paragraph")

submenu4 = Menu(menu1)
menu1.add_cascade(label="Run", menu=submenu4)
submenu4.add_command(label="Python Shell")
submenu4.add_cascade(label="Run Module")
submenu4.add_command(label="Check Module")

submenu5 = Menu(menu1)
menu1.add_cascade(label="Options", menu=submenu5)
submenu5.add_command(label="Configure IDLE")
submenu5.add_cascade(label="Show Code Context")
submenu5.add_command(label="Zoom Height")

submenu6 = Menu(menu1)
menu1.add_cascade(label="Window", menu=submenu6)
submenu6.add_command(label="Python 3.7.3 shell")
submenu6.add_cascade(label="Untitled")
submenu6.add_command(label="Resize window", command=Res)
submenu6.add_command(label="Norml size window", command=B2N)
submenu6.add_command(label="Large window", command=Full)

submenu7 = Menu(menu1)
menu1.add_cascade(label="Help", menu=submenu7)
submenu7.add_command(label="About IDLE")
submenu7.add_cascade(label="IDLE Help")
submenu7.add_command(label="FAQ")
submenu7.add_command(label="Contact US")

root.mainloop()