繁体   English   中英

Kivy / Python。 一个回调函数可用于多个按钮

[英]Kivy/python. One callback function for several buttons

我开始用树莓派和触摸屏实现网络广播。 我在屏幕上放置了几个按钮,我想为所有按钮实现一个回调函数。 与if-else结构所按下的按钮不同。

kv文件:

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

python文件:

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

我找不到用参数调用回调函数的方法,在if-else结构中,该参数可以有所不同。

为什么不在函数中使用另一个参数?

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

并在kivy中使用root.ctrl_buttons(self.text)

记不清是否需要另一个参数来传递按钮,但是如果是,那么这样做更有效:

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

暂无
暂无

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

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