Search code examples
pythontkinterinputstopwatchnew-window

Stopwatch with tkinter?


I have taken on the task of making a rubik's cube timer using python and tkinter. I was wondering if there is a way to start/stop that stopwatch using the "enter" key or the space bar. I have been able to achieve this NOT using the tkinter window, but I cant figure out how to get tkinter to detect the key presses without having to change input feilds. The code I attached is the stopwatch function WITHOUT tkinter.

def stopwatch():
    import time
    print("")

    start = input("Press 'Enter' to start timer")

    begin = time.time()
    endtimer = input("Press 'Enter' to end the timer")
    end = time.time()

    time = end-begin
    time = float(round(time,3))
    text = ""
    if time >= 60.999:
        newTime = time/60
        text += ("The final time was " + str(newTime) + " minutes")
    else:
        text += ("The final time was " + str(time) + " seconds")

    return text

FILE USING TKINTER

from tkinter import *
from timer import *
from scrambler import *
from colors import *
window = Tk()
window.title("Bryson's Scrambler")
lbl = Label(window, text=sprint(valid(s)))
lbl.grid(column=0, row=0)
lbl2 = Label(window, text=stopwatch())
lbl2.grid(column=10, row=10)
window.mainloop()

Solution

  • I already had a small prototype for a stopwatch built, so just copying the code in here. Looks like it will solve your needs

    import tkinter as tk
    import time
    
    
    class Timer:
        def __init__(self):
            self.root = tk.Tk()
            self.sv = tk.StringVar()
            self.start_time = None
            self.is_running = False
    
            self.make_widgets()
            self.root.bind('<Return>', self.startstop)
            self.root.mainloop()
    
        def make_widgets(self):
            tk.Label(self.root, textvariable=self.sv, font='ariel 15').pack()
    
            btn_frame = tk.Frame(self.root)
            btn_frame.pack()
            tk.Button(btn_frame, text='Start', command=self.start).pack(side=tk.LEFT)
            tk.Button(btn_frame, text='Stop', command=self.stop).pack(side=tk.RIGHT)
    
        def start(self):
            if not self.is_running:
                self.start_time = time.time()
                self.timer()
                self.is_running = True
    
        def timer(self):
            self.sv.set(self.format_time(time.time() - self.start_time))
            self.after_loop = self.root.after(50, self.timer)
    
        def stop(self):
            if self.is_running:
                self.root.after_cancel(self.after_loop)
                self.is_running = False
    
        def startstop(self, event=None):
            if self.is_running:
                self.stop()
            else:
                self.start()
    
        @staticmethod
        def format_time(elap):
            hours = int(elap / 3600)
            minutes = int(elap / 60 - hours * 60.0)
            seconds = int(elap - hours * 3600.0 - minutes * 60.0)
            hseconds = int((elap - hours * 3600.0 - minutes * 60.0 - seconds) * 10)
            return '%02d:%02d:%02d:%1d' % (hours, minutes, seconds, hseconds)
    
    
    Timer()