我正在尝试更改tkinter画布小部件上的对象数组的状态,但是需要等待一段时间才能使更改生效。当定义不带参数的change_color()函数时,行为是预期的,因此我首先看到画布和对象处于初始状态,然后是延迟,然后是对象处于最终状态。当我在函数定义中添加一个参数,然后调用它传递一个参数以选择要更改的对象时,最终结果可以,但是在画布上没有显示初始状态或延迟。是否可以将参数传递给从Tkinter调用的函数?
# LED simulation using tkinter canvas widget
from tkinter import *
root = Tk()
root.geometry('640x480')
root.title('LED Simulation')
canvas = Canvas(width=640, height=480, bg='black')
canvas.pack()
# create LED objects on the canvas in the OFF state (dark red)
d7 = canvas.create_oval(50, 50, 75, 75, fill='dark red')
d6 = canvas.create_oval(100, 50, 125, 75, fill='dark red')
d5 = canvas.create_oval(150, 50, 175, 75, fill='dark red')
d4 = canvas.create_oval(200, 50, 225, 75, fill='dark red')
d3 = canvas.create_oval(250, 50, 275, 75, fill='dark red')
d2 = canvas.create_oval(300, 50, 325, 75, fill='dark red')
d1 = canvas.create_oval(350, 50, 375, 75, fill='dark red')
d0 = canvas.create_oval(400, 50, 425, 75, fill='dark red')
# changes the state of an LED from OFF to ON (red)
def change_color(led):
canvas.itemconfig(led, fill='red')
# wait for 1 sec, then change state to ON
root.after(1000, change_color(d3))
root.mainloop()
答案 0 :(得分:0)
如亨利先生所说,您可以使用lambda:
root.after(1000, lambda: change_color(d3))