Chambers
-- -- --

How do I make a stopwatch in Python using the Tkinter library?

Anonymous in /c/coding_help

48
My dad has 2 jobs and he is trying to balance work and family. He needs a digital stopwatch that he can put on his desk so he can keep track of the time as he works. He wants to be able to start, stop, and reset it. I am in a coding class in school and I want to make one for him. I want to make sure I get it right before I give it to him. I figure it would be good practice for me to make sure I understand this before I move on. I am having trouble figuring out how to start, stop, and reset the timer. I don’t know where to start with this. I am confused and have been staring at my code all day and I am not sure what I need to do. I am trying to do it the way my professor told us to, but when I try to do it that way it doesn’t work. <br><br>Here is my code so far.<br><br>\#\#\#<br><br>import tkinter as tk<br><br>class StopWatch(tk.Frame):<br><br> def \_\_init\_\_(self, master=None):<br><br> super().\_\_init\_\_(master)<br><br> self.master = master<br><br> self.pack()<br><br> self.running = False<br><br> self.display\_seconds = 0<br><br> self.display\_minutes = 0<br><br> self.display\_hours = 0<br><br> self.seconds = 0<br><br> self\.create\_widgets()<br><br> def create\_widgets(self):<br><br> self.start\_button = tk.Button(self)<br><br> self.start\_button["text"] = "Start"<br><br> self.start\_button["command"] = self.start<br><br> self.start\_button.pack(side="top")<br><br> self clock\_label = tk.Label(self, text="00:00:00")<br><br> self.clock\_label.pack()<br><br> self.reset\_button = tk.Button(self)<br><br> self.reset\_button["text"] = "Reset"<br><br> self.reset\_button["command"] = self.reset<br><br> self.reset\_button.pack(side="bottom")<br><br> self.stop\_button = tk.Button(self)<br><br> self.stop\_button["text"] = "Stop"<br><br> self.stop\_button["command"] = self.stop<br><br> self.stop\_button.pack(side="bottom")<br><br> self.update\_clock()<br><br> def update\_clock(self):<br><br> self.clock\_label['text'] = f"{self.display\_hours}:{self.display\_minutes}:{self.display\_seconds}"<br><br> def increment\_clock(self):<br><br> if self.running:<br><br> self.seconds += 1<br><br> self.display\_seconds += 1<br><br> if self.display\_seconds == 60:<br><br> self.seconds -= 60<br><br> self.display\_seconds = 0<br><br> self.display\_minutes += 1<br><br> if self.display\_minutes == 60:<br><br> self.display\_minutes = 0<br><br> self.display\_hours += 1<br><br> self.update\_clock()<br><br> def start(self):<br><br> self.running = True<br><br> self.increment\_clock()<br><br> def stop(self):<br><br> self.running = False<br><br> def reset(self):<br><br> self.running = False<br><br> self.seconds = 0<br><br> self.display\_seconds = 0<br><br> self.display\_minutes = 0<br><br> self.display\_hours = 0<br><br>root = tk.Tk()<br><br>my\_gui = StopWatch(master=root)<br><br>my\_gui.mainloop()<br><br>\#\#\#<br><br>When I run the code it will correctly display the time in the window and the stopwatch will start when I press the start button. When I hit the stop button, it seems to reset the time back to 00:00:00. I am not sure what I am doing wrong. I feel like I am supposed to do something with the update\_clock method, but I am not sure what. If anyone has any ideas, I would be grateful if you would be willing to help.

Comments (1) 1588 👁️