简体   繁体   English

如何向 Kivy 布局添加多个按钮

[英]How to add mulitple buttons to a Kivy Layout

I am trying to a make a music player using Kivy, which will just have two button;我正在尝试使用 Kivy 制作一个音乐播放器,它只有两个按钮; one for play and one for pause.一个用于播放,一个用于暂停。 When I trying to bind a function to the buttons, when I press the buttons, I get errors.当我尝试将 function 绑定到按钮时,当我按下按钮时,出现错误。 Upon searching this very own site for this, there was a suggestion to use the partial function from the functools library.在搜索这个非常自己的网站后,有人建议使用functools库中的partial function。 But even that didn't work.但即使这样也没有用。 My code is below.我的代码如下。

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

        self.size = Window.size

        self.play = Button(text="Play", size_hint=(0.25, 0.25), font_size=36,  background_color=color,
                           pos=(self.size[0]*(3/8), self.size[1]*(4/10)) )
        self.pause = Button(text="Pause", size_hint=(0.25, 0.25), font_size=36, background_color=color,
                            pos=(self.size[0]*(3/8), self.size[1]*(1/10)) )
        self.play.bind(on_press=self.play)
        self.pause.bind(on_press=self.pause)
        self.add_widget(self.play)
        self.add_widget(self.pause)


    def play(self):
        print("PLay")

    def pause(self):
        print("Pause")

This gave this error这给出了这个错误

AssertionError: <kivy.uix.button.Button object at 0x000001D1D4792CF0> is not callable AssertionError: <kivy.uix.button.Button object at 0x000001D1D4792CF0> 不可调用

and by using the partial function并通过使用部分 function

        self.play.bind(on_press=partial(self.play))
        self.pause.bind(on_press=partial(self.pause))

I get served this我得到了这个

TypeError: the first argument must be callable TypeError:第一个参数必须是可调用的

There are two errors in your code.您的代码中有两个错误。 the first one is that you have given the event handlers and the buttons the same name.第一个是您为事件处理程序和按钮指定了相同的名称。 So change one of those.所以改变其中之一。 Second, you need a second parameter to event handlers as it gets called with a parameter.其次,您需要事件处理程序的第二个参数,因为它使用参数调用。

Here is your corrected code:这是您更正的代码:

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

        self.size = Window.size

        self.play = Button(text="Play", size_hint=(0.25, 0.25), font_size=36,
                           pos=(self.size[0]*(3/8), self.size[1]*(4/10)) )
        self.pause = Button(text="Pause", size_hint=(0.25, 0.25), font_size=36,
                            pos=(self.size[0]*(3/8), self.size[1]*(1/10)) )

        self.play.bind(on_press=self.play1)
        self.pause.bind(on_press=self.pause1)

        self.add_widget(self.play)
        self.add_widget(self.pause)


    def play1(self, instance):
        
        print("PLay")

    def pause1(self,  instance):

        print("Pause")

If you don't want to add a parameter then you can use lambda function.如果您不想添加参数,则可以使用 lambda function。 Something like this:像这样的东西:

self.play.bind(on_press=lambda _:self.play1())
self.pause.bind(on_press=lambda _:self.pause1())

In this case, you can remove the extra parameter in the eventhandler.在这种情况下,您可以删除事件处理程序中的额外参数。

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

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