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

这是我的代码:
Main.py:

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()


mainPanel.kv:

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

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


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


Information.kv:

#: import  FadeTransition kivy.uix.screenmanager.FadeTransition

<Information>
    transition: FadeTransition()
    SlothFacts:
    KiwiFacts:

<SlothFacts>:
    name: "sloth"
    Label:
        text:"im sloth"

<KiwiFacts>:
    name: "kiwi"
    Label:
        text:"im kiwi"

最佳答案

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'

关于python - 如何从另一个类(class)更改类(class)的属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53691227/

10-13 00:04