How to make a OptionMenu maintain the same width?

北城余情 提交于 2019-12-04 00:34:44

问题


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?


回答1:


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




回答2:


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



回答3:


optionmenu.configure(width=<YOUR_WIDTH_HERE>)


来源:https://stackoverflow.com/questions/5629745/how-to-make-a-optionmenu-maintain-the-same-width

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!