Trying to make a simple move in tkinter:
import tkinter as tk class GameApp(object): """ An object for the game window. Attributes: master: Main window tied to the application canvas: The canvas of this window """ def __init__(self, master): """ Initialize the window and canvas of the game. """ self.master = master self.master.title = "Game" self.master.geometry('{}x{}'.format(500, 500)) self.canvas = tk.Canvas(self.master) self.canvas.pack(side="top", fill="both", expand=True) self.start_game()
Where self.momentum
is an array containing 2 integers: one for movement x and the other for movement y. However, the actual movement of the rectangle is very slow (about 5 movements per second), and the time self.window.master.after()
has no effect.
Earlier in another tkinter project, I managed to get a very responsive tkinter movement, so I'm just wondering if there is a way to reduce the update time of the movement in this case, using either a different OOP style or a completely different code.
UPDATE: It turns out that the time in the .after()
method has a value, and it actually drains in real time of the method. After using timeit
while calling the method, I got this output:
>>> print(timeit.timeit("(self.window.master.after(2, self.mom_calc))", number=10000, globals={"self":self})) 0.5395521819053108
So, I think the real question is: why does this .after()
method take so long?
UPDATE 2: Tested on multiple computers, movement is still slow on any platform.
source share