0

我知道 kivy 和 kivymd 但我还不知道它们的功能。我想在单击按钮时更改我的应用程序的屏幕,我该怎么做?谢谢你。

下面是我的代码


from kivy.lang import Builder

from kivymd.app import MDApp

KV = '''

Screen:

    MDLabel: 
        text: "Material Design"
        font_style: "H3"
        theme_text_color: "Secondary"
        pos_hint: {"center_y": 0.9}
        halign: "center"
        
    MDRaisedButton:
        text: "Start"
        md_bg_color: 0, 0.26, 0.27, 1
        pos_hint: {"center_x": 0.5, "center_y": 0.2}
        on_press: root.manager.current = "main"

    MDRectangleFlatButton:
        text: "Exit"
        md_bg_color: rgba(197, 232, 204, 1)
        text_color: 0, 0.26, 0.27, 1
        pos_hint: {"center_x": 0.5, "center_y": 0.1}

        Screen:
            name: "main"

            MDLabel:
                text: "School guru"
                font_style: "H3"
                pos_hint: {"center_y": 0.8}
                halign: "center"
'''


class MaterialDesign(MDApp):
    def build(self):
        return Builder.load_string(KV)


MaterialDesign().run()

请尝试编辑我的代码并展示如何操作。

再次感谢您。

4

1 回答 1

1

首先,如果你要在 之间切换Screens,你需要一个ScreenManager.

这是您的代码的修改版本:

from kivy.lang import Builder

from kivymd.app import MDApp

KV = '''
ScreenManager:
    Screen:
    
        MDLabel: 
            text: "Material Design"
            font_style: "H3"
            theme_text_color: "Secondary"
            pos_hint: {"center_y": 0.9}
            halign: "center"
    
        MDRaisedButton:
            text: "Start"
            md_bg_color: 0, 0.26, 0.27, 1
            pos_hint: {"center_x": 0.5, "center_y": 0.2}
            on_press: root.current = "main"
    
        MDRectangleFlatButton:
            text: "Exit"
            md_bg_color: rgba(197, 232, 204, 1)
            text_color: 0, 0.26, 0.27, 1
            pos_hint: {"center_x": 0.5, "center_y": 0.1}
    
    Screen:
        name: "main"
    
        MDLabel:
            text: "School guru"
            font_style: "H3"
            pos_hint: {"center_y": 0.8}
            halign: "center"
'''


class MaterialDesign(MDApp):
    def build(self):
        return Builder.load_string(KV)


MaterialDesign().run()

所以ScreenManager成为 的根App,它的孩子是Screens。的on_press财产MDRaisedButton现在只是root.current = "main"

于 2020-08-06T14:05:01.753 回答