- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to make an OptionMenu maintain the same width using tkinter?
The OptionMenu allows users to select an option from the given menu of items. To prepare an OptionMenu, we use the OptionMenu(arguments) constructor, which takes the parent widget, a variable to store the options, the default option, and selectable options.
However, in some cases, we can find that the options width is quite different from the pane width. We can maintain the width of the options by finding the maximum length from the given options. Now, by using the config(width) method, we can set the width of the OptionMenu.
Example
#Import Tkinter library from tkinter import * from tkinter import ttk #Create an instance of Tkinter frame or window win= Tk() #Set the geometry of tkinter frame win.geometry("750x250") #Create Menu Items options=("Low", "Medium", "High") #Find the length of maximum character in the option menu_width = len(max(options, key=len)) #Create an OptionMenu menu=OptionMenu(win, options[0], *options) menu.config(width=menu_width) menu.pack(pady=30, ipadx=10) win.mainloop()
Output
Running the above code will display a window that contains an option pane with a list of options. The width of the options depends upon the maximum length of the string in the given options.
- Related Articles
- How to call a function using the OptionMenu widget in Tkinter?
- How to detect when an OptionMenu or Checkbutton changes in Tkinter?
- How to maintain stroke width of Ellipse while scaling using FabricJS?
- How to maintain stroke width of a Rectangle while scaling using FabricJS?
- How to maintain stroke width of a Triangle while scaling using FabricJS?
- How to make two histograms have the same bin width in Matplotlib?
- How to change the menu background color of Tkinter's OptionMenu widget?
- How to make an element width: 100% minus padding?
- How to make a Button using the Tkinter Canvas widget?
- How to get the width of the Tkinter widget?
- How to add Label width in Tkinter?
- Getting screens height and width using Tkinter Python
- How to get the width of an element using jQuery?
- How to resize an image using Tkinter?
- How to make a new folder using askdirectory dialog in Tkinter?

Advertisements