0

I'm trying to make a stopwatch where it starts from 00:00:00 and stops when a certain method is called. The problem is that all the websites and videos i found are tutorials on adding real time into the window and not a actual timer/stopwatch. The ones that worked i also didn't know how to put in in my program. Please use the below code as an example to create the stopwatch. thanks.

from tkinter import *
root = Tk()

class Start:
    def __init__ (self, master):
        self.master = master
        #code

    def timer(self):
        #start timer
    
    def end(self):
        #end timer

root.mainloop()
Mark Lin
  • 89
  • 6
  • 1
    Hello Mark, can I know why are you asking the [same question](https://stackoverflow.com/questions/62608780/how-to-make-a-timer-in-tkinter) over and over?? Tell us how this [anwser](https://stackoverflow.com/a/2401181/10364425) has not helped you? – Saad Jun 27 '20 at 13:47
  • @saad i have tried the code from the suggested questions and it's different. that one shows current time while i want a timer, stopwatch which starts from 00:00:00 instead of current time. – Mark Lin Jun 27 '20 at 14:16
  • @MarkLin You don't need to ask a new question for that. Just do a google search, [how to get current time with python](https://www.google.com/search?q=how+to+get+current+time+in+python&rlz=1C1GCEU_enUS819US819&oq=how+to+get+current+time+in+python&aqs=chrome..69i57j0l7.5280j1j7&sourceid=chrome&ie=UTF-8). From that, you get so many results. From that, you can set current time, then count up. – 10 Rep Jun 27 '20 at 15:02
  • @saad that's not what i want though, the internet is filled with setting current time while i want A TIMER A STOPWATCH, not current time, which i haven't been able to find, the answer you shared is the current time, which i have tried. – Mark Lin Jun 28 '20 at 01:33

2 Answers2

1

You can use something like this class:

import time

class StopWatch(object):

    def __init__(self):
        self.start_time = None
        self.stop_time = None

    def start(self):
        self.start_time = time.time()

    def stop(self):
        assert self.start_time
        self.stop_time = time.time()

    @property
    def time_elapsed(self):
        assert not self.stop_time, \
            "Can't check `time_elapsed` on an ended `Stopwatch`."

        if not self.start_time:
            #timer was not started.
            return 0
        
        return time.time() - self.start_time

    @property
    def total_run_time(self):
        assert self.start_time and self.stop_time
        return self.stop_time - self.start_time

    def __enter__(self):
        self.start()
        return self

    def __exit__(self, type, value, traceback):
        self.stop()
        if type:
            raise type

You can then use it like this:

m_sw = StopWatch()
    
m_sw.start()
print (m_sw.time_elapsed)
time.sleep(1)
print (m_sw.time_elapsed)

time.sleep(1)
m_sw.stop()
print(m_sw.total_run_time)
jvieira88
  • 115
  • 7
0

You can use datetime module:

from datetime import datetime
from tkinter import *
root = Tk()

class Start:
    def __init__ (self, master):
        self.master = master
        self.start_time = None
        self.end_time = None

    def timer(self):
        self.start_time = datetime.now()

    def end(self):
        if not self.start_time:
            raise ValueError("You  should call start() function first")
        
        self.end_time = datetime.now() - self.start_time
        print(self.end_time)


root.mainloop()

I do not know what you want to do when execution is over, so I assign self.end_time as time measured.

Andrey
  • 146
  • 1
  • 2
  • 8
  • from ```datetime.now()``` im assuming you're taking the current time? I want the time to be 00:00:00 so it's an actual stopwatch and not a clock. im only assuming because the code doesn't run. – Mark Lin Jun 27 '20 at 14:27