- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
- Python 3 Advanced Tutorial
- Python 3 - Classes/Objects
- Python 3 - Reg Expressions
- Python 3 - CGI Programming
- Python 3 - Database Access
- Python 3 - Networking
- Python 3 - Sending Email
- Python 3 - Multithreading
- Python 3 - XML Processing
- Python 3 - GUI Programming
- Python 3 - Further Extensions
- Python 3 Useful Resources
- Python 3 - Questions and Answers
- Python 3 - Quick Guide
- Python 3 - Tools/Utilities
- Python 3 - Useful Resources
- Python 3 - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python 3 - Tkinter Menubutton
A menubutton is the part of a drop-down menu that stays on the screen all the time. Every menubutton is associated with a Menu widget that can display the choices for that menubutton when the user clicks on it.
Here is the simple syntax to create this widget −
w = Menubutton ( master, option, ... )
master − This represents the parent window.
options − Here is the list of most commonly used options for this widget. These options can be used as key-value pairs separated by commas.
|Sr.No.||Option & Description|
The background color when the mouse is over the menubutton.
The foreground color when the mouse is over the menubutton.
This options controls where the text is positioned if the widget has more space than the text needs. The default is anchor = CENTER, which centers the text.
The normal background color displayed behind the label and indicator.
To display a bitmap on the menubutton, set this option to a bitmap name.
The size of the border around the indicator. Default is 2 pixels.
The cursor that appears when the mouse is over this menubutton.
Set direction = LEFT to display the menu to the left of the button; use direction = RIGHT to display the menu to the right of the button; or use direction = 'above' to place the menu above the button.
The foreground color shown on this menubutton when it is disabled.
The foreground color when the mouse is not over the menubutton.
The height of the menubutton in lines of text (not pixels!). The default is to fit the menubutton's size to its contents.
Color shown in the focus highlight when the widget has the focus.
To display an image on this menubutton,
This option controls where the text is located when the text doesn't fill the menubutton: use justify = LEFT to left-justify the text (this is the default); use justify = CENTER to center it, or justify = RIGHT to right-justify.
To associate the menubutton with a set of choices, set this option to the Menu object containing those choices. That menu object must have been created by passing the associated menubutton to the constructor as its first argument.
How much space to leave to the left and right of the text of the menubutton. Default is 1.
How much space to leave above and below the text of the menubutton. Default is 1.
Selects three-dimensional border shading effects. The default is RAISED.
Normally, menubuttons respond to the mouse. Set state = DISABLED to gray out the menubutton and make it unresponsive.
To display text on the menubutton, set this option to the string containing the desired text. Newlines ("\n") within the string will cause line breaks.
You can associate a control variable of class StringVar with this menubutton. Setting that control variable will change the displayed text.
Normally, no underline appears under the text on the menubutton. To underline one of the characters, set this option to the index of that character.
The width of the widget in characters. The default is 20.
Normally, lines are not wrapped. You can set this option to a number of characters and all lines will be broken into pieces no longer than that number.
Try the following example yourself −
# !/usr/bin/python3 from tkinter import * import tkinter top = Tk() mb = Menubutton ( top, text = "condiments", relief = RAISED ) mb.grid() mb.menu = Menu ( mb, tearoff = 0 ) mb["menu"] = mb.menu mayoVar = IntVar() ketchVar = IntVar() mb.menu.add_checkbutton ( label = "mayo", variable = mayoVar ) mb.menu.add_checkbutton ( label = "ketchup", variable = ketchVar ) mb.pack() top.mainloop()
When the above code is executed, it produces the following result −