How to make a OptionMenu maintain the same width?
Asked Answered
B

5

16

I have a snippet which creates an OptionMenu widget.

...
options = ('White', 'Grey', 'Black', 'Red', 'Orange', 
           'Yellow', 'Green', 'Blue', 'Cyan', 'Purple')
var = StringVar()
optionmenu = OptionMenu(par, var, *options)
optionmenu.grid(column=column, row=row)
...

One problem I've encountered is every time a new option is selected, the width of the widget changes. I believe this is due to the text within the widget changing widths. How do I make the widget hold a consistent width?

Became answered 12/4, 2011 at 2:27 Comment(0)
M
17

When you use the grid command to place the widget in its parent, have the widget fill its cell (try sticky="ew")

Marris answered 12/4, 2011 at 11:3 Comment(2)
This does not work for me. I am using a dropdown in a frame. Yet it expands it for long options in the dropdown.Jar
An alternate could be to find the maximum width of all widgets in that column and then set the default of the variable within the option menu to be " "*(max_width/font_size_of_option_widget). The size of the widget can be obtained using widget.update() and then widget.winfo_width()Germinate
D
37

To the best of my knowledge, you can use optionmenu.config(width=<YOUR_WIDTH>) as follows:

...
optionmenu = OptionMenu(par, var, *options)
optionmenu.config(width=<YOUR_WIDTH>)
optionmenu.grid(column=column, row=row)
...
Dit answered 12/4, 2011 at 13:58 Comment(0)
M
17

When you use the grid command to place the widget in its parent, have the widget fill its cell (try sticky="ew")

Marris answered 12/4, 2011 at 11:3 Comment(2)
This does not work for me. I am using a dropdown in a frame. Yet it expands it for long options in the dropdown.Jar
An alternate could be to find the maximum width of all widgets in that column and then set the default of the variable within the option menu to be " "*(max_width/font_size_of_option_widget). The size of the widget can be obtained using widget.update() and then widget.winfo_width()Germinate
S
3
optionmenu.configure(width=<YOUR_WIDTH_HERE>)
Skulk answered 11/7, 2013 at 16:13 Comment(0)
N
0

I'm in the exact same boat as the OP was 12 years ago - placing dropdowns in a frame, only to discover that no solution exists for setting them to expand horizontally and fill that frame. As a result they're always resizing (and re-centering) any time a different-length option is selected. sticky='' has no effect whatsoever. width='' works, but now you're setting a fixed width that is only optimized for one resolution.

Of course the terrible solution is to use a monospace font and add spaces to the end of all the options in the dropdown. I just don't understand how there can be no real solution for this. These dropdowns look absolutely atrocious as a result.

Non answered 13/11, 2022 at 2:20 Comment(3)
i've posted an answer to this, in case you're looking for a better solution.Hardly
Thanks, I'll take a look. Do you happen to have a link?Non
it's just below your response here: https://mcmap.net/q/713006/-how-to-make-a-optionmenu-maintain-the-same-widthHardly
H
0

This can be done with

frame.columnconfigure(1, minsize=120)
optmenu = ttk.OptionMenu(frame,...)
optmenu.grid(row=1, column=1, sticky=tk.EW)

Not optimal as you have to guess the pixel width of the colume, but it works.

Here are some links, I found to be useful in coming up with this answer.

https://www.pythontutorial.net/tkinter/tkinter-grid/

Set minimum width of column in grid

https://tkdocs.com/tutorial/grid.html

Hardly answered 15/8, 2023 at 2:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.