Kivy/python。多个按钮的一个回调函数
Kivy/python. One callback function for several buttons
我开始实现带有 raspberry pi 和触摸屏的网络收音机。我在屏幕上放置了几个按钮,我想为所有按钮实现一个回调函数。通过 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"
在基维使用root.ctrl_buttons(self.text)
不记得它是否需要另一个参数或不仅仅是为了传递按钮,但如果是,那么这更有效:
def ctrl_buttons(self, button):
if button.text=="PLAY":
print "pressed play"
elif button.text=="STOP":
print "pressed stop"
我开始实现带有 raspberry pi 和触摸屏的网络收音机。我在屏幕上放置了几个按钮,我想为所有按钮实现一个回调函数。通过 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"
在基维使用root.ctrl_buttons(self.text)
不记得它是否需要另一个参数或不仅仅是为了传递按钮,但如果是,那么这更有效:
def ctrl_buttons(self, button):
if button.text=="PLAY":
print "pressed play"
elif button.text=="STOP":
print "pressed stop"