我在优秀的 Tkdocs 站点上使用的示例之一是关于复选框,我想修改它以显示哪些复选框被选中(在标签中)。
我定义了一个函数,然后重新做了它,认为第二个更清晰,因此更 Pythonic。
但是我相信还有更好的方法......
如果不明显 onevar twovar 和 threevar 是复选框,则 outvar 是我在标签中显示的变量...
欢迎评论!
def checkvars(*args):
if onevar.get():
if twovar.get():
if threevar.get():
outvar.set('All three are true')
else:
outvar.set('one and two are set to true')
elif threevar.get():
outvar.set('one and three are set to true')
else:
outvar.set('one is set to true')
elif twovar.get():
if threevar.get():
outvar.set('two and three are set to true')
else:
outvar.set('two is set to true')
elif threevar.get():
outvar.set('three is set to true')
else:
outvar.set('They are all false')
def checkvars2(*args):
if onevar.get() and twovar.get() and threevar.get():
outvar.set('All three are true')
elif onevar.get() and twovar.get():
outvar.set('one and two are set to true')
elif onevar.get() and threevar.get():
outvar.set('one and three are set to true')
elif onevar.get():
outvar.set('one is set to true')
elif twovar.get() and threevar.get():
outvar.set('two and three are set to true')
elif twovar.get():
outvar.set('two is set to true')
elif threevar.get():
outvar.set('three is set to true')
else:
outvar.set('They are all false')