Unable to switch screen in Kivy

Viewed 26

So I just started using Kivy to create a simple app. I have 2 screens (main and register). However, I am unable to switch screen from main to register. There is also no error code, so i am unsure what went wrong

Python code

from kivy.app import App
from kivy.properties import ObjectProperty
from kivy.graphics import Rectangle, Color, Line
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import Screen,ScreenManager


class Main(Screen):
    def btn_tambah_data(self):
        print("Add Data")

    def btn_edit_data(self):
        print("Edit Data")

    def btn_lihat_data(self):
        print("See Data")

class Register(Screen):
    pass


class WindowManager(ScreenManager):
    pass


class Kosless(App):
    def build(self):
        return Main()


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

Kv code (Sorry if my code is kinda messy)

WindowManager:
    Main:
    Register:

<Main>:
    name: "Main"
    FloatLayout:

        canvas:

            Color:
                rgba: .62,.91,.968,1
            Rectangle:
                pos : self.pos
                size : self.size

            Color:
                rgba:1,.984,.58,1
            Rectangle:
                size: self.width , self.height/25
                pos: self.pos

        Label:
            text:"Kosless"
            font_size:30
            color: 1,.984,.58
            pos_hint:{"y":0.4}
            bold:True
        Button:
            text:"Add Data"
            pos_hint:{"y":0.55,"x":0.25}
            size_hint: 0.5 ,0.1
            background_color:0.1, 0.5, 0.6, 1
            on_press : app.root.current: "Register"    ##I think something went wrong here
        Button:
            text:"Edit Data"
            size_hint: 0.5 ,0.1
            pos_hint:{"y":0.40,"x":0.25}
            background_color:0.1, 0.5, 0.6, 1
            on_press : root.btn_edit_data()
        Button:
            text:"See Data"
            size_hint: 0.5 ,0.1
            pos_hint:{"y":0.25,"x":0.25}
            background_color:0.1, 0.5, 0.6, 1
            on_press : root.btn_lihat_data()

<Register>:
    name: "Register"
    Button:
        text:"Test"

Here is the main screen, theoretically if I click on the 'Add Data' button, the register screen will show up. However nothing happens... Main Screen

What should I do? Do you guys have any suggestions? Thanks for reading my post, have a great day!

1 Answers

There are two problems with your code. The first is that in order to switch screens, you need a ScreenManager, but your app.root is a Screen (Main). You can change your build() method to something like:

class Kosless(App):
    def build(self):
        return Builder.load_file('kv_file_name')

And the on_press attribute of the Add Data Button should be:

    Button:
        text:"Add Data"
        pos_hint:{"y":0.55,"x":0.25}
        size_hint: 0.5 ,0.1
        background_color:0.1, 0.5, 0.6, 1
        on_press : app.root.current="Register" # changed `:` to `=`
Related