[英]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. 与if-else结构所按下的按钮不同。
kv-file: kv文件:
BoxLayout:
Button:
text: "PLAY"
on_press: root.ctrl_buttons()
Button:
text: "STOP"
on_press: root.ctrl_buttons()
python-file: 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)
I have found no way, to call the callback function with an parameter, which I can differ within the if-else structure. 我找不到用参数调用回调函数的方法,在if-else结构中,该参数可以有所不同。
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)
并在kivy中使用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"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.