26
loading...
This website collects cookies to deliver better user experience
The top-level menus are the one which is displayed just under the title bar of the parent window.
menubar = Menu(master)
menu()
constructor. That is, file = Menu(menubar, tearoff=0)
. This creates a button named file (we add txt later) on the top of the screen. We do that for every button we want to place on the top menu. Example here we add two buttons, file and edit.add_comand()
method. These are displayed in the dropdown. Example file.add_command(label="New")
We can also use the command file.add_separator()
to add a horizontal line for separation menubar.add_cascade(label="File", menu=file)
to set the txt of the button and place it in the main menu.from tkinter import *
master = Tk()
menubar = Menu(master)
file = Menu(menubar, tearoff=0)
file.add_command(label="New")
file.add_command(label="Open")
file.add_command(label="Save")
file.add_command(label="Save as")
file.add_separator()
file.add_command(label="Exit")
menubar.add_cascade(label="File", menu=file)
edit = Menu(menubar, tearoff=0)
edit.add_command(label="Undo")
edit.add_separator()
edit.add_command(label="Cut")
edit.add_command(label="Copy")
edit.add_command(label="Paste")
edit.add_command(label="Delete")
edit.add_command(label="Select All")
menubar.add_cascade(label="Edit", menu=edit)
master.config(menu=menubar)
master.mainloop()
In the above program, clicking on the buttons will not run any actions, as we have not placed any command on clicking.