讓我們想象一下我們有一個(gè)表單,這個(gè)人在輸入上寫了一些東西但需要清除它們,我將如何在 Kivy 中做到這一點(diǎn)?我試過(guò)這個(gè),但沒(méi)有用:主要.py:class CreateUra(GridLayout): def clear_inputs(self, *args): for item in args: item = ''class UraApp(App): def build(self): return CreateUra()if __name__ == "__main__": UraApp().run()烏拉.kv:<CreateUra>: cols: 1 padding: 10 spacing: 10 row_force_default: True row_default_height: '32dp' BoxLayout: Label: text: 'Contexto:' TextInput: hint_text: "Contexto da ura" focus: True multiline: False cursor_color: (0, 0, 0, 0.8) id: context_input BoxLayout: Label: text: 'Nome da ura:' TextInput: hint_text: "Nome do arquivo da ura" multiline: False cursor_color: (0, 0, 0, 0.8) id: ura_file_name Button: text: 'Create Ura' id: bt_create on_press: root.uraConfig() on_release: root.clear_inputs(context_input.text, ura_file_name.text)*忽略 on_press 上的 uraConfig我試過(guò)這種方式,它奏效了:Button: text: 'Create Ura' id: bt_create on_press: root.uraConfig() on_release: context_input.text = ''我對(duì) kivy 和 Python 很陌生,所以我不確定這是否是清除文本的最佳方式,但我做錯(cuò)了什么?或者,如果你們能以最好的方式來(lái)做這件事。
1 回答

喵喵時(shí)光機(jī)
TA貢獻(xiàn)1846條經(jīng)驗(yàn) 獲得超7個(gè)贊
您的情況的問(wèn)題是傳遞文本,但不是文本屬性,而是文本的副本,因此即使將其設(shè)置為空也不會(huì)反映在文本輸入中。您必須將 textinput 作為列表傳遞,迭代并使用空字符串設(shè)置屬性:
*.kv
Button:
text: 'Create Ura'
id: bt_create
on_press: root.uraConfig()
on_release: root.clear_inputs([context_input, ura_file_name]) # <-- add brackets
*.py
class CreateUra(GridLayout):
def clear_inputs(self, text_inputs):
for text_input in text_inputs:
text_input.text = ""
添加回答
舉報(bào)
0/150
提交
取消