Python Tkinter Menu button

Created with Sketch.

Python Tkinter Menubutton

The Menubutton widget can be defined as the drop-down menu that is shown to the user all the time. It is used to provide the user a option to select the appropriate choice exist within the application.

The Menubutton is used to implement various types of menus in the python application. A Menu is associated with the Menubutton that can display the choices of the Menubutton when clicked by the user.

The syntax to use the python tkinter Menubutton is given below.

Syntax

  1. w = Menubutton(Top, options)

A list of various options is given below.

SNOptionDescription
1activebackgroundThe background color of the widget when the widget is under focus.
2activeforegroundThe font color of the widget text when the widget is under focus.
3anchorIt specifies the exact position of the widget content when the widget is assigned more space than needed.
4bgIt specifies the background color of the widget.
5bitmapIt is set to the graphical content which is to be displayed to the widget.
6bdIt represents the size of the border. The default value is 2 pixels.
7cursorThe mouse pointer will be changed to the cursor type specified when the widget is under the focus. The possible value of the cursor type is arrow, or dot etc.
8directionIt 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.
9disabledforegroundThe text color of the widget when the widget is disabled.
10fgThe normal foreground color of the widget.
11heightThe vertical dimension of the Menubutton. It is specified as the number of lines.
12highlightcolorThe highlight color shown to the widget under focus.
13imageThe image displayed on the widget.
14justifyThis specified the exact position of the text under the widget when the text is unable to fill the width of the widget. We can use the LEFT for the left justification, RIGHT for the right justification, CENTER for the centre justification.
15menuIt represents the menu specified with the Menubutton.
16padxThe horizontal padding of the widget.
17padyThe vertical padding of the widget.
18reliefThis option specifies the type of the border. The default value is RAISED.
19stateThe normal state of the Mousebutton is enabled. We can set it to DISABLED to make it unresponsive.
20textThe text shown with the widget.
21textvariableWe can set the control variable of string type to the text variable so that we can control the text of the widget at runtime.
22underlineThe text of the widget is not underlined by default but we can set this option to make the text of the widget underlined.
23widthIt represents the width of the widget in characters. The default value is 20.
24wraplengthWe can break the text of the widget in the number of lines so that the text contains the number of lines not greater than the specified value.

Example

  1. # !/usr/bin/python3
  2. from tkinter import *
  3. top = Tk()
  4. top.geometry(“200×250”)
  5. menubutton = Menubutton(top, text = “Language”, relief = FLAT)
  6. menubutton.grid()
  7. menubutton.menu = Menu(menubutton)
  8. menubutton[“menu”]=menubutton.menu
  9. menubutton.menu.add_checkbutton(label = “Hindi”, variable=IntVar())
  10. menubutton.menu.add_checkbutton(label = “English”, variable = IntVar())
  11. menubutton.pack()
  12. top.mainloop()

Output:

Python Tkinter Menubutton

 

Leave a Reply

Your email address will not be published. Required fields are marked *