【问题标题】:Cant get kivy animation to work with pos_hint无法让 kivy 动画与 pos_hint 一起使用
【发布时间】:2019-09-25 06:50:49
【问题描述】:

我想要一个小部件随动画功能一起移动,而不是使用 pos,我想使用 pos_hint,因为它简化了很多事情。动画开始时,出于某种原因,它会将 pos_hint 设置为空 {}。我认为这与将 pos_hint 正确绑定到按钮有关,或者 anim 没有正确发送 pos_hint 值。

我已经尝试使用 pos 并且效果很好。

from kivy.graphics import *
from kivy.uix.floatlayout import FloatLayout
from kivy.animation import *
from kivy.lang.builder import Builder

Builder.load_string("""
<FloatLayout>:
    button:
        size_hint: 1,1/4
        pos_hint: {'center_y':0.5,'right':0}
        canvas:
            Color:
                rgba: [1,0,1,1]
            Rectangle:
                pos: self.pos
                size: self.size
""")

class button(FloatLayout):
    def __init__(self, **kwargs):
        super(ButtonSr, self).__init__(**kwargs)

        def checkpos(inst,val):
            print(val) #prints {} as soon as self.startanim() is fired and 
                        nothing else. pos_hint stays as {}

        self.bind(pos_hint=checkpos)
        self.startanim()

    def startanim(self):
        anim = Animation(pos_hint={'center_x':0.5},d=2)
        anim.start(self)

我希望按钮从屏幕外移动到所选的 pos_hint

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    尝试设置一个单独的变量来定义您要在 kv 代码中更改的位置提示。然后在您的动画中,更改该变量而不是尝试更改 pos_hint 属性。

    千伏:

    my_y_hint: .5
    pos_hint: {"center_y": self.my_y_hint, "right": 0}
    

    然后在你的动画中:

    anim = Animation(my_y_hint=1)
    

    我最近还发了一个quick video tutorial 来说明如何做到这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-17
      • 1970-01-01
      • 2021-05-26
      • 2020-11-09
      • 1970-01-01
      相关资源
      最近更新 更多