Python Forum
How to stop time counter in Tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to stop time counter in Tkinter
#1
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.
Reply
#2
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Noob question:Using pyttsx3 with tkinter causes program to stop and close, any ideas? Osman_P 4 1,568 Nov-14-2020, 10:51 AM
Last Post: Osman_P
  [Tkinter] How to adjust time - tkinter Ondrej 2 1,225 Jun-20-2019, 05:53 PM
Last Post: Yoriz
  Stop Watch star/stop problem macellan85 1 854 Jun-12-2019, 06:04 PM
Last Post: woooee
  How to stop a tkinter function, without closing the window? keakins 5 8,275 Jun-29-2017, 11:53 AM
Last Post: keakins
  set time & date by user in tkinter gray 3 13,885 Mar-20-2017, 04:00 AM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020