当我添加sleep()时,我的tkinter窗口不会打开

2024-09-27 07:32:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着让一个球飞到屏幕的一边,转身,然后回来。每当我尝试运行这个程序时,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()

Tags: fromimport程序move屏幕timetkintersleep
1条回答
网友
1楼 · 发布于 2024-09-27 07:32:17

在windows中,只有在调用mainloop()之后,Tkinter框架才会显示。在您的情况下,for循环可能会阻塞它。在函数中保留for循环,然后使用线程调用该函数,这样它就不会阻塞主循环。你知道吗

相关问题 更多 >

    热门问题