如何按ID选择小部件?

时间:2015-07-28 05:59:54

标签: python kivy

我想在这样的python代码上添加许多小部件,

for item in parsed_result:
    time.sleep(0.01)
    btn = Button(text=item['id'], size_hint=(.5, None), height=32,
                 on_release=self.click_device_id)
    txt = TextInput(id=item['id'], text='hello', size_hint=(.5, None),
                    height=32)
    add_widget_target.add_widget(btn)
    add_widget_target.add_widget(txt)

def click_device_id(self, myself):
    # I want print TextInput.text on same level.
    self.go_next_screen()

问题是我无法添加txt作为btn on_release参数。所以,我需要他们如何选择txt小部件来打印文本。

1 个答案:

答案 0 :(得分:0)

使用lambda,如下所示。

for item in parsed_result:
    time.sleep(0.01)
    txt = TextInput(id=item['id'], size_hint=(.5, None), height=32, text='IMSI', multiline=False)
    btn = Button(text=item['id'], size_hint=(.5, None), height=32,
                     on_release=lambda ti=txt: self.click_device_id(ti.text, txt.text))
    add_widget_target.add_widget(btn)
    add_widget_target.add_widget(txt)

def click_device_id(self, button_text, txt_text):
    print button_text, txt_text
    self.go_next_screen()