Tkinter MenuButton

Tkinter MenuButton is a combination of both the button and menu widget, used to provide the user an option to select the appropriate choice that exists and implement various types of menu within the application. It can be defined as the drop-down menu that is shown to the user.

Syntax:

The Parameter master represents the parent window and options can be used as key-value pairs separated by commas.

Options of Tkinker MenuButton:

bd: Shows the size of the border around the indicator. By default is 2 pixels.

bg: Background color for the area around the widget.

fg: Color for the text.

image: Used to display an image.

state: By default is value is NORMAL. DISABLED causes the Widget to gray out and go inactive.

text: The text displayed next on the Menu Button.

activeforeground: Shows the foreground color when the mouse is over the menubutton in GUI.

activebackground: Shows the background color.

Width: Shows the width of the widget in number of characters.

pady: Shows the amount of padding in terms of pixels for the area of the widget.

padx: Shows the amount of padding in terms of pixels in the left-right directions.

menu: This option used to assign a menu object which allows for a list of options to be displayed to the menu button.

justify: This option specifies alignment of the text in the widget.

underline: Shows that text of the widget is not underlined by default but can set this option to make the text of the widget underlined.

wraplength: By setting this option to a number of characters and all lines will be broken into pieces no longer than that number.

cursor: It can be changed to a special cursor type, when the mouse is hovering over this widget.

anchor: It controls where the text is positioned if the widget has more space than the text needs.

disabledforeground: Shows the foreground color shown on the menubutton when it’s disabled.

Example:
from tkinter import *

appWindow = Tk()
appWindow.title("Window Title-Tutorialsart.com")

appWindow.geometry('350x200')

menubutton = Menubutton(appWindow, text="Country", relief=FLAT, bg="orange")

menubutton.grid()

menubutton.menu = Menu(menubutton)

menubutton["menu"] = menubutton.menu

menubutton.menu.add_checkbutton(label="America", variable=IntVar())

menubutton.menu.add_checkbutton(label="England", variable=IntVar())

menubutton.pack()


appWindow.mainloop()
Output:
11 Tkinter MenuButton

Comments are closed.