Search code examples
pythonbuttonkivy

Button in Kivy (Python) doesn't redirect


I've created a .py and a .kv file with the following code. You should expect the button to navigate to the next screen ("main") but it doesn't work.

I also don't know for sure if I must use on_press or on_release.

this is in the main.py file

import kivy
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen

Builder.load_file("storenavigator.kv")

class Logon(Screen):
     def __init__(self, **kwargs):
         super(Logon, self).__init__(**kwargs)


class Main(Screen):
    def __init__(self, **kwargs):
        super(Main, self).__init__(**kwargs)

class WindowManager(ScreenManager):
    def __init__(self, **kwargs):
        super(WindowManager, self).__init__(**kwargs)

class Storenavigator(App):
    def build(self):
        Window.clearcolor = ("#4D8D6E")
        return Logon()

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

and this is in the storenavigator.kv file

<WindowManager>:
    Logon:
    Main:

<Logon>:
    name: "logon"
    GridLayout:
        cols: 1
        size: root.width -200, root.height -200


        GridLayout:
            cols: 1
            padding: 50
            spacing: 20

            Image:
                source: "logo.png"
            Label:
                text: "Vind uw weg door onze winkel!"

            Button:
                text: "Inloggen"
                on_press:
                    app.root.current = "main"
                    #root.manager.transition.direction = "left"

            Button:
                text: "Doorgaan als gast"
                on_press:
                    app.root.current = "main"
                    #root.manager.transition.direction = "left"

<Main>:
    name: "main"
    GridLayout:
        cols: 1
        size: root.width -200, root.height -200
        pos: 100, 100

        GridLayout:
            cols: 1
            padding: 50
            spacing: 20

            Image:
                source: "logo.png"
            Label:
                text: "Vind uw weg door onze winkel!"

            Button:
                text: "Terug"
                on_press:
                    app.root.current = "logon"
                    #root.manager.transition.direction = "left"
    


The buttons are clickable but they dont redirect. In fact notting happens.

Anyone?

Thanks in advance!


Solution

  • Your App is not using a ScreenManager since your build() method returns just a Screen (Logon). Try changing:

    return Logon()
    

    to:

    return WindowManager()