简体   繁体   English

Kivy ScreenManager切换屏幕并留下按钮

[英]Kivy ScreenManager switch screens leaving buttons

I am using ScreenManager to swicth screens but when I switch screens normally the buttons on that screen stay with it. 我正在使用ScreenManager旋转屏幕,但是当我正常切换屏幕时,该屏幕上的按钮将保持不变。 I have to copy the buttons to each screen. 我必须将按钮复制到每个屏幕。 Is there a way to switch screens while leaving the buttons in place? 有没有办法在将按钮留在原处的同时切换屏幕?

This is my app code as it stands: 这是我目前的应用代码:

import (...)


class AppContainer(FloatLayout):
    pass


class NavButtons(BoxLayout):
    pass


class FirstScreen(Screen):
    pass


class SecondScreen(Screen):
    pass


class Screens(ScreenManager):
    pass


class MainApp(App):
    def build(self):
        return AppContainer()

(...) (...)

and the kv file looks like this: 而kv文件如下所示:

#:import BoxLayout kivy.uix.boxlayout.BoxLayout

<AppContainer>:
    NavButtons:
    Screens:
        FirstScreen:
        SecondScreen:
        ThirdScreen:


<NavButtons>:
    orientation:'vertical'
    Button:
        on_press: root.manager.current="first"
        text: 'First'
        pos_hint:{"top": 1, "left": 0}
    Button:
        on_press: root.manager.current="second"
        text: 'Second'
        pos_hint:{"top":0.8, "left": 0}
<Button>:
    size_hint: 0.2, 0.2


<FirstScreen>:
    name: "first"
    Label:
        text: "First Screen"


<SecondScreen>:
    name: "second"
    Label:
        text: "Second Screen"

But this throws an error: AttributeError: 'NavButtons' object has no attribute 'manager' 但这会引发错误:AttributeError:'NavButtons'对象没有属性'manager'

Any ideas? 有任何想法吗?

in the field of the button the root is NavButtons , and as you can see NavButtons does not have Screens as a children, a way to access Screens is through app.root that returns in this case to AppContainer , we set it up to Screens and we access this because he is a children. 在按钮字段中,根目录为NavButtons ,并且您可以看到NavButtons没有将Screens作为子级,一种访问Screens是通过app.root在这种情况下返回AppContainer ,我们将其设置为Screens和由于他是孩子,我们访问此页面。

#:import BoxLayout kivy.uix.boxlayout.BoxLayout

<AppContainer>:
    NavButtons:
    Screens:
        id: sm
        FirstScreen:
        SecondScreen:

<NavButtons>:
    orientation:'vertical'
    Button:
        on_press: app.root.ids.sm.current="first"
        text: 'First'
        pos_hint:{"top": 1, "left": 0}
    Button:
        on_press: app.root.ids.sm.current ="second"
        text: 'Second'
        pos_hint:{"top":0.8, "left": 0}

[...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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