我有一个关于after函数以及它在mainloop
中如何工作的问题。
我编写了下面的代码,我注意到我可以多次调用after
函数。这使得球每次被召唤时移动得更快。我想在不按几次按钮的情况下复制它。在
另外,有人能解释一下,按几次“开始”按钮会使球跑得更快吗。在
PS:对不起我的英语,我是法国人。。。在
from tkinter import *
def bouge():
global speedx,speedy,run,i
colors=["white", "black", "red", "green", "blue", "cyan", "yellow"]
pos = can1.coords(oval1)
if pos[0] <= 0 or pos[0] >= 450:
speedx *= -1
can1.itemconfig(oval1,fill=colors[i])
i+=1
i=i%6
if pos[1]<=0 or pos[1]>=450:
speedy*=-1
can1.move(oval1, speedx, speedy)
text1.configure(text=pos)
if run:
can1.after(30, bouge)
def stop():
global run
run=False
def start():
global run
run=True
bouge()
x1, y1 = 100, 250
speedx=5
speedy=6
run=True
i=0
fen1 = Tk()
fen1.title("Arkanoid made by Rico")
can1 = Canvas(fen1, height=500, width=500,background='lightblue')
oval1 = can1.create_oval(x1, y1, x1 + 50, y1 + 50, fill='yellow')
rec1=can1.create_rectangle(225,480,275,500,fill='grey')
pos=can1.coords(oval1)
Button(fen1, text='GO!!', command=start).grid(row=2, column=1, sticky=S)
Button(fen1, text='Quitter', command=fen1.quit).grid(row=2, column=3, sticky=S)
Button(fen1, text='Stop', command=stop).grid(row=2, column=2, sticky=S)
text1=Label(fen1,text=pos)
text1.grid(row=2,column=4)
can1.grid(row=1,columnspan=5)
fen1.mainloop()
相关问题 更多 >
编程相关推荐