0

我有以下代码:

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具有四个值foobar(the empty space)的a moo

如何在OptionMenu不显示数组的空值的情况下显示?换句话说,我只想显示,foobar忽略空格。mooOptionMenu

array_ly只是一个例子,我想要一些通用的东西来忽略空格。

提前致谢。

4

1 回答 1

1

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']

Use this when you pass the list to the OptionMenu

self.menult = OptionMenu(randomwindow, var, *filter(None, self.array_lt))
于 2014-01-31T11:38:54.880 回答