【问题标题】:How can I set Kivy to auto-animate the position of a shape after an interval?如何将 Kivy 设置为在间隔后自动为形状的位置设置动画?
【发布时间】:2021-06-22 11:23:49
【问题描述】:

如何让 Kivy 在间隔后为 Rectangle 的位置设置动画?目前,它只是为 POSITION 1 设置动画。

但是,我希望它在动画到位置 2 之前等待 3 秒,再等待 3 秒,然后动画到位置 3

我怎样才能做到这一点?我还是个编码新手。

(未使用 .kv 文件)

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.graphics import Rectangle
from kivy.graphics import Color
from kivy.animation import Animation
from kivy.core.window import Window
from kivy.clock import Clock


class MyGrid(AnchorLayout):

    def __init__(self, **kwargs):
        super(MyGrid, self).__init__(**kwargs)
        self.inside = GridLayout()
        self.inside.cols = 12

        for letter in "CANVAS":
            self.inside.add_widget(Label(text=str(letter), color=[1, 1, 1, 1], font_size='80sp', ))

        self.add_widget(self.inside)

        with self.canvas:
            # POSITIONS
            self.positionLeft_1 = 50
            self.positionLeft_2 = 150
            self.positionLeft_3 = 250

            Color(1, 1, 1, 1, mode='rgba')
            self.line_Left = Rectangle(pos=(300, 215), size=(1, 100))

            animate_left_line_01 = Animation(pos=(self.positionLeft_1, 215), t='out_circ')
            animate_left_line_02 = Animation(pos=(self.positionLeft_2, 215), t='out_circ')
            animate_left_line_03 = Animation(pos=(self.positionLeft_3, 215), t='out_circ')

            # START ANIMATION
            # MOVE TO POSITION 1
            animate_left_line_01.start(self.line_Left)

            # MOVE TO POSITION 2 AFTER 3 SECONDS
            animate_left_line_02.start(self.line_Left)
            
            # MOVE TO POSITION 3 AFTER 3 SECONDS
            animate_left_line_03.start(self.line_Left)



    def move(self):
        pass

    def update(self, dt):
        self.move()


class MainApp(App):

    def build(self):
        self.game = MyGrid()
        Clock.schedule_interval(self.game.update, 1.0 / 60.0)
        return self.game

    Window.size = (300, 530)
    Window.clearcolor = (0, 0, .5, 1)


if __name__ == "__main__":
    MainApp().run()

【问题讨论】:

    标签: python kivy kivy-language kivymd


    【解决方案1】:

    一种方法是使用Clock.schedule_once() 来安排每个动画:

    class MyGrid(AnchorLayout):
    
        def __init__(self, **kwargs):
            super(MyGrid, self).__init__(**kwargs)
            self.inside = GridLayout()
            self.inside.cols = 12
    
            for letter in "CANVAS":
                self.inside.add_widget(Label(text=str(letter), color=[1, 1, 1, 1], font_size='80sp', ))
    
            self.add_widget(self.inside)
    
            # POSITIONS
            self.positionLeft_1 = 50
            self.positionLeft_2 = 150
            self.positionLeft_3 = 250
    
            with self.canvas:
                Color(1, 1, 1, 1, mode='rgba')
                self.line_Left = Rectangle(pos=(300, 215), size=(1, 100))
    
    
            # START ANIMATION
            Clock.schedule_once(self.anim1)
            Clock.schedule_once(self.anim2, 3)
            Clock.schedule_once(self.anim3, 6)
    
    
    
        def anim1(self, dt):
            # MOVE TO POSITION 1
            animate_left_line_01 = Animation(pos=(self.positionLeft_1, 215), t='out_circ')
            animate_left_line_01.start(self.line_Left)
    
        def anim2(self, dt):
            # MOVE TO POSITION 2
            animate_left_line_02 = Animation(pos=(self.positionLeft_2, 215), t='out_circ')
            animate_left_line_02.start(self.line_Left)
    
        def anim3(self, dt):
            # MOVE TO POSITION 3
            animate_left_line_03 = Animation(pos=(self.positionLeft_3, 215), t='out_circ')
            animate_left_line_03.start(self.line_Left)
    

    【讨论】:

    • 约翰,你太棒了!!这就像魔术一样。我非常感谢您的帮助!我最近才开始学习编码,我还有很多东西要学。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    • 2010-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多