我正在试着让球跑到屏幕的一侧,转过身来,然后再回来。每当我尝试运行这个程序时,tkinter窗口都不会显示,但当我去掉睡眠(0.5)部分时,当球已经离开屏幕时,它就会显示出来。有人能告诉我我哪里做错了吗?
from tkinter import *
from time import sleep
window = Tk()
cWidth = 800
cHeight = 500
c = Canvas(window, width = cWidth, height = cHeight, bg='black')
c.pack()
x = 400
y = 250
ball = c.create_polygon(x, y, x, y+25, x+25, y+25, x+25,y, fill='yellow')
Ball_move = 10
for i in range(200):
c.move(ball, Ball_move, 0)
window.update
x += Ball_move
if x == cWidth:
Ball_move = -Ball_move
sleep(0.5)
window.mainloop()发布于 2016-06-16 22:08:52
在windows中,Tkinter frame只有在你调用mainloop()之后才会出现。在您的例子中,for循环可能阻塞了它。将for循环保留在函数中,然后使用线程调用该函数,这样它就不会阻塞主循环。
https://stackoverflow.com/questions/37861238
复制相似问题