You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kivy中如何清除文本内容?登录GUI清除功能失效求助

解决Kivy登录GUI输入框清除无效的问题

问题根源

  1. 属性拼写错误:resetType函数里错误使用了txt属性,Kivy的TextInput控件内容对应的属性是text,而非txt,这是导致输入框无法清空的核心原因。
  2. 按钮事件冗余且调用错误:CreateWindow的按钮中重复设置了on_press: root.resetType,既没有加括号(无法触发函数执行),且createAccountButton方法里已经调用了resetType,属于冗余代码。

修正后的代码

Python代码部分

import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ObjectProperty
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder

class LoginWindow(Screen):
    username = ObjectProperty(None)
    password = ObjectProperty(None)

    def btn(self):
        print(self.username.text, self.password.text)
        # 后续登录验证失败时,可调用以下清除方法
        # self.clear_inputs()
    
    # 新增登录失败清除输入框的方法
    def clear_inputs(self):
        self.username.text = ""
        self.password.text = ""

class CreateWindow(Screen):
    createusername = ObjectProperty(None)
    createpassword = ObjectProperty(None)

    def createAccountButton(self):
        createdusername = str(self.createusername.text)
        createdpassword = str(self.createpassword.text)
        with open("database.txt", "a") as database:
            database.write(createdusername + "," + createdpassword + '\n')
            print("In file now")
            # with语句会自动关闭文件,无需手动调用close()
        self.resetType()
    
    def resetType(self):
        self.createusername.text = ""  # 修正为text属性
        self.createpassword.text = ""
        print("Working")

class RealWindow(Screen):
    pass

class WindowManager(ScreenManager):
    pass

kv = Builder.load_file("login.kv")

class TestApp(App):
    def build(self):
        return kv

if __name__ == "__main__":
    TestApp().run()

KV文件部分

WindowManager:
    #: import NoTransition kivy.uix.screenmanager.NoTransition
    transition: NoTransition()
    LoginWindow:
    CreateWindow:
    RealWindow:

<LoginWindow>:
    name: "login"
    username: username
    password: password

    FloatLayout:
        Button:
            text:"Log In"
            size_hint: 0.5, 0.1
            pos_hint: {"x":0.25, "y":0.3}
            on_press: root.btn()
        
        Button:
            text:"Create Account"
            size_hint: 0.3, 0.05
            pos_hint: {"x":0.36, "y":0.20}
            on_release:
                app.root.current = "Create"
        
        TextInput:
            id: password
            size_hint: 0.25, 0.1
            pos_hint: {"x":0.5, "y":0.5}
        
        TextInput:
            id:username
            size_hint: 0.25, 0.1
            pos_hint: {"x":0.5, "y":0.6}
        
        Label:
            font_size: '40'
            text:"Log In"
            size_hint: 0.5, 0.5
            pos_hint: {"x":0.25, "y":0.6}
        
        Label:
            font_size: '25'
            text:"Username:"
            size_hint: 0.5, 0.1
            pos_hint:{"x":0.1,"y":0.6}
        
        Label:
            font_size: '25'
            text:"Password:"
            size_hint: 0.5, 0.1
            pos_hint:{"x":0.1,"y":0.5}

<CreateWindow>:
    name: "Create"

    createusername: createusername
    createpassword: createpassword

    FloatLayout:
        Button:
            text:"Login"
            size_hint: 0.5, 0.1
            pos_hint: {"x":0.25, "y":0.3}
            on_press: root.createAccountButton()
            on_release:
                app.root.current = "login"
        
        TextInput:
            id: createpassword
            size_hint: 0.25, 0.1
            pos_hint: {"x":0.5, "y":0.5}
        
        TextInput:
            id:createusername
            size_hint: 0.25, 0.1
            pos_hint: {"x":0.5, "y":0.6}
        
        Label:
            font_size: '40'
            text:"Create Account"
            size_hint: 0.5, 0.5
            pos_hint: {"x":0.25, "y":0.6}
        
        Label:
            font_size: '25'
            text:"Username:"
            size_hint: 0.5, 0.1
            pos_hint:{"x":0.1,"y":0.6}
        
        Label:
            font_size: '25'
            text:"Password:"
            size_hint: 0.5, 0.1
            pos_hint:{"x":0.1,"y":0.5}

<RealWindow>:
    name: "Real"

内容的提问来源于stack exchange,提问作者asp3ct

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 11:54:56