0

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()
Gadawg099
  • 117
  • 1
  • 3
  • 15
  • probably all GUI frameworks don't update widget at once but they wait for end of your function to redraw all widgets in window at the same time - this way it needs less drawing and window doesn't flick/blink. – furas Nov 02 '21 at 23:06
  • instead of `command=lambda: self.counter()` you can do `command=self.counter` - without `lambda` and without `()`. – furas Nov 02 '21 at 23:07

0 Answers0