0

I have a simple tool which is crashing every few days. I simplified it to show the problem.

Short description:

A counter is incremeted and displayed with Tk as a Label.

Problem:

There must be a memory leak - with this demo script I can crash a 1 GB Raspberry in 5 - 10 minutes...

The problem is the label-update (labeltext.set(counter))

Without updating the label it works for hours without a problem.

But why? I really don't understand it...

Thank you.

from tkinter import *

root = Tk()

counter=1
labeltext = StringVar()

root.config(cursor="none")
root.configure(background='black')
root.geometry("1920x1080") # set explicitly window size
frame_0= Frame(root, padx=5, pady=5, background='black' )
label1 = Label(frame_0, font=('LCD Display Grid', 170, ''), fg='lightgreen',bg='black', pady=30, textvariable=labeltext)
frame_0.grid( row=0, columnspan=3,pady=10, padx=0)
label1.pack()
root.grid_columnconfigure(0, weight=1)


class perpetualTimer():

    def __init__(self, t, hFunction):
        self.t = t
        self.hFunction = hFunction
        self.thread = Timer(self.t, self.handle_function)

    def handle_function(self):
        self.hFunction()
        self.thread = Timer(self.t, self.handle_function)
        self.thread.start()

    def start(self):
        self.thread.start()

    def cancel(self):
        self.thread.cancel()


def printer():
    global counter
    counter = counter + 1
    labeltext.set(counter)
    
t = perpetualTimer(0.001, printer)
t.start()

root.mainloop()
Thingamabobs
  • 7,274
  • 5
  • 21
  • 54
  • tkinter has a own event manager, try this instead. https://stackoverflow.com/questions/63118430/create-a-main-loop-with-tkinter/63118515#63118515 – Thingamabobs Jul 31 '20 at 11:39
  • tkinter isn't thread safe. Calling tkinter functions from more than one thread is not guaranteed to work. – Bryan Oakley Jul 31 '20 at 15:07

0 Answers0