Tkinter App becomes laggy after several minutes

Hi there,

Thanks for taking the time to read this. I made a Tkinter app that does some cool stuff. When you first run it, it runs great, responsive, and everything like that.

After several minutes, if you minimize the app and then re-maximize it, the following issues will happen:

  • frames will show up as black rectangles for several seconds
  • if you grab the title bar with the mouse and drag across the screen, it is very laggy and choppy, like 3 FPS
  • clicking “X” to close the app is slow to respond (2 to 5 second lag)

It seems that the longer the app runs, the longer the lag gets. It’s almost like there is something in the program that adds to the latency the longer it is run.

I tried the following things:

  • I checked all arrays which use .append() instructions, and made sure that there aren’t runaway arrays that just get filled endlessly
  • I used Text() widgets, one of which was populating endlessly, so I now clear it after every 100 lines
  • I might try making a much simpler app (Maybe a root, label, and a single scale) and see if the sort of time-based-breakdown / latency happens with a simpler app, or if it’s something in my program.

Any help that anyone can provide would be greatly appreciated, thanks.

You’ll need to post your code, but I’m wondering if perhaps there’s something that’s adding more event handlers than are needed?

Gotcha, could be the case. Is the best way to share my code to do so through Github?

Thanks

Ideal would be posting it right here (in triple backticks), but ideal is also that it be short enough to post. If it isn’t, it’s rather hard to analyze suitably. So if you don’t feel comfortable with the amount of code you’re dumping in here, it’s probably worth going with your plan of making a simpler app and seeing if you can recreate the issue.

Are you creating a lot of widgets dynamically perhaps? Those don’t get destroyed if you just remove them from a window, only once you explicitly call destroy(). You might have ended up with a whole heap of hidden widgets that are still alive.

1 Like

Loading a text widget with long lines over, say, 1000 chars, slows response down. The longer the slower. 100000 chars may close to freeze it. (It does in IDLE.) On the other hand, 500000 40-char lines is not a problem. Without long lines, IDLE seems to run indefinitely.

I would disable various large chunks of your app, then smaller chunks, to try to find one thing that unblocks your app.

1 Like