0

I created a timer but for some reason the timer randomly stops updating until I click the tkinter window then it starts to update again. This happens about every minute or two. Here is my code:

from tkinter import *
from threading import Thread


tk = Tk()
tk.attributes('-alpha',1)
tk ['bg']='#302F2F'
tk.title('')
tk.wm_attributes('-topmost', 1) # put the window to front

def timer():
    
    while True:
        sleep(0.009)
        ...
        #cut out all the stuff of creating the time but here is how i did it
        
        label['text'] = (ftime2) 
        label['fg'] = colorfortext
        label2['text'] = (ftime) 
        label2['fg'] = colorfortext
        label3['text'] = (numberofworlds) 
        label3['fg'] = 'blue'
        

label = Label(tk, '', font=('Arial', 30),bg='#302F2F')
label.grid(columnspan=2) 
label2 = Label(tk, '', font=('Arial', 30),bg='#302F2F')
label2.grid(columnspan=2) 
label3 = Label(tk, '', font=('Arial', 30),bg='#302F2F')
label3.grid(columnspan=2) 

timer_thread = Thread(target=timer)
timer_thread.start() 

tk.mainloop() 
  • You shouldn't use `while True` loops or `time.sleep` when using `tkinter`, unless you really know what it can do. Look at `.after` scripts instead. – TheLizzard Jul 24 '21 at 22:28
  • Also you shouldn't call `tkinter` functions from threads other than the one where you created the `Tk()` window – TheLizzard Jul 24 '21 at 22:31
  • this was a base of tkinter window i was given by someone who had the while loop in the thing. I took away the time.sleep and it still did it. I deleted a lot of script but all it is is creating the strings to put in the tkinter text. – George Davis-Diver Jul 24 '21 at 22:59
  • I don't know who gave you the code (that you modified), but `tkinter` isn't thread safe. There are many cases where `tkinter` can crash or cause problems if it's used inside another thread. [This](https://stackoverflow.com/a/2401181/11106801) is one way to create a timer without threads/sleep (you will need to change the `Tkinter` into `tkinter` because you are using python 3). – TheLizzard Jul 24 '21 at 23:03

0 Answers0