Python Kivy屏幕管理

2024-09-30 18:12:53 发布

您现在位置:Python中文网/ 问答频道 /正文

<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:

我在一个.kv文件中有两个小部件: -主页-它继承自Screen并具有屏幕管理器属性,用于管理屏幕转换 -LowerBoxNav-这是一个方框布局。基本上,我希望每个页面都有这样的布局。有两个按钮-LoginButtonApp和NextButtonsApp,它们位于框布局中。在

我的问题是: -使用上面的代码,我得到错误AttributeError:'LowerBoxNav'对象没有属性'manager' -在每个小部件内都尝试了在每个小部件的主页内添加按钮

如果能在我的应用程序的每个屏幕上都有相同的LowerBoxNav,我将非常感激。在


Tags: possize屏幕on部件managerroot布局
2条回答

root表示<;>中的规则或小部件,对您来说意味着BoxLayout,因此没有可用的manager,您将得到AttributeError

您必须通过Screen小部件访问manager属性,或者为自己制定一个规则,其中Screen在顶部,BoxLayout在其中。而且,Screen必须是ScreenManager的子级,否则它将无法工作。在

编辑:将root.manager.current更改为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:

给屏幕管理器一个sm的id,然后将按钮设置为on-tu-press:app.root.ids.sm.电流=“不管怎样”。然后确保使用名称定义页面并将该名称传递给屏幕管理器。例如,在你的第三屏的kv文件中

^{pr2}$

相关问题 更多 >