Raymond
Raymond

Reputation: 5

Kivy/python. One callback function for several buttons

I started to realize an internet radio with a raspberry pi and a touch screen. I have placed several buttons on screen and I want to realize one callback function for all buttons. Differs which button was pressed, by an if-else structure.

kv-file:

BoxLayout:
    Button:
        text: "PLAY"
        on_press: root.ctrl_buttons()
    Button:
        text: "STOP"
        on_press: root.ctrl_buttons()

python-file:

def ctrl_buttons(self):
    if "play pressed":
        subprocess.check_output("mpc play", shell=True)
     elif "stop pressed":
         subprocess.check_output("mpc stop", shell=True)

I have found no way, to call the callback function with an parameter, which I can differ within the if-else structure.

Upvotes: 0

Views: 621

Answers (1)

Peter Badida
Peter Badida

Reputation: 12199

Why don't you use another argument in your function?

def ctrl_buttons(self, button):
    if button=="PLAY":
        print "pressed play"
    elif button=="STOP":
        print "pressed stop"

and in kivy use root.ctrl_buttons(self.text)

Can't remember if it wants another argument or not just for passing button, but if yes, then this is more effective:

def ctrl_buttons(self, button):
    if button.text=="PLAY":
        print "pressed play"
    elif button.text=="STOP":
        print "pressed stop"

Upvotes: 1

Related Questions