与Kivy / KivyMD接壤

时间:2020-05-28 20:36:13

标签: python kivy kivy-language

我想在我的应用程序的主屏幕上添加边框,但不确定如何。 我试图从这个问题中得到一些提示: Kivy-how can i make my canvas height smaller than the parent height 但我似乎无法弄清楚。

问题是我也在使用KivyMD的导航抽屉,我希望边框与顶部栏分开,将栏下面的所有内容都封闭起来。如果我不清楚,请告诉我。

这里有一些示例代码可以复制我的设置。

也许我可以添加一些随机的矩形作为边框?

编辑:

好了,我已经有了'border',但是现在我需要在size_hint中添加AnchorLayout来忽略菜单栏所在的屏幕顶部。这是更新的代码。

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout

from kivymd.app import MDApp

kv = '''
#:import hex kivy.utils.get_color_from_hex

NavigationLayout:
    canvas.before:
        Color:
            rgb: hex('#C0C0C0')
        Rectangle:
            size: self.size
            pos: self.pos
    ScreenManager:
        id: screen_manager
        Screen:
            name: "home_screen"
            BoxLayout:
                orientation: 'vertical'
                MDToolbar:
                    title: 'Name of the App!'
                    elevation: 10
                Widget:
            FloatLayout:

                orientation: 'vertical'
                AnchorLayout:
                    anchor_x: 'center'
                    anchor_y: 'center'
                    Widget:
                        canvas.before:
                            Color:
                                rgb: hex('#F5F5F5')
                            Rectangle:
                                size: self.size
                                pos: self.pos
                        size_hint: .95, .95
                MDLabel:
                    text: "Some More Text"
                    halign: "center"
                    color: 0,0,0,1
                    pos_hint: {"center_x": .5, "center_y": .75}
                    size_hint: .7, .1

    MDNavigationDrawer:
        id: nav_drawer
        ContentNavigationDrawer:
            orientation: "vertical"
            padding: "8dp"
            spacing: "8dp"
            AnchorLayout:
                anchor_x: "left"
                size_hint_y: None
                height: avatar.height
                Image:
                    id: avatar
                    source: "image.jpg"
            MDLabel:
                text: "Text here"
                font_style: "Button"
                size_hint_y: None
                height: self.texture_size[1]
    '''
class ContentNavigationDrawer(BoxLayout):
    pass

class MyApp(MDApp):
    def build(self):
        return Builder.load_string(kv)


MyApp().run()


1 个答案:

答案 0 :(得分:1)

我认为,如果缩进FloatLayout,使其在BoxLayout中,您将得到想要的。像这样:

#:import hex kivy.utils.get_color_from_hex

NavigationLayout:
    canvas.before:
        Color:
            rgb: hex('#C0C0C0')
        Rectangle:
            size: self.size
            pos: self.pos
    ScreenManager:
        id: screen_manager
        Screen:
            name: "home_screen"
            BoxLayout:
                orientation: 'vertical'
                MDToolbar:
                    title: 'Name of the App!'
                    elevation: 10
                # Widget:  # not needed
                FloatLayout:

                    orientation: 'vertical'
                    AnchorLayout:
                        anchor_x: 'center'
                        anchor_y: 'center'
                        Widget:
                            canvas.before:
                                Color:
                                    rgb: hex('#F5F5F5')
                                Rectangle:
                                    size: self.size
                                    pos: self.pos
                            size_hint: .95, .95
                    MDLabel:
                        text: "Some More Text"
                        halign: "center"
                        color: 0,0,0,1
                        pos_hint: {"center_x": .5, "center_y": .75}
                        size_hint: .7, .1

    MDNavigationDrawer:
        id: nav_drawer
        ContentNavigationDrawer:
            orientation: "vertical"
            padding: "8dp"
            spacing: "8dp"
            AnchorLayout:
                anchor_x: "left"
                size_hint_y: None
                height: avatar.height
                Image:
                    id: avatar
                    # source: "image.jpg"
                    source: 'tester.png'
            MDLabel:
                text: "Text here"
                font_style: "Button"
                size_hint_y: None
                height: self.texture_size[1]