Search code examples
pythonkivytextinput

Python Kivy TextInput not working?


I have created a simple login and registration screen using kivy, but the TextInput do not let me type inside of it? This error is occuring on both the LoginScreen and the RegistrationScreen. I tried looking up past questions on here, but i couldn't find anything.

The Python file:

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()

And the MainApp.kv file:

<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()

Thanks for help :)


Solution

  • Remove this:

    Builder.load_file("MainApp.kv")
    

    from your py file because you're App instance, ie:

    class MainApp(App):
    

    Loads it automatically already.