I’m new to tkinter and have traced a memory leak in a project I’m doing down to a clock in my code. It turns out the memory leak happens when updating a label, the simplest example I’ve got it down to in code is:
import Tkinter as tk
class Display:
def __init__(self, master):
self.master = master
self.tick()
def tick(self):
self.label = tk.Label(self.master, text = 'a')
self.label.place(x=0,y=0)
self.master.after(50, self.tick)
root = tk.Tk()
disp = Display(root)
If somebody could tell me why this leaks memory I’d be grateful.
Thanks, Matt
The problem appears to be that you are creating Labels without destroying them. Each time you create a new label and place it over the old one, so it is still being referenced and thus can’t be garbage collected.
Here is a slightly revised version that doesn’t leak….