I created a Python timer using classes, but it's not running. Where is the problem in code?

Viewed 43

I wrote this code without using classes and the timer works fine. But here it seems to stop at 00:00:01. Where is the error in the code preventing it from running? My guess is in:

update_time = self.stopwatch.after(1000, self.update)

But I'm not sure.

import tkinter as tk

class MyGUI:

    def __init__(self):
        self.root = tk.Tk()
        self.root.title('Timer with classes')

        running = False
        hours, minutes, seconds = 0, 0, 0


        self.stopwatch = tk.Label(self.root, text='00:00:00', width=25, font=('Arial', 30), justify='center')
        self.stopwatch.pack()

        self.button_start = tk.Button(self.root, text="START TIMER", width=25, borderwidth=5, command=self.start, font=('Arial', 30))
        self.button_start.pack()

        self.root.mainloop()


    def update(self):
        hours, minutes, seconds = 0,0,0
        seconds += 1
        if seconds == 60:
            minutes += 1
            seconds = 0
        if minutes == 60:
            hours += 1
            minutes = 0
        hours_string = f'{hours}' if hours > 9 else f'0{hours}'
        minutes_string = f'{minutes}' if minutes > 9 else f'0{minutes}'
        seconds_string = f'{seconds}' if seconds > 9 else f'0{seconds}'
        self.stopwatch.config(text=hours_string + ':' + minutes_string + ':' + seconds_string)
        global update_time
        update_time = self.stopwatch.after(1000, self.update)

    def start(self):
        running=False
        if not running:
            self.update()
            running = True



MyGUI()
1 Answers

Your timer works, however, it only displays one second because you initialize your seconds, minutes and hours in your update method. What you want to do instead is to declare hours, minutes, seconds as instance attributes of your class so you can initialize them on startup in your start(self) and increment in your update(self) methods like this:

import tkinter as tk

class MyGUI:

    def __init__(self):
        self.root = tk.Tk()
        self.root.title('Timer with classes')

        self.running = False
        self.hours, self.minutes, self.seconds = 0, 0, 0


        self.stopwatch = tk.Label(self.root, text='00:00:00', width=25, font=('Arial', 30), justify='center')
        self.stopwatch.pack()

        self.button_start = tk.Button(self.root, text="START TIMER", width=25, borderwidth=5, command=self.start, font=('Arial', 30))
        self.button_start.pack()

        self.root.mainloop()


    def update(self):
        self.seconds += 1
        if self.seconds == 60:
            self.minutes += 1
            self.seconds = 0
        if self.minutes == 60:
            self.hours += 1
            self.minutes = 0
        hours_string = f'{self.hours}' if self.hours > 9 else f'0{self.hours}'
        minutes_string = f'{self.minutes}' if self.minutes > 9 else f'0{self.minutes}'
        seconds_string = f'{self.seconds}' if self.seconds > 9 else f'0{self.seconds}'
        self.stopwatch.config(text=hours_string + ':' + minutes_string + ':' + seconds_string)
        global update_time
        update_time = self.stopwatch.after(1000, self.update)

    def start(self):
        self.running=False
        if not self.running:
            self.hours, self.minutes, self.seconds = 0,0,0
            self.update()
            running = True



MyGUI()
Related