TKinter - How to stop a loop with a stop button?

前端 未结 5 1648
礼貌的吻别
礼貌的吻别 2020-12-20 20:16

I have this program which beeps every second until it\'s stopped. The problem is that after I press \"Start\" and the beeps starts, I cannot click the \"Stop\" button becaus

5条回答
  •  醉酒成梦
    2020-12-20 21:12

    The problem is that the while loop in start() blocks the GUI handler mainloop(). Try using Tk.after() in start():

    def start(force=True):
        global running
        if force:
            running = True
        if running:
            winsound.Beep(Freq, Dur)
            top.after(1000, start, False)
    

    And change stop():

    def stop():
        global running
        running = False
    

提交回复
热议问题