我可以看到我和How can I update a Tkinter canvas in the middle of a function call?有类似的问题,但我无法将接受的答案翻译成我的情况。在那个SO答案中,原则上调用自身的函数的解决方案很好,但我需要将一个列表a
传递给函数,而不仅仅是调用它。
顺便说一句,是的,我知道该程序有效:打印列表a
这是#{1}}中的第一行#'d工作得很好
问题:我如何让Tkinter将列表def renderGUI(a):
(0表示灯关闭)动画为ons和offs?
请注意我也尝试了a
,虽然有些偷看似乎对这种做法很不满意。
代码在
之下提前多多感谢:
w.update_idletasks()
答案 0 :(得分:0)
要使您的程序正常运行,只需在函数内部进行for
循环,然后通过after
调用它:
def f():
for i in range (0,numLights):
master.after(100, renderGUI(a))
w.update_idletasks()
for n in range(0,numLights):
if a[n]>1:
a[n] = a[n]-1
elif a[n] == 1:
#then its about to become nought, right?
a[n-1] = speed[n]
speed[n-1] = speed[n]
a[n] = a[n]-1
continue
continue
btn = Button(master, text='Stop', width=20, command=master.destroy)
btn.pack()
master.after(1, f)
master.mainloop()
但是,如果你想摆脱update_idletasks
,你可以使用一个协同程序(在这里包装成装饰器):
def updatesdisplay(root, t=0):
def _updatesdisplay(func):
def driver(iterator):
try: next(iterator)
except StopIteration: pass
else:
if t: root.after(t, driver, iterator)
else: root.after_idle(driver, iterator)
def wrapped():
driver(func())
return wrapped
return _updatesdisplay
@updatesdisplay(master, 100) # 100 because we want to slow things down a bit
def f():
for i in range (0,numLights):
renderGUI(a)
yield # window gets updated here
for n in range(0,numLights):
if a[n]>1:
a[n] = a[n]-1
elif a[n] == 1:
#then its about to become nought, right?
a[n-1] = speed[n]
speed[n-1] = speed[n]
a[n] = a[n]-1
continue
continue
btn = Button(master, text='Stop', width=20, command=master.destroy)
btn.pack()
master.after(1, f)
master.mainloop()