首页 > 解决方案 > 如何使用按钮清除 TextInput

问题描述

我目前正在开发一个心理健康应用程序,但遇到了一个问题。每当我点击某个按钮时,我都需要清除 TextInput,但我不确定该怎么做。

作为参考,这是 TextInput:

TextInput:
        id: text
        font_size: (root.width**2 + root.height**2) / 15**4
        multiline: False
        pos_hint: {"x": 0.1 , "top":0.8}
        size_hint: 0.8, 0.7

以及将触发它的按钮:

Button:
        pos_hint: {"x": 0.0, "y": 0.95}
        size_hint: 0.2, 0.05
        text: 'Enter'
        background_color: 0, 0, 0, 1
        on_press: text.delete_selection()

本节的 Python 代码为:

class Screen_One(Screen):
    text = ObjectProperty(None)

    def delete_selection(self, from_undo=False):
        if not self._selection:
            return ("")

正如你所看到的,我尝试了一些东西,但它仍然没有效果,它不会清除文本输入。

有人可以帮帮我吗?提前致谢!

标签: pythonkivy

解决方案


text.text = ""在你的python代码中会做到这一点。您可以创建一个类似的函数onclick,如果您在一个整体类中,您可以调用self.text.text = "". 这将重置 TextInput 的文本。


推荐阅读