Python 3 – Tkinter Menubutton
Tkinter is a powerful library in Python for creating graphical user interfaces. The Menubutton widget in Tkinter is an option menu that allows the user to select a value from a predefined set of options. It is a graphical representation of a drop-down list.
Syntax
tk.Menubutton(master, option=value, ...)
Parameters
master
: This parameter is used to represent the parent window in which the Menubutton is to be displayed.-
option
: There are several options that can be used with the Menubutton. These include:
Option | Description |
---|---|
activebackground |
This option specifies the background color of the menu when it is active. |
background |
This option specifies the background color of the Menubutton widget. |
bitmap |
This option specifies a bitmap that is displayed in the Menubutton widget. |
default |
This option specifies the default value of the Menubutton widget. |
direction |
This option specifies the direction in which the menu is opened. |
disabledforeground |
This option specifies the foreground color of the widget when it is disabled. |
font |
This option specifies the font used for displaying text in the Menubutton widget. |
foreground |
This option specifies the foreground color of the Menubutton widget. |
height |
This option specifies the height of the Menubutton widget. |
highlightbackground |
This option specifies the color of the highlight border when the Menubutton widget does not have focus. |
highlightcolor |
This option specifies the color of the highlight border when the Menubutton widget has focus. |
highlightthickness |
This option specifies the thickness of the highlight border. |
indicatoron |
This option specifies whether an indicator is displayed to the left of the selected option. |
menu |
This option specifies the menu that is displayed when the Menubutton widget is pressed. |
padx |
This option specifies the horizontal padding of the Menubutton widget. |
pady |
This option specifies the vertical padding of the Menubutton widget. |
relief |
This option specifies the border style of the Menubutton widget. |
state |
This option specifies the state of the Menubutton widget. |
takefocus |
This option specifies whether the Menubutton widget can be focused. |
text |
This option specifies the text displayed in the Menubutton widget. |
textvariable |
This option specifies a Tkinter variable used to hold the selected value. |
underline |
This option specifies which letter of the text should be underlined. |
width |
This option specifies the width of the Menubutton widget. |
Creating a Menubutton
Creating a Menubutton in Tkinter requires two steps: creating the menu options and then creating the Menubutton widget itself.
Creating menu options
To create a set of menu options, a Menu
widget is used. It is initialized using the syntax:
tk.Menu(master, option=value, ...)
The master
parameter is used to specify the parent window. The option
parameter can be used to set various properties of the menu. The available options are:
Option | Description |
---|---|
activebackground |
This option specifies the background color of the menu option when it is active. |
activeborderwidth |
This option specifies the width of the menu option border when it is active. |
activeforeground |
This option specifies the foreground color of the menu option when it is active. |
background |
This option specifies the background color of the menu option. |
bitmap |
This option specifies a bitmap that is displayed in the menu option. |
font |
This option specifies the font used for displaying text in the menu option. |
foreground |
This option specifies the foreground color of the menu option. |
hidemargin |
This option specifies whether the margins around the menu option are hidden. |
image |
This option specifies an image that is displayed in the menu option. |
label |
This option specifies the text displayed in the menu option. |
menu |
This option specifies a submenu that is displayed when the menu option is selected. |
state |
This option specifies the state of the menu option. |
underline |
This option specifies which letter of the text should be underlined. |
Once the Menu
widget is created, the options can be added to it using the add_command
method. The syntax is:
menu.add_command(label="Option 1", command=my_function)
The label
parameter specifies the text that will be displayed in the menu option. The command
parameter specifies the function that will be called when the menu option is selected.
Creating a Menubutton
After creating the menu options, a Menubutton widget can be created using the syntax:
menubutton = tk.Menubutton(root, text="Select an option", relief="raised")
The root
parameter specifies the parent window. The text
parameter specifies the text that will be displayed in the Menubutton widget. The relief
parameter specifies the border style of the widget.
To add the menu options to the Menubutton widget, the menu
property should be set using the config
method:
menubutton["menu"] = menu
This sets the menu
property of the Menubutton widget to the Menu
widget created earlier.
The Menubutton widget can be displayed using the pack
or grid
methods. For example:
menubutton.pack()
Example
Let’s create a simple example that creates a Menubutton with two options: “Option 1” and “Option 2”.
import tkinter as tk
def select_option(option):
print("Option selected:", option)
root = tk.Tk()
menu = tk.Menu(root, tearoff=0)
menu.add_command(label="Option 1", command=lambda: select_option(1))
menu.add_command(label="Option 2", command=lambda: select_option(2))
menubutton = tk.Menubutton(root, text="Select an option", relief="raised")
menubutton["menu"] = menu
menubutton.pack()
root.mainloop()
In this example, we created a function select_option
that will be called when a menu option is selected. The lambda
keyword is used to pass the argument 1
or 2
to the function depending on which option is selected. The tearoff
option is used to prevent the menu from being detached from the Menubutton widget.
Conclusion
In summary, the Menubutton widget in Tkinter is an option menu that allows the user to select a value from a predefined set of options. It is created by creating a set of menu options using the Menu
widget, and then creating a Menubutton widget with the menu
property set to the Menu
widget. The Menubutton widget can be displayed using the pack
or grid
methods. By using the Menubutton widget, you can add a drop-down list to your graphical user interface in Python.