如何从另一个班级更改班级的属性

时间:2018-12-09 10:07:44

标签: python kivy

我是Kivy的新手,我遇到了一个问题。我有“ MainPanel”类,其中包含四个类“ Navigation”,“ Images”,“ Config”和“ Information”。我想要的是在“导航”类中按下按钮时,然后在“信息”类中使用屏幕管理器更改当前屏幕。

这是我的代码: Main.py:

copts

mainPanel.kv:

class Navigation(FloatLayout):
    pass

class Information(ScreenManager):
    pass

class SlothFacts(Screen):
    pass

class KiwiFacts(Screen):
    pass

class MainPanel(GridLayout):
    pass

Builder.load_file("mainPanel.kv")

class Complex(App):
    def build(self):
        return MainPanel()

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

Navigation.kv:

#:include navigation.kv
#:include images.kv
#:include config.kv
#:include information.kv

<MainPanel>:
    cols:2
    Navigation:
    Images:
    Config:
    Information:

Information.kv:

<Navigation>:
    Button:
        text:"Kiwi"
        size_hint:(.3, .2)
        pos_hint:{'x':.1, 'y':.25}
        #on_press: <--- Change screen to kiwi in Information class

1 个答案:

答案 0 :(得分:0)

id文件中的ScreenManager中向您的mainPanel.kv添加#:include navigation.kv #:include images.kv #:include config.kv #:include information.kv <MainPanel>: cols:2 Navigation: Images: Config: Information: id: screenManager

Navigation.kv

然后,在您的<Navigation>: Button: text:"Kiwi" size_hint:(.3, .2) pos_hint:{'x':.1, 'y':.25} #on_press: <--- Change screen to kiwi in Information class on_press: app.root.ids.screenManager.current='kiwi' 文件中使用它:

https://github.com/rakudo/rakudo/commit/7bf7a2c6f83a57713c