Apr-16-2020, 08:59 PM
You should give you widgets names that indicate what they do like "start_button" or "time_display". You should definitely rename class Window to something like class Stopwatch. Good names go a long way in making programs easy to understand.
After you create the start_button you need to bind it to a function. I'm going to write a new function named "start_timer" (good names make programming easier). start_timer() is going to start the timer. It should reset the tick counter and start the timer. Binding the button to the function is as simple as:
Let's review. Pressing the start_button calls start_timer(). start_timer() starts the timer running. Every ten milliseconds the timer calls update_time(). update_time() increments the tick counters and updates the time labels.
Now all you need is a way to stop the timer.
After you create the start_button you need to bind it to a function. I'm going to write a new function named "start_timer" (good names make programming easier). start_timer() is going to start the timer. It should reset the tick counter and start the timer. Binding the button to the function is as simple as:
self.start_button.click.connect(self.start_timer)I start writing the "start_timer()" func. Hey, where is the timer? If you write a stopwatch class it should have a timer somewhere. Add some code to the __init__ method to create a timer (with a good name like "timer") and bind it to the function that updates time. Binding the button click to the start_time function is as simple as:
self.timer = QTimer(self) self.timer.timeout.connect(self.update_time)Now I need a "update_time()" function. I see your code has a function named "local_button_handler" that does the counting and updates the time labels. Let's rename that to "update_time". Getting pretty close to making this thing work.
Let's review. Pressing the start_button calls start_timer(). start_timer() starts the timer running. Every ten milliseconds the timer calls update_time(). update_time() increments the tick counters and updates the time labels.
Now all you need is a way to stop the timer.