首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >短吻鳄睡眠(python2)

短吻鳄睡眠(python2)
EN

Stack Overflow用户
提问于 2016-07-12 17:11:31
回答 1查看 66关注 0票数 0

我在短短的一圈里,在一个平地的画布里,寻找一个睡觉的地方。在Python2中,目标是有一个随机的移动点,每X秒刷新一次(然后我将能够有一个更大的脚本来精确地制作我想要的东西),而不需要任何外部用户的输入。

现在,我做了这个:

代码语言:javascript
复制
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函数,但是我不能以我想要的方式增加它。我认为多线程是可能的,但必须有一个更简单的解决方案。

你知不知道?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-12 17:21:55

sleep不能很好地与Tkinter混合,因为它会使事件循环停止,从而使窗口锁定并对用户输入失去响应。使某件事情每隔X秒发生的通常方法是将after调用放在传递给after的函数中。尝试:

代码语言:javascript
复制
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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38335168

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档