Tkinter-仅在OptionMenu中显示数组的填充值 [英] Tkinter - Only show filled values of an array in a OptionMenu
问题描述
我有以下代码:
self.array_lt = ['foo', 'bar', '', 'moo']
var = StringVar()
self.menult = OptionMenu(randomwindow, var, *self.array_lt)
self.menult.config(width=30)
self.menult.grid(row=0, column=0, padx=(5,5), pady=(5,5))
这向我显示了一个 OptionMenu
,其中包含四个值 foo
, bar
,(空白处)
和 moo
。
This shows me a OptionMenu
with the four values, foo
, bar
, (the empty space)
and moo
.
如何显示 OptionMenu
而不显示数组的空值?换句话说,我只想显示 foo
, bar
和 moo $ c
OptionMenu
上的$ c>并忽略空白区域。
How can I show the OptionMenu
without showing the empty value of the array? In another words, I want to show only foo
, bar
and moo
on the OptionMenu
and ignore the empty space.
array_ly
只是一个例子,我希望有一些通用的东西总是忽略空格。
The array_ly
is just an example, I would like to have something general to ignore always the blank spaces.
提前谢谢。
推荐答案
您可以使用 filter
与 None
作为过滤器函数,用于过滤出当解释为布尔值时会得出 False
的值:
You can use filter
with None
as the filter function to filter out values that would evaluate to False
when interpreted as a boolean:
>>> filter(None, ["1", 0, " ", "", None, True, False, "False"])
['1', ' ', True, 'False']
当您将列表传递给 OptionMenu
时使用此选项p>
Use this when you pass the list to the OptionMenu
self.menult = OptionMenu(randomwindow, var, *filter(None, self.array_lt))
这篇关于Tkinter-仅在OptionMenu中显示数组的填充值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!