簡體   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