Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter in Python - Is it supposed to be slow or is the bottleneck somewhere else in the program?

In school, we have to create a game using Python and Tkinter as a group project and since the program is getting rather large, I want to know if my problem is caused by Tkinter itself before trying to search for problems in the program.

The problem is, when we start creating units, when there are multiple units to display at the same time (let's say 80) created using the create_rectangle method, if we start moving them all around, it gets really choppy. On the part of the game we're displaying at that time, there are a couple of elements (some using small gifs) for the multiple menus, and on the canvas there are the units I just mentionned and some small buildings also using some small gifs.

We use the after method to callback a method that deletes everything on the canvas and then it redraws all the buildings and units at their correct positions every 50 milliseconds.

Should that be something that is easily handled by Tkinter or is the problem in our program itself?

like image 389
Adam Smith Avatar asked Dec 27 '22 11:12

Adam Smith


1 Answers

Nobody will be able to answer to this specific question, because it depends on a lot of factors. When you have a performance issue, you should measure the time you spend in each function to spot where are the bottlenecks. It is called profiling, and you have a nice tutorial here for the python built in profiler: http://docs.python.org/library/profile.html

You are looking for two kind of information:

  • the functions you spend the most time inside, including calls to other function. Obviously, the first one in the list is your main, since all your program runs inside it. Still, you may spot a function which consume more than you think

  • the functions you spend the most time inside, excluding calls to other functions. There you will have basic functions. Those which are called often, etc. Again, if some results surprise you: investigate with your own eyes.

if you find that most of the time is spend in tkinter internals, then you may use it the wrong way. Try to isolate a small stand alone program making calls to tkinter and showing a similar performance issue, and post it here. The profiling information should be useful for this task.

like image 103
Simon Bergot Avatar answered Dec 31 '22 12:12

Simon Bergot