Sunday, November 17, 2024
Google search engine
HomeLanguagesPython | Menu widget in Tkinter

Python | Menu widget in Tkinter

Tkinter is Python’s standard GUI (Graphical User Interface) package. It is one of the most commonly used package for GUI applications which comes with the Python itself.

Menus are the important part of any GUI. A common use of menus is to provide convenient access to various operations such as saving or opening a file, quitting a program, or manipulating data. Toplevel menus are displayed just under the title bar of the root or any other toplevel windows.

menu = Menu(master, **options)

 
Below is the implementation:




# importing only  those functions 
# which are needed
from tkinter import * 
from tkinter.ttk import * 
from time import strftime
  
# creating tkinter window
root = Tk()
root.title('Menu Demonstration')
  
# Creating Menubar
menubar = Menu(root)
  
# Adding File Menu and commands
file = Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='File', menu = file)
file.add_command(label ='New File', command = None)
file.add_command(label ='Open...', command = None)
file.add_command(label ='Save', command = None)
file.add_separator()
file.add_command(label ='Exit', command = root.destroy)
  
# Adding Edit Menu and commands
edit = Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='Edit', menu = edit)
edit.add_command(label ='Cut', command = None)
edit.add_command(label ='Copy', command = None)
edit.add_command(label ='Paste', command = None)
edit.add_command(label ='Select All', command = None)
edit.add_separator()
edit.add_command(label ='Find...', command = None)
edit.add_command(label ='Find again', command = None)
  
# Adding Help Menu
help_ = Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='Help', menu = help_)
help_.add_command(label ='Tk Help', command = None)
help_.add_command(label ='Demo', command = None)
help_.add_separator()
help_.add_command(label ='About Tk', command = None)
  
# display Menu
root.config(menu = menubar)
mainloop()


Output:


Note: In above application, commands are set to None but one may add different commands to different labels to perform the required task.

RELATED ARTICLES

Most Popular

Recent Comments