Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

python - 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; 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. Upon searching this very own site for this, there was a suggestion to use the partial function from the functools library. 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

and by using the partial 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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. 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...