【发布时间】:2018-11-06 11:53:39
【问题描述】:
当我简单地使用tkinter 的小部件时,程序按预期工作。当我使用ttk 的小部件时,程序会重复两次。我尝试了我所知道的几乎所有事情来解决这个问题,我相信*args 与它有关。有什么办法可以防止我的function 运行两次?
from tkinter import *
from tkinter import ttk
root = Tk()
first = StringVar(root)
second = StringVar(root)
Ore = {'Options': [''], 'Yes': ['One'], 'No': ['Two']}
entry1 = ttk.OptionMenu(root, first, *Ore.keys())
entry2 = ttk.OptionMenu(root, second, '')
entry1.pack()
entry2.pack()
def _up_options(*args):
print('update_options')
ores = Ore[first.get()]
second.set(ores[0])
menu = entry2['menu']
menu.delete(0, 'end')
for line in ores:
print('for')
menu.add_command(label=line, command=lambda choice=line: second.set(choice))
first.trace('w', _up_options)
root.mainloop()
PS,我把 *args 放在我的函数中工作。如果有人能解释一下,我将不胜感激
【问题讨论】:
-
看我的回答,有什么问题就问吧!
-
@MosheSlavin 谢谢,我完全明白为什么我现在需要
*args。我的主要问题仍然存在,我将进一步搜索ttk的文档。 -
很高兴帮助...我没有看到您描述的问题:>'当我使用 ttk 的小部件时,程序会重复两次'你能详细说明吗?
-
我无法在 linux 上复制该问题。当我选择一个选项时,
_up_options只会被调用一次。 -
@BryanOakley 既然我认为你是专家(并且你提出了我提到的错误报告),对我的回答有什么想法吗?
标签: python-3.x tkinter ttk