KivyMD - 未添加按钮

问题描述 投票:0回答:1

我正在尝试为我的侄女制作一个简单的刽子手游戏(并练习一点 kivymd)。我输入了典型的起始代码,但屏幕上没有添加任何内容。 .py 文件是:

from kivymd.app import MDApp
from kivy.lang import Builder
from kivymd.uix.screenmanager import MDScreenManager
from kivymd.uix.screen import MDScreen


class AhorcadoScreen(MDScreen):
    pass


class MainScreen(MDScreen):
    pass


class WindowManager(MDScreenManager):
    pass


class JuegodePalabrasApp(MDApp):
    
    def build(self):
        return Builder.load_file("juegodepalabras.kv")
    
if __name__ == "__main__":
    JuegodePalabrasApp().run()

我尝试了 3 个不同的 kivy 文件。 第一个是

WindowManager:
    MainScreen:
    AhorcadoScreen:

<MainScreen>:
    name: "mainscreen"

    
    MDScrollView:
        md_bg_color: "white"

        MDButton:
            style: "elevated"

第二个是:

WindowManager:
    MainScreen:
    AhorcadoScreen:

<MainScreen>:
    name: "mainscreen"
    

    MDScreen:
        MDScrollView:
            md_bg_color: "white"

            MDButton:
                style: "elevated"
                pos_hint: {"center_x": .5, "center_y": .5}

                MDButtonText:
                    text: "Ahorcado"

还有最后一个

WindowManager:
    MainScreen:
    AhorcadoScreen:

<MainScreen>:
    name: "mainscreen"

    MDBoxLayout:
        orientation: "vertical"
        size: root.width, root.height
        md_bg_color: "white"

        MDScreen:
            MDScrollView:
                md_bg_color: "white"

                MDButton:
                    style: "elevated"
                    pos_hint: {"center_x": .5, "center_y": .5}

                    MDButtonText:
                        text: "Ahorcado"

但在所有情况下,我得到的只是白屏。我用另一个程序做了同样的事情(当然,更改了类的名称),没有问题。

我使用与其他程序相同的 venv,不确定这是否有问题。

有什么想法为什么会发生这种情况吗?

python button scrollview kivymd boxlayout
1个回答
0
投票

您可以通过添加根小部件(应用程序的主小部件)来解决此问题,该小部件稍后将允许应用程序显示其他屏幕,一些细微的更改,将所有内容添加到一个文件中(您可以将它们分开),结果: enter image description here

完整代码:

from kivymd.app import MDApp
from kivy.lang import Builder
from kivymd.uix.screen import MDScreen



the_game="""
Screen:
    ScreenManager:
        id: mainx
        Screen:
            name: "home"

            BoxLayout:
                canvas.before:
                    Color:
                        rgba: (1,1,1, .9)
                    Rectangle:
                        pos: self.pos
                        size: self.size
                orientation: "vertical"
                size: root.width, root.height
                

                BoxLayout:
                    id: m5
                    spacing: dp(10)
                    padding: dp(20)
                    pos_hint: {"center_x": .55, "center_y": .4}
                    orientation: "vertical"

                    BoxLayout:
                        id: s_res2
                        size_hint_y: None
                        height: self.minimum_height
                        pos_hint: {"center_x": .5, "center_y": .7}
                        orientation: "vertical"
                    ScrollView:
                        id: maaa
                        do_scroll_x: False
                        do_scroll_y: True
                        pos_hint:{'center_x': .5,'center_y': .39}
                        size_hint_y: 10
                        BoxLayout:
                            canvas.before:
                                Color:
                                    rgba: [0,0,1,.2] # if white [1,1,1,1]
                                RoundedRectangle:
                                    radius: [30,]
                                    pos: self.pos
                                    size: self.size
                            id: rvvv2
                            orientation: "vertical"
                            key_size: 'height'
                            size_hint_y: None
                            size_hint_x: 0.9
                            height: self.minimum_height+45
                            padding: dp(20)
                            spacing: dp(8)
                            adaptive_height: True
                            BoxLayout:
                                MDIconButton:
                                    style: "elevated"
                                    icon: "home"
                                    pos_hint: {"center_x": .5, "center_y": .5}

                                MDLabel:
                                    style: "H5"
                                    text: "Ahorcado"
                                    pos_hint: {"center_x": .5, "center_y": .5}
                            BoxLayout:
                                MDIconButton:
                                    style: "elevated"
                                    icon: "battery"
                                    pos_hint: {"center_x": .5, "center_y": .5}

                                MDLabel:
                                    style: "H5"
                                    text: "Snake Game"
                                    pos_hint: {"center_x": .5, "center_y": .5}
            FloatLayout:
                MDIconButton:
                    canvas.before:
                        Color:
                            rgba: [1,0,0,.2] # if white [1,1,1,1]
                        RoundedRectangle:
                            radius: [30,]
                            pos: self.pos
                            size: self.size
                    icon: "plus"
                    pos_hint: {"center_x": .8, "center_y": .1}
                    on_release: app.root.ids.mainx.current="ahorcado"

        Screen:
            name: "ahorcado"
            canvas.before:
                Color:
                    rgba: [0,1,0,.2] # if white [1,1,1,1]
                RoundedRectangle:
                    radius: [30,]
                    pos: self.pos
                    size: self.size
            MDCard:
            FloatLayout:
                MDIconButton:
                    canvas.before:
                        Color:
                            rgba: [1,0,0,.2] # if white [1,1,1,1]
                        RoundedRectangle:
                            radius: [30,]
                            pos: self.pos
                            size: self.size
                    icon: "plus"
                    pos_hint: {"center_x": .8, "center_y": .1}
                    on_release: app.root.ids.mainx.current="home"


"""

class JuegodePalabrasApp(MDApp):
    
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
    def build(self):
        global app
        app=MDApp.get_running_app()
        return Builder.load_string(the_game)
    
if __name__ == "__main__":
    JuegodePalabrasApp().run()
© www.soinside.com 2019 - 2024. All rights reserved.