簡體   English   中英

在Kivy上對畫布行進行動畫處理

[英]Animate canvas Line on 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's Clock 然后Kivy將能夠更新屏幕。

但是您可能應該只看一下Kivy的Animation ,它可以做到更好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM