在 Python Tkinter 中,如何将 OptionMenu 设置为比最长项目短的固定大小?
In Python Tkinter, how do I set an OptionMenu to a fixed size shorter than the longest item?
听起来很简单,我有一个选项菜单,其中包含大约 5 个字符的术语和一个很长的术语。 When the long option is selected, the window stretches out and it looks atrocious.设置宽度或 sticky=EW 仅在宽度大于最长项的长度时才有效。
理想情况下,我希望最多显示 15 个字符,如果更长的话后面跟一个“...”。
有什么想法吗?谢谢
我认为您正在寻找 ttk 中更强大的“Combobox”(也在标准 python 中作为 Tk 的简单扩展)。
The option menu is similar to the combobox
例如:
from tkinter.ttk import Combobox # python 3 notation
combo = Combobox(root,values=['a','aa','aaaaa','aaaaaaa'],width=3)
它会简单地剪掉太长的元素
(If you're in python 2 it's slightly different to import ttk)
如果您希望在截断条目时出现漂亮的“...”,我认为您最好的选择是
elements = ['a','aa','aaaaaaaa']
simple_values = [ e[:3] + ('...' if len(e) > 3 else '') for e in elements]
combo = Combobox(root,values=simple_values )
如果您需要能够在它们之间进行映射,请使用您喜欢的数据结构或通过索引而不是组合框中的值进行引用
听起来很简单,我有一个选项菜单,其中包含大约 5 个字符的术语和一个很长的术语。 When the long option is selected, the window stretches out and it looks atrocious.设置宽度或 sticky=EW 仅在宽度大于最长项的长度时才有效。
理想情况下,我希望最多显示 15 个字符,如果更长的话后面跟一个“...”。
有什么想法吗?谢谢
我认为您正在寻找 ttk 中更强大的“Combobox”(也在标准 python 中作为 Tk 的简单扩展)。
The option menu is similar to the combobox
例如:
from tkinter.ttk import Combobox # python 3 notation
combo = Combobox(root,values=['a','aa','aaaaa','aaaaaaa'],width=3)
它会简单地剪掉太长的元素
(If you're in python 2 it's slightly different to import ttk)
如果您希望在截断条目时出现漂亮的“...”,我认为您最好的选择是
elements = ['a','aa','aaaaaaaa']
simple_values = [ e[:3] + ('...' if len(e) > 3 else '') for e in elements]
combo = Combobox(root,values=simple_values )
如果您需要能够在它们之间进行映射,请使用您喜欢的数据结构或通过索引而不是组合框中的值进行引用