【问题标题】:Binding button to function after adding button with Kivy language使用 Kivy 语言添加按钮后绑定按钮功能
【发布时间】:2016-07-02 21:39:42
【问题描述】:

我试图弄清楚如何将使用 Kivy 语言布局的按钮绑定到函数。在用 Python 语言布置按钮时,我见过很多 answers。但是,一旦一切就绪并且您现在通过继承自 Button 的自定义类进行引用,该怎么办?

按下时,下面的代码会抛出错误TypeError: show() takes 1 positional argument but 2 were given 并使程序崩溃。

class TimerButton(ButtonBehavior, Image):
    timer_container = ObjectProperty(None)
    client_scoreboard = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(TimerButton, self).__init__(**kwargs)
        self.bind(on_press=self.show)
        self.bind(on_release=self.changeImage)

    def show(self):
        print('hi there')
        self.source = 'assets/mainViewTimerButtonPressed.png'
        import kivy.animation as Animate
        anim = Animate.Animation(
            x=self.client_scoreboard.right - self.timer_container.width,
            y=self.timer_container.y,
            duration=0.25)
        anim.start(self.timer_container)
        self.unbind(on_press=self.show)
        self.bind(on_press=self.hide)

    def changeImage(self):
        self.source = 'assets/mainViewTimerButton.png'

    def hide(self):
        import kivy.animation as Animate
        anim = Animate.Animation(
            x=self.client_scoreboard.width,
            y=self.timer_container.y,
            duration=0.25)
        anim.start(self.timer_container)
        self.unbind(on_press=self.hide)
        self.bind(on_press=self.show)

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    调用您在.bind() 中设置的函数的 kivy 代码正在传递您的函数未准备好的参数。我上次使用 kivy 已经有一段时间了,所以我不能确定,但​​我认为事件详细信息已传递给函数。

    因此,您对事件处理程序的定义应如下所示:

    def show(self, event):
        ...
    
    def hide(self, event):
        ...
    

    如果您好奇,您可以在这些函数中print(event),查看发送的内容。

    【讨论】:

      【解决方案2】:

      答案是在函数定义中包含类名,在本例中为TimerButton。这是一个我不完全理解的概念,因为该函数是在TimerButton 类的范围内定义的。但是,嘿,它有效。

      class TimerButton(ButtonBehavior, Image):
          timer_container = ObjectProperty(None)
          client_scoreboard = ObjectProperty(None)
      
          def __init__(self, **kwargs):
              super(TimerButton, self).__init__(**kwargs)
              self.bind(on_press=self.show)
              self.bind(on_release=self.changeImage)
      
          def show(self):
              print('hi there')
              self.source = 'assets/mainViewTimerButtonPressed.png'
              import kivy.animation as Animate
              anim = Animate.Animation(
                  x=self.client_scoreboard.right - self.timer_container.width,
                  y=self.timer_container.y,
                  duration=0.25)
              anim.start(self.timer_container)
              self.bind(on_press=self.hide)
      
          def changeImage(self):
              self.source = 'assets/mainViewTimerButton.png'
      
          def hide(self):
              import kivy.animation as Animate
              anim = Animate.Animation(
                  x=self.client_scoreboard.width,
                  y=self.timer_container.y,
                  duration=0.25)
              anim.start(self.timer_container)
              self.bind(on_press=self.show)
      

      【讨论】:

        猜你喜欢
        • 2014-06-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-15
        • 1970-01-01
        • 1970-01-01
        • 2022-10-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多