简体   繁体   English

使用Kivy语言添加按钮后将按钮绑定到功能

[英]Binding button to function after adding button with Kivy language

I'm trying to figure out how to bind a button that's been laid out using Kivy language to a function. 我试图弄清楚如何将使用Kivy语言布局的按钮绑定到函数。 I've seen plenty of answers when laying out buttons in Python language. 使用Python语言布置按钮时,我已经看到很多答案 But what about once everything is in place and you're now referencing via a custom class that inherits from Button ? 但是,一旦一切就绪,您现在可以通过继承自Button的自定义类进行引用了吗?

On press, the code below throws the error TypeError: show() takes 1 positional argument but 2 were given and crashes the program. 在发布时,下面的代码将引发错误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)

The answer is to include the class name, in this case TimerButton , in the function definitions. 答案是在函数定义中包含类名,在这种情况下为TimerButton This is a concept I don't completely understand since the function is defined within the scope of the TimerButton class. 由于函数是在TimerButton类的范围内定义的,所以我对此概念并不完全了解。 But hey, it works. 但是,它有效。

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)

The kivy code that calls the function you set in .bind() is passing an argument which your function isn't prepared for. 调用您在.bind()设置的函数的奇异代码传递了一个参数,您的函数没有准备好该参数。 It's been a while since I last used kivy, so I can't be sure, but I think the event details are passed to the function. 自从我上次使用kivy已经有一段时间了,所以我不确定,但是我认为事件详细信息已传递给该函数。

As such, your definitions for event handlers should look like this: 因此,事件处理程序的定义应如下所示:

def show(self, event):
    ...

def hide(self, event):
    ...

If you're curious, you could print(event) within those functions, to see what's being sent in. 如果您很好奇,可以在这些函数中print(event) ,以查看正在发送的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM