滞后而不是使用数组执行tkinter for循环

时间:2018-04-19 15:09:41

标签: python arrays python-3.x tkinter tkinter-canvas

我正在尝试使用colors中的颜色制作“圆形”爆炸动画,但每当我运行它时,它会冻结并且什么都不做。

谢谢!

from tkinter import *
from random import *
from time import *
from math import *

space = Tk()
s = Canvas(space, height = 1000, width = 1000, background = "light blue")
s.pack()


##EXPLOSION

xE = 375
yE = 475

x = []
y = []
r = []

rSpd = []

eAngle = []

xAr = []
yAr = []

colors = []
drawing = []
sleep(1)
cakeE = 500
colors = ["#EA7FF0","#E44FEE","#87078F","#F9DBFB","#F7B8FB","gray76","grey","white"]
for i in range( cakeE ):                                       
    x.append( xE )
    y.append( yE )

    xAr.append( randint(1, 3) )
    yAr.append( randint(1, 3) )

    r.append( 0 )
    rSpd.append(0)

    eAngle.append( uniform(1,15) )
    eAngle.append( uniform(0, 2*pi) )

    drawing.append(0)
    colors.append( choice(colors) )
for f in range(400):                                                 

    for i in range(cakeE):

        drawing[i] = s.create_rectangle( x[i], y[i], x[i] + xAr[i], y[i]\
                                         + yAr[i], fill = colors[i] )

        x[i] = xE + r[i] * cos( eAngle[i] )
        y[i] = yE - r[i] * sin( eAngle[i] )

        r[i] = r[i] + rSpd[i]
        eAngle[i] = eAngle[i]+0.01

1 个答案:

答案 0 :(得分:0)

使用您提供的代码(不是MCVE)很难提供帮助。 但是,这里有after()的示例用法,向您展示如何使用它。此示例应足以让sleep()替换为after(),请记住,您可能需要将部分代码移动到函数中才能使其正常工作。

from tkinter import *


space = Tk()
lbl = Label(space, text="Before timed event.")
lbl.pack()

def do_something():
    global lbl
    lbl.config(text="After timed event.")
    # execute some code here

# set a 3 second timer to activate the do something function.       
space.after(3000, do_something)

space.mainloop()