I was working on making something that needed a timer involved, so I tried making one using a for loop, which works, but only at the end of the for loop. I feel like this should be able to change the stringVar every time it makes the loop. But for some reason it only changes the text at the end, and with the last count. I can't quite figure out why it doesn't work properly.
import time
import tkinter as tk
from tkinter import ttk
class Test:
def __init__(self):
self.root = tk.Tk()
self.win = ttk.Frame(self.root, padding=100)
self.win.grid()
self.text = tk.StringVar()
ttk.Label(self.win, textvariable=self.text).grid(column=1, row=1)
ttk.Button(self.win, text="test", command=lambda: self.counter()).grid(column=1, row=2)
self.root.mainloop()
def changeText(self, string):
self.text.set(string)
def counter(self):
count = 0
for i in range(1, 101):
time.sleep(1)
count += 1
self.changeText(str(count))
if __name__ == "__main__":
Test()