我在短短的一圈里,在一个平地的画布里,寻找一个睡觉的地方。在Python2中,目标是有一个随机的移动点,每X秒刷新一次(然后我将能够有一个更大的脚本来精确地制作我想要的东西),而不需要任何外部用户的输入。
现在,我做了这个:
import Tkinter, time
x1, y1, x2, y2 = 10, 10, 10, 10
def affichage():
global x1, y1, x2, y2
can1.create_rectangle(x1, y1, x2, y2, fill="blue", outline="blue")
def affichage2():
global x1, y1, x2, y2
can1.delete("all")
can1.create_rectangle(x1, y1, x2, y2, fill="blue", outline="blue")
fen1 = Tkinter.Tk()
can1 = Tkinter.Canvas(fen1, height=200, width=200)
affichage()
can1.pack()
temps = 3000
while True:
can1.after(temps, affichage2)
x1 += 10
y1 += 10
x2 += 10
y2 += 10
temps += 1000
fen1.mainloop()
fen1.destroy()(不好意思,法语变量名是:°)所以,我尝试使用.after函数,但是我不能以我想要的方式增加它。我认为多线程是可能的,但必须有一个更简单的解决方案。
你知不知道?
发布于 2016-07-12 17:21:55
sleep不能很好地与Tkinter混合,因为它会使事件循环停止,从而使窗口锁定并对用户输入失去响应。使某件事情每隔X秒发生的通常方法是将after调用放在传递给after的函数中。尝试:
import Tkinter, time
x1, y1, x2, y2 = 10, 10, 10, 10
def affichage():
global x1, y1, x2, y2
can1.create_rectangle(x1, y1, x2, y2, fill="blue", outline="blue")
def affichage2():
global x1, y1, x2, y2
can1.delete("all")
can1.create_rectangle(x1, y1, x2, y2, fill="blue", outline="blue")
x1 += 10
y1 += 10
x2 += 10
y2 += 10
can1.after(1000, affichage2)
fen1 = Tkinter.Tk()
can1 = Tkinter.Canvas(fen1, height=200, width=200)
affichage()
can1.pack()
temps = 3000
can1.after(1000, affichage2)
fen1.mainloop()
fen1.destroy()https://stackoverflow.com/questions/38335168
复制相似问题