I am trying to use multithreading to show a loading wheel while doing some background calculations. I try to start the multithreaded function, and then run a timer to simulate the calculations. The problem is the thread first starts running when the timer is over, even though I am starting the thread first...
from tkinter import *
from tkinter import messagebox
import time
from threading import *
from PIL import Image, ImageTk
# Create Object
root = Tk()
# Set geometry
root.geometry("400x400")
flag = True
# use threading
def threading():
t1=Thread(target=work)
t1.start()
time.sleep(10)
# work function
def work():
print("sleep time start")
image = Image.open("Static/spinner0.png")
img = ImageTk.PhotoImage(image)
lab = Label(root,image=img)
lab.pack()
while flag:
for i in range(8):
image = Image.open("Static/spinner"+str(i)+".png")
img = ImageTk.PhotoImage(image)
lab['image'] = img
time.sleep(0.2)
#return False
print("sleep time stop")
def on_closing():
if messagebox.askokcancel("Quit", "Do you want to quit?"):
flag = False
root.destroy()
# Create Button
Button(root,text="Click Me",command = threading).pack()
# Execute Tkinter
root.protocol("WM_DELETE_WINDOW", on_closing)
root.mainloop()