【问题标题】:How to switch images after animation?动画后如何切换图像?
【发布时间】:2019-04-14 17:44:00
【问题描述】:

我有几个问题:

  1. 我有旋转球的动画,它应该始终在屏幕顶部,并且屏幕应该始终只显示它的一半。我可以做到这一点,但只能通过单击按钮调用将球带到正确位置的函数。我需要球总是在正确的位置,不仅然后我点击按钮。我尝试使用 init 函数,但它总是给我这个错误:'super' object has no attribute 'getattr'。我也尝试使用 getattr 而不是 init,但它没有将球带到正确的位置。

  2. 所以我想单击一个按钮,然后球开始旋转几秒钟。当它停止时,我希望球是另一种颜色,甚至是球的另一种图像。我尝试使用 on_complete 但我不明白应该在哪里使用它。

我的主py文件:

from kivy.app import App
from kivy.uix.screenmanager import Screen
from kivy.lang import Builder
from kivy.animation import Animation
from kivy.properties import NumericProperty

class OpenScreen(Screen):
    angle = NumericProperty(0)

    #Trying to make __init__ function
    #def __init__(self, **kwargs):
        #super(OpenScreen, self).__init__(**kwargs)
        #y = self.height
        #y1 = self.width / 2
        #self.ids.test.pos = 0, y - y1

    #Function of taking ball in the right place and spinning it
    def z(self):
        anim = Animation(angle=360, duration=0.5)
        anim += Animation(angle=360, duration=0.5)
        anim.start(self)
        y = self.height
        y1 = self.width / 2
        self.ids.test.pos = 0, y - y1
        self.ids.test.source = 'redball.png'


    def on_angle(self, item, angle):
        if angle == 360:
            item.angle = 0


...screens classes...


GUI = Builder.load_file('game.kv')
class GameApp(App):
    def build(self):
        return GUI

    def change_screen(self, screen_name):
        screen_manager = self.root.ids['screen_manager']                
        screen_manager.current = screen_name



GameApp().run()

我的 Openscreen.kv 文件:

#:kivy 1.10.1

<OpenScreen>:
    FloatLayout:
        canvas:
            Rectangle:
                size: self.size
                pos: self.pos
                source: 'bg.png'
        Image:
            id: test
            size_hint_y: None
            height: self.width
            source: 'blueball.png'
            allow_stretch: True
            keep_ratio: False
            canvas.before:
                PushMatrix
                Rotate:
                    angle: root.angle
                    axis: 0, 0, 1
                    origin: self.center
            canvas.after:
                PopMatrix
        Button:
            text: "spin"
            font_size: self.height - 29
            valign: 'middle'
            halign: 'center'
            padding: 2,2
            size_hint: .7, .1
            pos_hint:{"x":.15, "y":.585}
            background_color: 0,0,0,0
            on_press:
                root.z()
....

我该怎么做?

【问题讨论】:

  • 尝试在您的__init__() 中使用Clock.schedule_once() 安排呼叫设置球。

标签: python oop animation kivy


【解决方案1】:

您可以使用Clock.schedule_once() 来启动动画:

def __init__(self, **kwargs):
    super(OpenScreen, self).__init__(**kwargs)
    Clock.schedule_once(self.z)

您需要将dt 参数添加到z() 方法并利用on_complete 事件作为:

def z(self, dt):
    anim = Animation(angle=360, duration=0.5)
    anim += Animation(angle=360, duration=0.5)
    anim.bind(on_complete=self.switch_source)
    anim.start(self)

然后使用switch_source() 方法作为:

def switch_source(self, *args):
    y = self.height
    y1 = self.width / 2
    self.ids.test.pos = 0, y - y1
    self.ids.test.source = 'redball.png'

【讨论】:

  • 是的,但这并不是我想要的。所以我把它变成了: def __init__(self, **kwargs): super(OpenScreen, self).__init__(**kwargs) Clock.schedule_interval(self.switch_source, 0) def switch_source(self, *args): y = self.height y1 = self.width / 2 self.ids.test.pos = 0, y - y1 它奏效了。还是谢谢你!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-22
  • 1970-01-01
  • 2012-02-29
  • 1970-01-01
  • 2014-02-17
  • 1970-01-01
相关资源
最近更新 更多