【发布时间】:2017-07-18 15:34:52
【问题描述】:
我有一个带有多个按钮的应用程序,我需要在按下按钮时将按钮的 id 和文本值作为字符串获取。然后将抓取的按钮的 Ids 和 Text 值传递给另一个函数以进行进一步处理。为简单起见,我编写了这个示例程序。
# main.py
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
########################################################################
class KVMyHBoxLayout(BoxLayout):
pass
########################################################################
class ExampleApp(App):
def Pressbtn(self, *args):
print'Pressed button'
#----------------------------------------------------------------------
def build(self):
return KVMyHBoxLayout()
#----------------------------------------------------------------------
if __name__ == "__main__":
app = ExampleApp()
app.run()
kv文件
<MyButton@Button>:
color: .8,.9,0,1
font_size: 32
<KVMyHBoxLayout>:
orientation: 'vertical'
MyButton:
id:"idBtn1"
text: "Btn1"
background_color: 1,0,0,1
on_press:app.Pressbtn()
MyButton:
id:"idBtn2"
text: "Btn2"
background_color: 0,1,0,1
on_press:app.Pressbtn()
Label:
text: "ID"
background_color: 0,0,1,1
Label:
text: "Text"
background_color: 1,0,1,1
当按下按钮时,其对应的 id 和 text 值将显示在 ID 和 Text 标签中。目前上述代码仅在按下按钮时打印Pressed button。我想知道如何以python方式获取按钮的id和文本值。
【问题讨论】: