Search code examples
python-3.xkivykivy-language

Kivy widget hierarchy not behaving as expected


I am trying to learn Kivy and can't seem to understand why my code (below) doesn’t work properly. In my view all three of the buttons on the first screen should move you to the second screen, but only one does! I thought that since I am referring to the Window manager from the base of the Widget tree (app.root), that the reference should be absolute and "position independent". There is clearly something I don't understand…

    WindowManager:
        CourseGridLayout:
        CourseProgressWindow:
    
    <CourseGridLayout>:
        name: "courselist"
        id: courselist
        BoxLayout:
            orientation: "vertical"
            size: root.width, root.height
    
            Image:
                source: "logo.png"
    
            GridLayout:
                name: "bottomGrid"
                cols: 2
    
                Button:
                    text: "placeholder"
                    font_size: 32
                    on_press:
                        app.root.current: "courseprogress"
                        root.manager.transition.direction: "left"
    
                Button:
                    text: "placeholder2"
                    font_size: 32
                    on_press:
                        app.root.current: "courseprogress"
                        root.manager.transition.direction: "left"
    
            Button:
                text: "Next Screen"
                font_size: 32
                on_press:
                    app.root.current = "courseprogress"
                    root.manager.transition.direction = "left"
    
    <CourseProgressWindow>:
        name: "courseprogress"
        id: courseprogress
        BoxLayout:
            orientation: "vertical"
            size: root.width, root.height
    
            Label:
                id: progressoutput
            Button:
                text: "Go Back"
                font_size: 32
                on_press:
                    app.root.current = "courselist"
                    root.manager.transition.direction = "right"
#!/usr/bin/env python3
__author__ = "Arana Fireheart"

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


#Define our different screens
class CourseGridLayout(Screen):
    pass


class CourseProgressWindow(Screen):
    pass


class WindowManager(ScreenManager):
    pass


# Designate Our .kv design file
kivvy = Builder.load_file("CGMonitor2.kv")


class DemoApp(App):
    def build(self):
        return kivvy


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


Solution

  • In the two Buttons that don't work you use:

                        app.root.current: "courseprogress"
                        root.manager.transition.direction: "left"
    

    Try changing that to:

                        app.root.current = "courseprogress"
                        root.manager.transition.direction = "left"
    

    The commands after on_press: must be python commands.