Table of Contents
Menu and Menubutton Widget of tkinter in Python
Tkinter provides many widgets for GUI in Python.
Menu and Menubutton widgets are used to give drop down menu from which user can select one according to the need.
Menus in GUIs are presented with a combination of text and symbols to represent the choices.
Every menubutton is associated with a Menu widget that can display the choices for that menubutton when the user clicks on it.
Menu widget
Toplevel menus are displayed just under the title bar of the root or any other toplevel windows.
We need to create a new instance of the Menu widget and add various commands to it by using the add() method.
Syntax
Here is the simple syntax to create this widget −
w = Menu( master, option=value, ... )
Parameters
master − This represents the parent window.
options − Here is the list of most commonly used options for this widget. These options can be used as key-value pairs separated by commas.
Various options of Menu widget:
- activebackground : Background color when the button is under the cursor.
- activeforeground : Foreground color when the button is under the cursor.
- tearoff : By default, the choices in the menu start taking place from position 1. If we set the tearoff = 0, then it will start taking place from 0th position.
- bg : The normal background color displayed behind the label and indicator.
- bd : The size of the border around the indicator. Default is 2 pixels.
- title : Set this option to the title of the window if you want to change the title of the window.
- bitmap : Set this option equal to a bitmap or image object and the label will display that graphic.
- fg : This option specifies the color of the text. If you are displaying a bitmap, this is the color that will appear at the position of the 1-bits in the bitmap.
- height : The vertical dimension of the new frame.
- image : To display a static image in the label widget, set this option to an image object.
- text : To display one or more lines of text in a label widget, set this option to a string containing the text. Internal newlines (“n”) will force a line break.
- underline : You can display an underline (_) below the nth letter of the text, counting from 0, by setting this option to n. The default is underline=-1, which means no underlining.
- width: Width of the label in characters (not pixels!). If this option is not set, the label will be sized to fit its contents.
- relief : Specifies the appearance of a decorative border around the label. The default is FLAT; for other values.
- disabledforeground : The text color of the widget when the widget is disabled.
Various methods of Menu widget:
- add_command() : It is used to add a menu item to the menu
- add_separator() : It used to add separator line in between menus
- add_cascade() creates a hierarchical menu by associating with parent menu
- add_radiobutton() : It creates radiobutton menu item.
- add_checkbutton() : It creates checkbutton menu item.
Example
""" Author : ITVoyagers (itvoyagers.in) Date :24th March 2020 Description : Program to show use of Menu widget """ from tkinter import * def donothing(): top = Toplevel(root) button = Button(top, text="Do nothing button") button.pack() root = Tk() menubar = Menu(root) """tearoff : In menu first position in the list of choices is occupied by the tear-off element and the addictional choices are added at the starting from position 1. If tearoff = 0 the menu will not have tear off feature and choices will be added starting at position 0""" filemenu = Menu(menubar, tearoff=0) #add_command() is used to add a menu item to the menu filemenu.add_command(label="New", command=donothing) filemenu.add_command(label="Open", command=donothing) filemenu.add_command(label="Save", command=donothing) filemenu.add_command(label="Save as...", command=donothing) filemenu.add_command(label="Close", command=donothing) filemenu.add_separator()#used to add separator in between menus filemenu.add_command(label="Exit", command=root.quit) #add_cascade() creates a hierarchical menu by assoociating with parent menu menubar.add_cascade(label="File", menu=filemenu) editmenu = Menu(menubar, tearoff=0) editmenu.add_command(label="Undo", command=donothing) editmenu.add_separator() editmenu.add_command(label="Cut", command=donothing) editmenu.add_command(label="Copy", command=donothing) editmenu.add_command(label="Paste", command=donothing) editmenu.add_command(label="Delete", command=donothing) editmenu.add_command(label="Select All", command=donothing) menubar.add_cascade(label="Edit", menu=editmenu) helpmenu = Menu(menubar, tearoff=0) helpmenu.add_command(label="Help Index", command=donothing) helpmenu.add_command(label="About...", command=donothing) menubar.add_cascade(label="Help", menu=helpmenu) root.config(menu=menubar) root.mainloop()
OUTPUT

Menubutton widget
Syntax
Here is the simple syntax to create this widget −
w = Menubutton( master, option=value, ... )
Parameters
master − This represents the parent window.
options − Here is the list of most commonly used options for this widget. These options can be used as key-value pairs separated by commas.
Various options of Menubutton widget:
- activebackground : Background color when the button is under the cursor.
- activeforeground : Foreground color when the button is under the cursor.
- anchor: This option controls where the text is positioned. The default anchor is CENTER.Other possible values are NW(North West is top left), N, NE(North East is top right), W, E, SW(South West is bottom left), S, SE(South East is bottom right)
- bg : The normal background color displayed behind the label and indicator.
- bd : The size of the border around the indicator. Default is 2 pixels.
- menu : It represents the menu specified with the Menubutton.
- bitmap : Set this option equal to a bitmap or image object and the label will display that graphic.
- direction : It direction can be specified so that menu can be displayed to the specified direction of the button. Use LEFT, RIGHT, or ABOVE to place the widget accordingly.
- fg : If you are displaying text or a bitmap in this label, this option specifies the color of the text. If you are displaying a bitmap, this is the color that will appear at the position of the 1-bits in the bitmap.
- height : The vertical dimension of the new frame.
- image : To display a static image in the label widget, set this option to an image object.
- text : To display one or more lines of text in a label widget, set this option to a string containing the text. Internal newlines (“n”) will force a line break.
- underline : You can display an underline (_) below the nth letter of the text, counting from 0, by setting this option to n. The default is underline=-1, which means no underlining.
- width: Width of the label in characters (not pixels!). If this option is not set, the label will be sized to fit its contents.
- relief : Specifies the appearance of a decorative border around the label. The default is FLAT; for other values.
- disabledforeground : The text color of the widget when the widget is disabled.
Example
""" Author : ITVoyagers (itvoyagers.in) Date :24th March 2020 Description : Program to show use of Menubutton widget """ from tkinter import * itv = Tk() itv.geometry("200x250") menubutton = Menubutton(itv, text = "Programming Language", relief = RAISED) menubutton.grid() menubutton.menu = Menu(menubutton) menubutton["menu"]=menubutton.menu menubutton.menu.add_checkbutton(label = "Python", variable=IntVar()) menubutton.menu.add_checkbutton(label = "Java", variable = IntVar()) menubutton.menu.add_checkbutton(label = "C++", variable = IntVar()) menubutton.pack()
OUTPUT

You can also check following posts to know more about tkinter and GUI
- What is tkinter?
- Layout / Geometry Manager in Python
- GUI programming in Python and Python GUI Library
- Events and Bindings in Python along with Widget configuration and styling
- Fonts Names, Font Descriptors, System Fonts, Text formatting, Borders, Relief Styles in Python
- Canvas widget of tkinter module – Python
- Widgets in tkinter module – Python
- Label, Text, Entry & Message Widget in Python
- Best post on Button, Checkbutton & Radiobutton Widget in Python
- Best post- Listbox and Scrollbar(Slider) Widget-Python
- Best post on Frame Widget of tkinter in Python
- Best post on Spinbox and Scale Widget in Python
- Best post- LabelFrame, Toplevel, PanedWindow widgets
- Best post: message box widget and its methods
- Best post:Dimensions, Anchors, Bitmaps & Cursors in Python