import tkinter as tk
import time
timer = 0
def startTimer():
if timer == 0:
time.sleep(1)
elif (running):
global timer
timer += 1
timeText.configure(text=str(timer))
window.after(1000, startTimer)
def start():
global running
running = True
def stop():
global running
running = False
running = True
window = tk.Tk()
timeText = tk.Label(window, text = '0', font=("Helvetica", 80))
timeText.pack()
startButton = tk.Button(window, text = 'Start', bg='yellow', command=start)
startButton.pack(fill=tk.BOTH)
stopButton = tk.Button(window, text = 'Stop', bg='yellow', command=stop)
stopButton.pack(fill=tk.BOTH)
startTimer()
window.mainloop()
I wanted to make a stopwatch with python but I have question to you to solve this problem. when I execute this, I can find
line 10
global timer
^
SyntaxError: name 'timer' is used prior to global declaration
this error.
I tried to make this stopwatch to start without I press start button. And, I want this stopwatch to start at 0.
Can you change my code to solve the problem?
global timerto the line beforeif timer==0instartTimerfunction