如何从另一个类更改类的属性

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


这是我的代码: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()

主面板.kv:


#:include navigation.kv

#:include images.kv

#:include config.kv

#:include information.kv


<MainPanel>:

    cols:2

    Navigation:

    Images:

    Config:

    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

资料.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"


倚天杖
浏览 218回答 1
1回答

慕妹3146593

在文件中添加一个id,例如:ScreenManagermainPanel.kv#:include navigation.kv#:include images.kv#:include config.kv#:include information.kv<MainPanel>:&nbsp; &nbsp; cols:2&nbsp; &nbsp; Navigation:&nbsp; &nbsp; Images:&nbsp; &nbsp; Config:&nbsp; &nbsp; Information:&nbsp; &nbsp; &nbsp; &nbsp; id: screenManager然后,在您的Navigation.kv文件中使用它:<Navigation>:&nbsp; &nbsp; Button:&nbsp; &nbsp; &nbsp; &nbsp; text:"Kiwi"&nbsp; &nbsp; &nbsp; &nbsp; size_hint:(.3, .2)&nbsp; &nbsp; &nbsp; &nbsp; pos_hint:{'x':.1, 'y':.25}&nbsp; &nbsp; &nbsp; &nbsp; #on_press: <--- Change screen to kiwi in Information class&nbsp; &nbsp; &nbsp; &nbsp; on_press: app.root.ids.screenManager.current='kiwi'
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python