python - Kivy 上的动画 Canvas 线

标签 python animation canvas line kivy

我有一个包含 x 和 y 位置的数组。我想显示这些点为每个点连续连接一条线,然后创建一个动画。这就像一条路径追踪,其踪迹就是线。我正在使用 python-kivy 来尝试显示它。

我在 Google 上找不到任何帮助。

有一个按钮可以触发此动画。

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.graphics.vertex_instructions import Line
from kivy.graphics.context_instructions import Color
from time import sleep

x_moves = [250, 250.4305, 249.8804, 246.0923, 239.7496, 233.8188, 225.7797, 215.8385, 205.8413, 196.6497, 189.7026, 181.2445, 174.9816, 171.9882, 166.1171, 161.6505, 159.9929, 161.1338, 164.853, 168.2874, 170.768, 178.6918, 184.5233, 190.0262, 195.607, 202.0255, 210.5954, 216.1031, 219.6285, 224.9134, 230.2314, 237.7017, 243.7408, 250.5839, 256.2949]
y_moves = [250, 240.0093, 230.0244, 220.7697, 213.0386, 204.9872, 199.0396, 197.9567, 197.7209, 201.6598, 208.8527, 214.1875, 221.9834, 231.5249, 239.62, 248.567, 258.4287, 268.3634, 277.6461, 287.0379, 296.7253, 302.8256, 310.9492, 319.299, 327.5969, 335.2652, 340.4185, 348.7651, 358.1231, 366.6125, 375.0812, 381.7291, 389.6996, 396.9915, 405.2003]

class my_App(App):
    def build(self):
        self.widget = Widget()
        self.widget.on_touch_down = self.touch
        with self.widget.canvas:
            Color(0, 1, 0, 1) #just initial config
            Line(points = [0,0,500,0,500,500,0,500], close = True) #just initial config
        return self.widget

    def touch(self, touch): #executes the animation
        pts = []
        for i in range(len(x_moves)):
            pts.append(x_moves[i])
            pts.append(y_moves[i])
            self.widget.canvas.add(Line(points = pts))
            sleep(0.1)

if __name__ == '__main__':
    obj = my_App()
    obj.run()

这是我的代码,不起作用。但这就是想法。

最佳答案

您正在阻止 UI 更新,因为您的函数未返回并且您使用了 sleep()。由于显而易见的原因,Kivy 在您的代码运行时无法执行任何操作。如果您想在运行更多代码之前等待,可以使用 Kivy 的 Clock为了这。然后 Kivy 将能够更新屏幕。

但是你应该看看 Kivy 的 Animation ,它是为了更好地做到这一点而构建的。

关于python - Kivy 上的动画 Canvas 线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26242071/

相关文章:

python - 在 Python 中处理对数空间中的矩阵乘法

python - 在 Pandas 中循环清理多个文档并将它们保存到一本书中

cocoa - 自定义 NSAnimationCurve

javascript - HTML5 Canvas 更快 fillText() vs drawImage()

javascript - 调用canvas.toDataURL时,为什么rasterizeHTML.drawHTML部分是黑色的?

javascript - js渲染循环一旦满足条件就会中断

python - python中的多进程会重新初始化全局变量吗?

python - 在不丢失数据的情况下更新字典

javascript - 动画宽度以适合内容

javascript - 如何让 jQuery 动画只工作一次?