I've created this code that updates a label every second to indicate that something is loading (run the code to see what I mean). I'm using the threading module with tkinter but I feel like there must be a more efficient way to do this.
Here is my code:
from tkinter import *
from time import sleep
import threading
root = Tk()
new_var = StringVar()
new_var.set('Loading')
def change_text():
array = [".", "..", "...", ""]
while True:
for num in range(4):
sleep(1)
new_var.set(f"Loading{array[num]}")
root.update_idletasks()
l = Label(root, textvariable = new_var)
l.pack()
Loading_animation = threading.Thread(target=change_text)
Loading_animation.start()
root.mainloop()
Also, if there isn't a better way to do this how do I prevent the error that I keep receiving whenever I close the root window?
Thank you!