我们如何停止“ root.after(time.function)”循环?

问题描述 投票:0回答:1

您能帮我吗,我有以下代码:

    root = tkinter.Tk()
    root.geometry("100x200")
    root.resizable(0, 0)

    def loop():
        print("test")
        root.after(1000,loop)

    button=Button(root, text="Start Loop", command=loop).place(x=10,y=10)

    root.mainloop()

而且我想在某个时候停止由另一个def手动或自动调用的after循环。我们如何才能停止循环?

python-3.x loops
1个回答
0
投票

谢谢您的建议。

采用这种方法的接缝:

var=None   #I have given this variable an value 

def start_loop():
    global var
    print("test")
    button_start.configure(text="Stop Loop", command=stop_loop)
    var = root.after(1000,start_loop)   # now the variable has this value

def stop_loop():
    button_start.configure(text="Start Loop", command=start_loop)
    root.after_cancel(var)  # apply the command for this var which is the loop started early  

button_start=Button(root, text="Start Loop", command=start_loop)
button_start.place(x=10,y=10)

希望这是正确的方法,我一直都在寻找它!

© www.soinside.com 2019 - 2024. All rights reserved.