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
#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


Messages In This Thread
How to stop time counter in Tkinter - by LoneStar - Sep-11-2020, 08:18 PM
RE: How to stop time counter in Tkinter - by Yoriz - Sep-11-2020, 08:56 PM

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 5,324 Nov-14-2020, 10:51 AM
Last Post: Osman_P
  [Tkinter] How to adjust time - tkinter Ondrej 2 2,870 Jun-20-2019, 05:53 PM
Last Post: Yoriz
  Stop Watch star/stop problem macellan85 1 2,584 Jun-12-2019, 06:04 PM
Last Post: woooee
  How to stop a tkinter function, without closing the window? keakins 5 13,088 Jun-29-2017, 11:53 AM
Last Post: keakins
  set time & date by user in tkinter gray 3 18,924 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