Python Forum
How to stop time counter in Tkinter - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: How to stop time counter in Tkinter (/thread-29603.html)



How to stop time counter in Tkinter - LoneStar - Sep-11-2020

Hi,

I have an "animated" timer that displays hour:minute:seconds on a Label. When user clicks the start button, the timer starts incrementing from 1 second on.

Now, I have a stop button that when users click, I want the time at that moment to freeze (stop). But I can't make it stop. It keeps going. Below is my code snippets:

def start_test_time(self):
    def count():
        global tt_counter
        
        #Manage initial delay:
        if tt_counter == 0:
            display = "Starting"
        else:
            startTime = datetime.fromtimestampt(tt_counter)
            startTime = startTime.replace(hour=0)
            ttStr = startTime.strftime("%H:%M:%S")
            display = ttStr

	self.LabelTestTime.config(text=display)

	#Trigger after 1 second delay
	self.LabelTestTime.after(1000, count)

	tt_counter += 1

    #Trigger the start of counter
    count()


def stop_test_time(self):
    #I tried samples online but none worked.


def on_btn_start(self):
    #Trigger the test time
    self.start_test_timer()


def on_btn_stop(self):
    self.stop_test_time()
Appreciate any help.


RE: How to stop time counter in Tkinter - Yoriz - Sep-11-2020

After will return an id
self.after_id = self.LabelTestTime.after(1000, count)
when you want to stop call
tk.after_cancel(self.after_id)

Actually your code is not quite set up right, when posting code it should be minimal but also runnable to reproduce your problem.
see the below example
import tkinter as tk


class Main:
    def __init__(self, root):
        self.counter = 0
        self.after_id = None
        self.frame = tk.Frame(root)
        self.label = tk.Label(self.frame, text="0")
        self.label.pack()

        btn_start = tk.Button(self.frame, text="Start",
                              command=self.on_btn_start)
        btn_start.pack()
        btn_stop = tk.Button(self.frame, text="Stop",
                             command=self.on_btn_stop)
        btn_stop.pack()
        btn_reset = tk.Button(self.frame, text="Reset",
                              command=self.on_btn_reset)
        btn_reset.pack()
        self.frame.pack()

    def on_btn_start(self):
        if not self.after_id:
            self.increase_counter()

    def on_btn_stop(self):
        if self.after_id:
            self.frame.after_cancel(self.after_id)
            self.after_id = None

    def on_btn_reset(self):
        self.on_btn_stop()
        self.counter = 0
        self.display_counter()

    def increase_counter(self):
        self.counter += 1
        self.display_counter()
        self.after_id = self.frame.after(1000, self.increase_counter)

    def display_counter(self):
        self.label['text'] = self.counter


root = tk.Tk()
main = Main(root)
root.mainloop()