Tkinter-仅在OptionMenu中显示数组的填充值 [英] Tkinter - Only show filled values of an array in a OptionMenu

查看:69
本文介绍了Tkinter-仅在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 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 mooon 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆