GreenSaber
GreenSaber

Reputation: 1148

Tkinter timer to start at 0 on button click

I would like to create a timer that starts at 0 when a user presses a button and stop at whatever time it has displayed when the user presses the button again. So far, all of the questions user after that looks at the current time and updates in seconds from whatever time it is like so:

def timer(self):
    now = time.strftime("%H:%M:%S")
    self.label.configure(text=now)
    self.after(1000, self.timer)

But I would like to start at zero, display the minutes and seconds. Is there anyway to achieve this?

Upvotes: 0

Views: 5075

Answers (2)

PM 2Ring
PM 2Ring

Reputation: 55499

Here's a simple stopwatch GUI. There's some room for improvement. ;)

import tkinter as tk
from time import time

class Stopwatch:
    def __init__(self):
        root = tk.Tk()
        root.title('Stopwatch')

        self.display = tk.Label(root, text='00:00', width=20)
        self.display.pack()

        self.button = tk.Button(root, text='Start', command=self.toggle)
        self.button.pack()

        self.paused = True
        root.mainloop()

    def toggle(self):
        if self.paused:
            self.paused = False
            self.button.config(text='Stop')
            self.oldtime = time()
            self.run_timer()
        else:
            self.paused = True
            self.oldtime = time()
            self.button.config(text='Start')

    def run_timer(self):
        if self.paused:
            return
        delta = int(time() - self.oldtime)
        timestr = '{:02}:{:02}'.format(*divmod(delta, 60))
        self.display.config(text=timestr)
        self.display.after(1000, self.run_timer)

Stopwatch()

The toggle method toggles the stopwatch on or off. The run_timer method updates the display Label with the time since the timer started, in minutes & seconds. For more accuracy, reduce the .after delay to say, 500, or 100. That will do unnecessary (and invisible) updates to the Label, but the displayed time will be a little more accurate, and the GUI will feel a little more responsive.

Upvotes: 3

Joshua Nixon
Joshua Nixon

Reputation: 1425

import tkinter as tk
import time

class GUI:
    def __init__(self, master):
        self.root = master

        self.parent = tk.Frame(self.root)
        self.parent.pack(fill = tk.BOTH)
        self.parent.config(bg = "black")

        self.now         = time.time()
        self.buttonVar   = tk.IntVar()
        self.buttonCycle = False
        self.buttonVar.set(0)

        self.button = tk.Button(root,
                                textvariable = self.buttonVar,
                                command      = self.updateButton)
        self.button.pack(fill = tk.BOTH)

        self.button_cycle()


    def updateButton(self):
        if self.buttonCycle:
            self.buttonCycle = False
            self.now = time.time()
        elif not self.buttonCycle:
            self.buttonCycle = True

    def button_cycle(self):
        if self.buttonCycle:
            now            = time.time()
            timeDifference = int(now - self.now)
            self.buttonVar.set(timeDifference)
        self.root.after(1000, self.button_cycle)

root = tk.Tk()
myApp = GUI(root)
root.mainloop()

Upvotes: 1

Related Questions