I've been working on a Stopwatch project with UI. I built the UI using my novice skills in TKinter. I am trying to find a way to change the text of the label which prints the new time every second.
My first idea was to reload the frame every time the text is supposed to change and repack it and run it to the mainloop. Is there a way to do this, or is there another way it is possible to achieve this?
I had tried to do this using the following code:
import tkinter as tk
import time as t
hour = 0
minute = 0
second = 0
ptime = [hour, minute, second]
time = ':'.join(ptime)
window = tk.Tk()
window.title("Stopwatch")
###window.minsize(250,250)
###window.maxsize(500,500)
window.resizable(False, False)
window.geometry("250x120")
window.attributes('-topmost', 1)
window.iconbitmap("clock.ico")
timeLabel = tk.Label(window, text = time, font = (None, 40))
running = False
def resetf():
print("Stopwatch Resetting")
ptime = [str(0), str(0), str(0)]
time = ':'.join(ptime)
timeLabel.config(text = time)
timeLabel.pack()
print("Stopwatch Reset")
def tgl():
global running
if running:
print("Stopwatch Ending")
print(running)
running = False
print(running)
print("Stopwatch Not Running")
elif not running:
print("Stopwatch Starting")
print(running)
running = True
print(running)
print("Stopwatch Running")
startstop = tk.Button(window, text = "Start/Stop", command = tgl)
startstop.config(width = 250)
reset = tk.Button(window, text = "Reset", command = resetf)
reset.config(width = 250)
timeLabel.pack()
startstop.pack()
reset.pack()
window.mainloop()
while running:
t.sleep(1)
second = second + 1
if second == 60:
minute += 1
second = 0
if minute == 60:
hour += 1
ptime = [str(hour), str(minute), str(second)]
time = ':'.join(ptime)
timeLabel.config(text = time)
timeLabel.pack()
window.mainloop()
This is my entire code so far. What should I change to make it so that every second the label's text changes?