【问题标题】:Python Kivy Screen ManagerPython Kivy 屏幕管理器
【发布时间】:2016-03-09 10:10:40
【问题描述】:
<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 文件中有两个小部件: - HomePage - 这继承自 Screen 并具有 screen.manager 属性,用于管理屏幕转换 - LowerBoxNav - 这是一个盒子布局。本质上,我希望在每一页上都有这种布局。框布局中有 2 个按钮 - LoginButtonsApp 和 NextButtonsApp。

我的问题如下: - 使用上面的代码,我得到错误 AttributeError: 'LowerBoxNav' object has no attribute 'manager' - 我还尝试为 HomePage 屏幕中 LowerBoxNav 小部件中的每个按钮添加 on_press - 在这种情况下,我每个按钮都有 2 个

如果能在我的应用程序的每个屏幕上基本上拥有相同的 LowerBoxNav,我们将不胜感激。

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    root 表示 中的规则或小部件,对您而言,这意味着 BoxLayout,因此没有可用的 manager,您会得到 AttributeError

    您必须通过Screen 小部件访问manager 属性,或者为自己设置一个规则,屏幕位于顶部,BoxLayout 位于其中。此外,Screen 必须是 ScreenManager 的子代,否则将不起作用。

    编辑:将 root.manager.current 更改为 root.parent.manager.current

    【讨论】:

      【解决方案2】:
      <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:
      

      给 ScreenManager 一个 sm 的 id,然后将按钮设置为 on_press:app.root.ids.sm.current="Whatever"。然后确保您使用名称定义页面并将该名称传递给您的屏幕管理器。所以在你的屏幕三的 kv 文件中,例如

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-04
        • 1970-01-01
        • 1970-01-01
        • 2019-04-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多