Search code examples
kivykivy-language

Kivy ScreenManager doesn't do anything at all


I have a really basic Kivy program, one that just has a basic screenmanager and buttons to switch between the screens, except the screenmanager isn't working. Here's the Python file:

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


class WindowManager(ScreenManager):
    pass

class LoginPage(Screen, Widget):
    pass

class CreateAccountPage(Screen, Widget):
    pass


kv = Builder.load_file('the.kv')
class TheApp(App):
    def build(self):
        return LoginPage()

if __name__ == '__main__':
    TheApp().run()

Here's the .kv file:

WindowManager:
    LoginPage:
    CreateAccountPage:

<LoginPage>
    name: 'log'
    Button:
        text: 'log'
        on_release: app.root.current = 'create'

<CreateAccountPage>
    name: 'create'
    Button:
        text: 'create'
        on_release: app.root.current = 'log'

When I click the button, it doesn't bring me to the next page, it just does nothing.


Solution

  • The kv file is loaded twice (once implicitly with default naming convention and the other with explicit kv = Builder.load_file('the.kv')). Also the LoginPage screen is rooted by the call return LoginPage() in build method. To correct these, you can define the kv file to any name other than the default name. Below is the corrected snippet of the problematic block:

    kv = Builder.load_file('the1.kv')
    class TheApp(App):
        def build(self):
            return kv
    

    Here the kv file is renamed to the1.kv.

    The rest of the code is ok.