tkinter_menus
Table of Contents
tkinter menus
Menus
Really quick, lets look at menus, then go on to the quiz show.
import tkinter as tk
def donothing():
print("Button pressed.")
def main():
# Create the main window
window = tk.Tk()
window.title("Appledog Menu Program v1.0")
window.geometry("300x200")
# A) Make the menu bar.
menu_bar = tk.Menu(window)
# B) Make menu commands (called 'Go!')
menu1 = tk.Menu(menu_bar, tearoff=0)
menu1.add_command(label="Go!", command=donothing)
# C) Add a command to the menu bar,
menu_bar.add_cascade(label="Stuff", menu=menu1)
# D) Attach menu bar to the window.
window.config(menu=menu_bar)
window.mainloop()
if __name__ == '__main__':
main()
More Advanced
import tkinter as tk
def donothing():
print("Button pressed.")
def main():
# Create the main window
window = tk.Tk()
window.title("Appledog Menu Program v1.0")
window.geometry("300x200")
# Create the menu bar
menu_bar = tk.Menu(window)
# Create the menus
menu_1 = tk.Menu(menu_bar, tearoff=0)
menu_2 = tk.Menu(menu_bar, tearoff=0)
# Add items for Menu1
menu_1.add_command(label="Item1", command=donothing)
menu_1.add_command(label="Item2", command=donothing)
menu_1.add_separator()
menu_1.add_command(label="Item3", command=donothing)
# Add items for Menu2
menu_2.add_command(label="Item4", command=donothing)
menu_2.add_command(label="Item5", command=donothing)
# Add the menus to the menu bar
menu_bar.add_cascade(label="Menu1", menu=menu_1)
menu_bar.add_cascade(label="Menu2", menu=menu_2)
# Attach the menu bar to the main window
window.config(menu=menu_bar)
# Start the Tkinter event loop
window.mainloop()
if __name__ == '__main__':
main()
tkinter_menus.txt · Last modified: 2024/04/24 09:04 by appledog
