【问题标题】:call after function several times tkinter多次调用函数后tkinter
【发布时间】:2018-01-10 14:33:03
【问题描述】:

我对 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()

【问题讨论】:

  • 多次按下'go'按钮让球跑得更快的原因是在bouge函数中,它安排自己在30ms后再次运行。每次按下“开始”按钮时,bouge 不仅会运行,而且还会创建另一个事件,再次调用 bouge...
  • 好的,我怎么能在代码中做到这一点?我想创建一个变量 whick 让球在不按下按钮的情况下加速。
  • 如果您希望球自动移动得更快,例如 5 秒间隔后,您可以定期更改全局速度变量(使用 after)。然后当rouge 再次运行时,它将使用新的速度。实现另一个使用after 调用的函数,该函数会更改速度值。
  • 您可以通过更频繁地调用bouge()(直接或间接通过after()更改当前硬编码的速度(即@ 987654331@) 它用于变量。这样,每次调用它时,它都会更多地移动椭圆。每次调用时,加速都会增加这个速度。
  • 认为我明白了,将发布代码仅供任何人参考。

标签: python function tkinter


【解决方案1】:
    from tkinter import *

def speedup():
    global vit
    if vit>2:
        vit-=2
        can1.after(500,speedup)

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,vit))

    if run:
        can1.after(vit, bouge)


def stop():
    global run
    run=False

def start():
    global run
    run=True
    bouge()
    speedup()

x1, y1 = 100, 250
speedx=6
speedy=7
run=True
i=0
vit=100
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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-10
    • 1970-01-01
    • 2021-08-02
    • 2020-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多