Skip to content Skip to sidebar Skip to footer

Kivy Class In .py And .kv Interaction

I'm not fully grasping how classes in a python file and the kivy language interact. I'm currently modifying the Showcase example of Kivy to increase my understanding. File structur

Solution 1:

You can use the ScreenManager. Have a look here, I changed some of your code to provide an example of using the ScreenManager to seperate out the code for each screen into their own classes. If there are problems with this let me know.

main.py:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.properties import ObjectProperty


class Testy(Screen):

    ttext = 'Screen 2'
    #other code

class ScreenTwo(Screen):
    pass

class Manager(ScreenManager):

    testy = ObjectProperty(None)
    screen_2 = ObjectProperty(None)


class ShowcaseApp(App):

    def build(self):
        return Manager(transition=FadeTransition())

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

kv file:

<Testy>:
    BoxLayout:
        Button:
            text: root.ttext
            on_press: root.manager.current = "screen_two"

<ScreenTwo>:
    BoxLayout:
        Button:
            text: "Screen 1"
            on_press: root.manager.current = "testy_screen"


<Manager>:
    id: screen_manager

    testy: testy
    screen_2: screen_2

    Testy:
        id: testy
        name: 'testy_screen'
        manager: screen_manager

    ScreenTwo:
        id: screen_2
        name: 'screen_two'
        manager: screen_manager

Post a Comment for "Kivy Class In .py And .kv Interaction"