Header Ads

Header ADS

MenuButton in Tkinter


Tkinter MenuButton widget .

The Menubutton widget can be defined as the drop-down menu that is shown to the user all the time. The Menubutton is used to implement various types of menus in the python application.

it works like a pop-up menu but the difference is it poped-up on a specific place not like a pop-up menu which is created atevery place on root window .

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.

So lets code it .
       			 
# Tkinter - Dynamic Coding
# Menubutton widget ...

# import library
from tkinter import *

root = Tk() 
root.geometry("300x200") 
root.title("Dynamic Coding")

w = Label(root, text ='Dynamic Coding', font =('arial',15,'bold')) 
w.pack() 

menubutton = Menubutton(root, text = "Menu") 
	
menubutton.menu = Menu(menubutton,tearoff = 0 ,relief=RAISED) 
menubutton["menu"]= menubutton.menu 

var1 = IntVar() 
var2 = IntVar() 
var3 = IntVar() 

# add checkbutton in menu buton menu
menubutton.menu.add_checkbutton(label = "Python", variable = var1) 
menubutton.menu.add_checkbutton(label = "Java",variable = var2) 
menubutton.menu.add_checkbutton(label = "HTML",variable = var3) 
	
menubutton.pack() 
root.mainloop() 

 


Output.




Attributes / Options of Menubutton

activebackground: This option used to represent the background color when the Menubutton is under the cursor.

activeforeground: This option used to represent the foreground color when the Menubutton is under the cursor.

bg: This option used to represent the normal background color displayed behind the label and indicator.

bitmap: This option used to display a monochrome image on a button.

bd: This option used to represent the size of the border around the indicator and the default value is 2 pixels.

anchor: This option specifies the exact position of the widget content when the widget is assigned more space than needed.

cursor: By using this option, the mouse cursor will change to that pattern when it is over the Menubutton.

disabledforeground: The foreground color used to render the text of a disabled Menubutton. The default is a stippled version of the default foreground color.

direction: It direction can be specified so that menu can be displayed to the specified direction of the button.

fg: This option used to represent the color used to render the text.

height: This option used to represent the number of lines of text on the Menubutton and it’s default value is 1.

highlightcolor: This option used to represent the color of the focus highlight when the Menubutton has the focus.

image: This option used to display a graphic image on the button.

justify: This option used to control how the text is justified: CENTER, LEFT, or RIGHT.
menu: It represents the menu specified with the Menubutton.

padx: This option used to represent how much space to leave to the left and right of the Menubutton and text. It’s default value is 1 pixel.

pady: This option used to represent how much space to leave above and below the Menubutton and text. It’s default value is 1 pixel.

relief: The type of the border of the Menubutton. It’s default value is set to FLAT.

state: It represents the state of the Menubutton. By default, it is set to normal. We can change it to DISABLED to make the Menubutton unresponsive. The state of the Menubutton is ACTIVE when it is under focus.

text: This option used use newlines (“\n”) to display multiple lines of text.

underline: This option used to represent the index of the character in the text which is to be underlined. The indexing starts with zero in the text.

textvariable: This option used to represents the associated variable that tracks the state of the Menubutton.

width: This option used to represents the width of the Menubutton. and also represented in the number of characters that are represented in the form of texts.

wraplength: This option will be broken text into the number of pieces.





Recommended Posts:





Thank you ..
If have Any doubts and Errors , comment section is open for you and we will try to solve your doubts as soon as possible.



No comments

Powered by Blogger.