0

I am making a timer with a for loop and showing it in a tkinter window, but as I execute the script, there is no pop-up. It is very probable that there is some error I don't know of, which is probably pretty simple and I don't see it

I tryed to move the for loop within the code to maybe resolve the problem but that was no help.

import tkinter
import time
root = tkinter.Tk()
label_1 = tkinter.Label(root, text="Start")
label_1.pack()
for i in range(1, 3600):
    label_1["text"] = i
    time.sleep(1)
root.mainloop()
Julian G.
  • 29
  • 1
  • 1
  • 6

1 Answers1

0

The window will show up after 3600 seconds. Dont wait 3600 seconds to see it, just change change to 5 seconds. The problem is you are doing label_1["test"] = i in the main thread, the same as the App. You need to do such task in another thread, otherwise such loop (due to time.sleep) will block the Application

import tkinter
import time
root = tkinter.Tk()
label_1 = tkinter.Label(root, text="Start")
label_1.pack()

import threading

def task():
    for i in range(1, 10):
        label_1["text"] = i
        time.sleep(1)

thread = threading.Thread(target = task)
thread.start()

root.mainloop()

Take a look at this post about Multi-threading

Let me know if this worked for you

JoshuaCS
  • 2,524
  • 1
  • 13
  • 16
  • Thanks. It worked. I'm going to research the Multi-thread subject. I am no expert and I have never heared of this. – Julian G. Jan 04 '19 at 20:17