简体   繁体   中英

Python Kivy Screen Manager

<LowerBoxNav>:
    BoxLayout:
        orientation: 'horizontal'
        LoginButtonsApp:
            size_hint_x:0.5
            pos_hint_x: 0.2
            pos_hint_y: 0.2
            on_press:
                root.manager.current='LoginScreen#'

        NextButtonsApp:
            size_hint_x:0.5
            pos_hint_x: 0.2
            pos_hint_y: 0.2
            on_press:
                root.manager.current='ScreenThree#'

<HomePage>:
    LayoutsApp:
        LabelsApp:
            pos_hint: {'center_x': .5, 'center_y': .7}
            text: root.homepageusernametext
        NextButtonsApp:
            pos_hint: {'center_x': .5, 'center_y': .5}
            on_press: 
                root.manager.current='ScreenThree#'
    LowerBoxNav:

I have two widgets in a .kv file: - HomePage - this inherits from Screen and has a screen.manager property which is used to manage screen transitions - LowerBoxNav - this is a box layout. Essentially I want this layout on every page. There are 2 buttons - LoginButtonsApp and NextButtonsApp which are within the box layout.

My problem is the following: - With the code above, I get the error AttributeError: 'LowerBoxNav' object has no attribute 'manager' - I have also tried adding the on_press for each of the buttons within the LowerBoxNav widget within the HomePage screen - in that case, I have 2 of each button

Would appreciate any help in essentially having the same LowerBoxNav on every screen of my app.

root means a rule or a widget in <>, which for you means a BoxLayout , therefore no manager is available and you get an AttributeError

You have to access manager property through Screen widget or make yourself a rule where Screen is at the top and BoxLayout is in it. And also, the Screen has to be a child of a ScreenManager , otherwise it won't work.

Edit: Change root.manager.current to root.parent.manager.current

<LowerBoxNav>:
    BoxLayout:
        orientation: 'horizontal'
        LoginButtonsApp:
            size_hint_x:0.5
            pos_hint_x: 0.2
            pos_hint_y: 0.2
            on_press:
                app.root.ids.sm.current='LoginScreen#'

        NextButtonsApp:
            size_hint_x:0.5
            pos_hint_x: 0.2
            pos_hint_y: 0.2
            on_press:
                app.root.ids.sm.current='ScreenThree#'

<HomePage>:
    id: sm
    LayoutsApp:
        LabelsApp:
            pos_hint: {'center_x': .5, 'center_y': .7}
            text: root.homepageusernametext
    LowerBoxNav:
    ScreenThree:

Give the ScreenManager an id of sm and then set buttons to on_press: app.root.ids.sm.current="Whatever". Then make sure you define the pages with a name and pass that name to your screenmanager. So in your kv file for screenthree for example

<ScreenThree>:
    name: ' ScreenThree' #this is what you would call for the on_press event
    Label:
        text: 'Hello'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM