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

At school, we need to create a game using Python and Tkinter as a group project, and as the program gets quite large, I want to know if Tkinter itself caused my problem before trying to find problems in the program.

The problem is that when we start creating units, when several units are displayed simultaneously (say 80) created using the create_rectangle method, if we start moving them around, it becomes really mutable. On the side of the game that we are showing at that time, there are several elements (some of which use small gifs) for several menus, and on the canvas there are units that I just mentioned, and some small buildings also use some small GIFs.

We use the after method to call back a method that removes everything on the canvas, and then it redraws all the buildings and units in their correct positions every 50 milliseconds.

Should there be something that Tkinter easily handles or is this a problem in our program itself?

+4
source share
2 answers

No one will be able to answer this specific question, because it depends on many factors. When you have a performance problem, you need to measure the time you spend on each function to determine where the bottlenecks are. This is called profiling, and you have a good tutorial for python built into the profiler: http://docs.python.org/library/profile.html

You are looking for two types of information:

  • functions that you spend most often inside, including calls to another function. Obviously, the first in the list is your main one, since your entire program works inside it. However, you may find a function that consumes more than you think.

  • functions that you spend the most time, with the exception of calls to other functions. There you will have the main functions. Those that are often called, etc. Again, if some of the results surprise you: explore with your own eyes.

if you find that most of the time is spent on the internal parts of tkinter, you may use it incorrectly. Try isolating a small stand-alone program that calls tkinter calls and shows a similar performance issue and publishes it here. Profiling information should be useful for this task.

+5
source

Tk may work moderately well, but for writing a game you will probably get much better performance out of the box if you use a library like http://pygame.org

+3
source

Source: https://habr.com/ru/post/1379742/


All Articles