Tkinter long-running window freezes

Richard Damon Richard at Damon-Family.org
Thu Feb 25 11:06:05 EST 2021


On 2/24/21 6:35 AM, John O'Hagan wrote:
> Hi list
>
> I have a 3.9 tkinter interface that displays data from an arbitrary
> number of threads, each of which runs for an arbitrary period of time.
> A frame opens in the root window when each thread starts and closes
> when it stops. Widgets in the frame and the root window control the
> thread and how the data is displayed.
>
> This works well for several hours, but over time the root window
> becomes unresponsive and eventually freezes and goes grey. No error
> messages are produced in the terminal.
>
> Here is some minimal, non-threaded code that reproduces the problem on
> my system (Xfce4 on Debian testing):
>
> from tkinter import *
> from random import randint
>
> root = Tk()
>
> def display(label):
>     label.destroy()
>     label = Label(text=randint(0, 9))
>     label.pack()
>     root.after(100, display, label)
>
> display(Label())
> mainloop()
>  
> This opens a tiny window that displays a random digit on a new label
> every .1 second. (Obviously I could do this by updating the text rather
> than recreating the label, but my real application has to destroy
> widgets and create new ones).
>
> This works for 3-4 hours, but eventually the window freezes.
>
> The process uses about 26 Mb of memory at first, and this gradually
> increases to around 30 or so by the time it freezes.
>
> Any ideas what could be causing this, or even how to approach debugging
> or workarounds?
>
> Thanks
>
> --
>
> John

One thought is that repeatedly destroying and recreating a label might
be leaking a resource. One option would be to change the code to just
update the label rather than recreating it each time.  Simplest is
probably to link the Label to a StringVar instead of a fixed text and
updating the variable to change the text. You can also (I believe) go
into the Label and change the text it has with a configuration call.

-- 
Richard Damon



More information about the Python-list mailing list