有没有办法在kivy BoxLayout周围加上边框?

5

我正在设计一个应用程序的主菜单,但是我似乎无法弄清楚如何在Kivy中为BoxLayout设置边框,有没有办法可以做到这一点?如果可以,有人可以帮忙吗?我已经尝试使用canvas.before并给矩形加上边框,但这并不起作用。

<MainMenuWindow>:
    id: Main_Menu_window  ## setting the id to Login_window

    BoxLayout: ## whole screen
        orientation: 'horizontal'

        BoxLayout: ## left hand side 1/3
            canvas.before:
                Color:
                    rgba: 0, 0, 0, 1
                Line:
                    width:
                    rectangle: self.x, self.y, self.width, self.height

            orientation: 'vertical' ## setting orientation of the screen to vertical
            size_hint_x: 1/3 ## setting size of box to 1/3 of the screen
            canvas.before:
                Color: ## just for debugging reasons
                    rgba: (255/255,255/255,255/255, 1)  ## setting colour
                Rectangle:
                    size: self.size  ## setting size to the size of the box layout
                    pos: self.pos  ## setting position to the position of the box layout


如果将 rgba 更改为 1,0,0,1 会发生什么? - John Anderson
@JohnAnderson 这个并没有做任何事情。我认为canvas.before在这里根本不起作用。还有其他什么我可以尝试的吗? - Tyler Channer
请发布一个 [mcve]。 - John Anderson
@JohnAnderson 这样好一些吗? - Tyler Channer
1个回答

4
BoxLayout 会分配一定的空间给每个子部件,以利用其所有可用空间。由于您内部的 BoxLayout 是外部 BoxLayout 的唯一子部件,因此它将被分配外部 BoxLayout 的整个空间。因此,您可以通过调整外部 BoxLayout 的大小或显式设置内部 BoxLayout 的大小(使用 size_hint:None,None)来控制内部 BoxLayout 的大小。
因此,以下是将内部 BoxLayout 设置为屏幕宽度的三分之一的方法:
<MainMenuWindow>:
    id: Main_Menu_window  ## setting the id to Login_window

    BoxLayout: ## whole screen
        orientation: 'horizontal'

        BoxLayout: ## left hand side 1/3
            canvas.before:
                Color:
                    rgba: 1, 0, 0, 1
                Line:
                    width: 20
                    rectangle: self.x, self.y, self.width, self.height

            orientation: 'vertical' ## setting orientation of the screen to vertical
        Label:  ## middle third
            text: 'Abba'
        Label:  ## right third
            text: 'Dabba'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接