python - 为什么 Tkinter 中的这个形状更新缓慢?

标签 python tkinter tkinter-canvas

尝试在 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()

    #----------------------------------------------#


    def start_game(self):
        """
        Actual loading of the game.
        """

        player = Player(self)

    #----------------------------------------------#

#----------------------------------------------#


class Player(object):
    """
    The player of the game.

    Attributes:
        color: color of sprite (string)
        dimensions: dimensions of the sprite (array)
        canvas: the canvas of this sprite (object)
        window: the actual game window object (object)
        momentum: how fast the object is moving (array)
    """


    def __init__(self, window):

        self.color = ""
        self.dimensions = [225, 225, 275, 275]
        self.window = window
        self.properties()

    #----------------------------------------------#

    def properties(self):
        """
        Establish the properties of the player.
        """

        self.color = "blue"
        self.momentum = [5, 0]

        self.draw()
        self.mom_calc()

    #----------------------------------------------#

    def draw(self):
        """
        Draw the sprite.
        """

        self.sprite = self.window.canvas.create_rectangle(*self.dimensions, fill=self.color, outline=self.color)

    #----------------------------------------------#


    def mom_calc(self):
        """
        Calculate the actual momentum of the thing
        """

        self.window.canvas.move(self.sprite, *self.momentum)
        self.window.master.after(2, self.mom_calc)

    #----------------------------------------------#

#----------------------------------------------#


root = tk.Tk()

game_window = GameApp(root)

其中 self.momentum 是一个包含 2 个整数的数组:一个用于 x 运动,另一个用于 y 运动。然而,矩形的实际移动速度非常慢(每秒大约移动 5 次),self.window.master.after() 时间似乎没有效果。

之前在另一个 tkinter 项目上,我已经设法获得真正响应的 tkinter 运动,所以我只是想知道在这种情况下是否有一种方法可以通过使用不同风格的 OOP 来最小化运动更新时间,或者只是完全不同的代码。

更新:事实证明 .after() 方法中的时间确实很重要,它实际上叠加在方法的实际时间上。在使用 timeit 计时调用方法后,我得到了这个输出:

>>> print(timeit.timeit("(self.window.master.after(2, self.mom_calc))", number=10000, globals={"self":self}))
0.5395521819053108

所以我想真正的问题是:为什么 .after() 方法需要这么长时间?

更新 2:在多台计算机上测试,在任何平台上移动仍然缓慢。

最佳答案

“默认的 Windows 定时器分辨率是 ~15ms。尝试每 1ms 触发一个定时器不太可能按照你想要的方式工作,而且对于游戏来说可能是完全没有必要的(运行 60FPS 的显示器仅每 ~16ms 更新一次) . 参见 Why are .NET timers limited to 15 ms resolution?

Python - tkinter call to after is too slow 找到了解决方案Andrew Medico 给出了很好的答案(在评论中)。

关于python - 为什么 Tkinter 中的这个形状更新缓慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42589881/

相关文章:

python - Numpy:在新维度中堆叠矩阵的副本

python - 使用 Snack 在 python 中播放 WAV 文件

python - Tkinter - 自动滚动到底部

python - 为什么 print ("{}").format() 有效语法?

python - 如何在 Python 的初始化程序中访问类变量?

python - 我怎样才能向量化这个 python 计数排序,使其绝对尽可能快?

tkinter 包几何形状 : 1 column with 4 radio buttons in one row?

python - 删除使用图像创建的 tkinter 按钮的边框

windows - 显示窗口后如何从 Tkinter 中删除 Canvas

python - 将鼠标悬停在饼图上时更改饼图的颜色