tkinker menu code example

Example 1: how to make menu in tkinter

from tkinter import *

def do_some():
    print('Do some!')

root = Tk()

main_menu = Menu(root)
root.config(menu=main_menu)


# Type 1 Menu
main_menu.add_command(label='File', command=do_some)
# Type 1 Menu


# Type 2 Menu
file_menu = Menu(main_menu)
main_menu.add_cascade(label='File', menu=file_menu)

file_menu.add_command(label='Save', command=do_some)
file_menu.add_command(label='Save As', command=do_some)
# Type 2 Menu


# Type 3 Menu
file_menu = Menu(main_menu)
main_menu.add_cascade(label='File', menu=file_menu)

save_menu = Menu(file_menu)
save_as_menu = Menu(file_menu)

file_menu.add_cascade(label='Save', menu=save_menu)
file_menu.add_cascade(label='Save As', menu=save_as_menu)

save_menu.add_command(label='New', command=do_some)
save_menu.add_command(label='Old', command=do_some)
save_as_menu.add_command(label='New', command=do_some)
save_as_menu.add_command(label='Old', command=do_some)
# Type 3 Menu

root.mainloop()

Example 2: menù a tendina tkinter

pythonCopyimport tkinter as tk

OptionList = [
"Aries",
"Taurus",
"Gemini",
"Cancer"
] 

app = tk.Tk()

app.geometry('100x200')

variable = tk.StringVar(app)
variable.set(OptionList[0])

opt = tk.OptionMenu(app, variable, *OptionList)
opt.config(width=90, font=('Helvetica', 12))
opt.pack()

app.mainloop()