讓我們想象一下我們有一個表單,這個人在輸入上寫了一些東西但需要清除它們,我將如何在 Kivy 中做到這一點?我試過這個,但沒有用:主要.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我試過這種方式,它奏效了:Button: text: 'Create Ura' id: bt_create on_press: root.uraConfig() on_release: context_input.text = ''我對 kivy 和 Python 很陌生,所以我不確定這是否是清除文本的最佳方式,但我做錯了什么?或者,如果你們能以最好的方式來做這件事。
1 回答

喵喵時光機
TA貢獻1846條經驗 獲得超7個贊
您的情況的問題是傳遞文本,但不是文本屬性,而是文本的副本,因此即使將其設置為空也不會反映在文本輸入中。您必須將 textinput 作為列表傳遞,迭代并使用空字符串設置屬性:
*.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 = ""
添加回答
舉報
0/150
提交
取消