Tkinter - Only show filled values of an array in a OptionMenu

旧城冷巷雨未停 提交于 2019-12-25 03:59:23

问题


I've the following code:

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

This shows me a OptionMenu with the four values, foo, bar, (the empty space) and moo.

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 mooon the OptionMenu and ignore the empty space.

The array_ly is just an example, I would like to have something general to ignore always the blank spaces.

Thanks in advance.


回答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))


来源:https://stackoverflow.com/questions/21478806/tkinter-only-show-filled-values-of-an-array-in-a-optionmenu

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