Python 3 Tkinter Menu Bar
In this post, you will learn about the Python3 Tkinter menu bar.
Tkinter is a standard cross-platform package for creating graphical user interfaces (GUIs). It is also called the Tk interface. It is an original GUI library for Tcl (Tool Command Language). Tkinter comes pre-installed with Python.
Tkinter also provides a facility for menu designs. It provides a facility for multiple fonts, images, bitmaps, and radio buttons. It provides many options and methods to allow the user to choose operations within an application.
To create the menu, first we need to create an instance of the Menu widget, and then we can create the menu in several schemes.
Syntax of Tkinter menu
w = Menu(top, options)
These are the possible options-
Options | Descriptions |
---|---|
activebackground | The background color of the widget when the widget has the focus. |
activeborderwidth | The width of the border of the widget when it is under the mouse. Default is 1 pixel. |
activeforeground | The font color of the widget when the widget has the focus. |
bg | The background color of the widget. |
bd | The border width of the widget. |
cursor | The cursor type of mouse pointer. |
font | The font type of the text of the widget. |
fg | The foreground color of the widget. |
relief | The type of the border of the widget. The default type is RAISED. |
image | It is used to display an image on the menu. |
title | Set this option to the title of the window. |
Tkinter Menu Methods
Methods | Descriptions |
---|---|
add_command(options) | This method adds the Menu items to the menu. |
add_radiobutton(options) | This method adds the radiobutton to the menu. |
add_checkbutton(options) | This method is used to add the checkbuttons to the menu. |
add_cascade(options) | This method is used to create a hierarchical menu to the parent menu. |
add_seperator() | This method adds the separator line to the menu. |
add(type, options) | This method adds the specific menu item to the menu. |
delete(startindex, endindex) | It is used to delete the menu items exist in the specified range. |
entryconfig(index, options) | It is used to configure a menu item identified by the given index. |
index(item) | It is used to get the index of the specified menu item. |
insert_seperator(index) | It is used to insert a separator at the specified index. |
invoke(index) | It is used to invoke the associated with the choice given at the specified index. |
type(index) | It is used to get the type of the choice specified by the index. |
Tkinter menu bar
Here is a simple example of top-level menu that is created by instantiating the Menu widget and calling the add_cascade() method. This method is used to create a hierarchical menu to the parent menu by associating the given menu to the parent menu.
from tkinter import *
# creating tkinter window
root = Tk()
root.title('MENUS')
# Creating Menubar
menubar = Menu(root)
# Adding Home Menu
home = Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='Home', menu = home)
# Adding Share Menu
share = Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='Share', menu = share)
# Adding View Menu and commands
view = Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='View', menu = view)
# display Menu
root.config(menu = menubar)
mainloop()
The screenshot below is the output of the above command-

Tkinter sidebar menu
The menu bar can contain zero or more submenus. Using the add_command() method, we can create submenus.
from tkinter import *
# creating tkinter window
root = Tk()
root.title('Menu Bar')
# Creating Menubar
menubar = Menu(root)
# Adding File Menu
file = Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='File', menu = file)
file.add_command(label="New")
file.add_command(label="Open")
file.add_command(label="Save")
file.add_command(label="Save as...")
file.add_command(label="Close")
# Adding Edit Menu
edit = Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='Edit', menu = edit)
# display Menu
root.config(menu = menubar)
mainloop()

Tkinter Menu Checkbutton
In this given example, we have used the add_checkbutton() method. By using this, users can mark a check to apply the property of the menu.
from tkinter import *
# creating tkinter window
root = Tk()
root.title('Folder Properties')
# Creating Menubar
menubar = Menu(root)
# Adding Group By Menu
file = Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='Group By..', menu = file)
# Adding Checkbuttons
file.add_checkbutton(label="Name")
file.add_checkbutton(label="Date Modified")
file.add_checkbutton(label="Type")
file.add_checkbutton(label="Size")
# display Menu
root.config(menu = menubar)
mainloop()

Related Articles
Python Tkinter Combobox Event BindingPython Tkinter Combobox
Add background image in Python Tkinter
Python Tkinter Text Widget
Countdown clock and timer using Tkinter in Python
Python Tkinter Frame Widget
Python Tkinter Checkbutton Widget
Entry Field Validation in Tkinter Python
Python Tkinter Tutorial with Examples
Python Tkinter Scale Widget
Python3 Tkinter Messagebox
Python 3 Tkinter Menu Bar
Python Tkinter Geometry Managers
Python get visitor information by IP address
Python OpenCV ColorMap
Python gmplot to add google map on a web page
Simple Calculator Program in Python
Python get visitor information by IP address