Python Kivy TextInput无法正常工作?

时间:2018-01-20 12:34:45

标签: python kivy textinput

我使用kivy创建了一个简单的登录和注册屏幕,但是TextInput不允许我在里面键入它吗? LoginScreen和RegistrationScreen上都会出现此错误。我试着在这里查看过去的问题,但我找不到任何东西。

Python文件:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen

class Login_Screen(Screen):
    def login(self):
        if self.ids.username.text == "root" and\
            self.ids.passwrd.text == "123":
            print("Direct Entry")
            self.manager.current = "Login_Successful"
        else:
            print("Wrong Password")
            self.manager.current = "Login_Failed"
    def registration(self):
        self.manager.current = "Registration_Screen"

class LoginConfirmationScreen(Screen):
    pass

class Registration_Screen(Screen):
    def MainScreen(self):
        self.manager.current = "Login_Screen"

class Login_Failed(Screen):
    def MainScreen(self):
        self.manager.current = "Login_Screen"

class RootWidget(ScreenManager):
    pass

Builder.load_file("MainApp.kv")

class MainApp(App):
    def build(self):
        return RootWidget()

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

和MainApp.kv文件:

<RootWidget>:
    id: Main
    Login_Screen:
        id: login
        name: "Login_Screen"
    Login_Failed:
        id: Failed
        name: "Login_Failed"
    Registration_Screen:
        id: register
        name: 'Registration_Screen'


<Login_Screen>:
    GridLayout:
        rows:3
        cols:2
        Label:
            text: "Username:"
            font_size: 20
        TextInput:
            id: username
            multiline: False
            hint_text: 'Enter your Username'
        Label:
            text: "Password"
            font_size: 20
        TextInput:
            id: passwrd
            multiline: False
            hint_text: 'Enter your Password'
            password: True
        Button:
            text: "Register"
            on_press: root.registration()
        Button:
            text: "Sign In"
            on_press: root.login()

<Registration_Screen>:
    GridLayout:
        rows:3
        cols:2
        Label:
            text: 'First Name:'
            font_size: 20
        TextInput:
            id: FirstName
            multiline: False
            hint_text: 'Enter your First Name'
        Label:
            text: 'Surname'
            font_size: 20
        TextInput:
            id: Surname
            multiline: False
            hint_text: 'Enter your Surname'
        Button:
            text: "Create Account"
            on_press: root.MainScreen()


<Login_Failed>:
    BoxLayout:
        orientation: "vertical"
        Label:
            text: "Login Failed"
        Button:
            text: "Try again"
            on_press: root.MainScreen()

感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

删除它:

Builder.load_file("MainApp.kv")

来自你的py文件,因为你是App实例,即:

class MainApp(App):

已自动加载。