MENUBUTTON IN TKINTER


In this tutorial we will learn about the menubutton in Tkinter. We will learn about the features and properties of the widget followed by a handy example.





What is Menubutton in Tkinter?

Menu Button in Tkinter is a drop-down menu which is shown to the user when the pulldown menu is activated. The Menubutton displays different choices that the user can select from.

Normally, you won’t use the Menubutton. It is only used when you want to provide a set of options to the user i.e. asking the user for occupation, survey etc. The syntax for using Menubutton is:

menubutton_tk = Menubutton( window, features )

Menubutton features and properties are:

1 activebackground   Shows the button background color upon the mouse hover
2 activeforeground  Shows the widget font color upon the mouse hover.
3 anchor It’s shows the initial position of the widget. It’s initial value is always the CENTER position which is used to position the text in the middle position.
4 bg It shows the background color of the menubutton 
5 bitmap It is used to display bitmap in the widget but if the image option is already given then this option will be ignored.
6 bd It shows the width of the border. Same as borderwidth. Default value is 2px.
7 cursor Shows the type of cursor when the mouse is hovered over the label. The default is a pointed arrow cursor.
8 direction Sets the direction of the menu by using LEFT, RIGHT and ABOVE to place the widget.
9 disabledforeground This will specify the foreground color when the widget is disabled.
10 fg Shows the foreground color of the Menubutton 
11 height It decides the height of the menubutton. 
12 highlightcolor It shows the highlight color of the menubutton when it is clicked
13 image It is used to set image on the menubutton 
14 justify It is used to display text on menubutton in RIGHT, LEFT and CENTER justified positions.
15 menu Shows the menu in correspondence to the menubutton.
16 padx This adds padding in horizontal direction
17 pady This adds padding in vertical direction
18 relief This option specifies the type of the border. The default value is RAISED.
19 state This represents DISABLED or ACTIVE state of the menubutton.
20 text Decides the text to be displayed on the menubutton.
21 textvariable By using this feature, you can easily update the text of the label whenever you want.
22 underline It is used with the text option to tell whether the character should be underlined. -1 is a default value (no underline).
23 width Shows the width of the checkbutton. Default value is 20.
24 wraplength Sets the wrap length of the number of characters to be used.




Creating Listbox in Tkinter

You can create a simple menubutton as follows:

from tkinter import *    
screen = Tk()

screen.geometry('400x400')

languages = Menubutton(screen, text='Programming languages', justify=CENTER)     

options   = Menu(languages)               
languages.config(menu=options)           
options.add_command(label='Python')
options.add_command(label='Java')
options.add_command(label='PHP')
languages.pack()
screen.mainloop()

Output will be:

Menubutton

References have been used from here