开发者

How to make a OptionMenu maintain the same width?

开发者 https://www.devze.com 2023-02-24 03:38 出处:网络
I have a snippet which creates an OptionMenu widget. 开发者_开发技巧... options = (\'White\', \'Grey\', \'Black\', \'Red\', \'Orange\',

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?


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)
...


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


optionmenu.configure(width=<YOUR_WIDTH_HERE>)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消