简体   繁体   中英

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.

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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