Kivy按钮位置和标签背景颜色

3
我使用Kivy设置了一个网格布局,其中有3个按钮和一个文本区域。这三个按钮没有从0,0开始,标签背景色也没有应用。
以下是我的主要代码:
import kivy
import os
import sys
from kivy.uix.gridlayout import GridLayout
from kivy.app import App
from kivy.lang import Builder

Builder.load_file('exceltoolui.kv')

class checker_ui(GridLayout):
    pass


class Checker(App):
    def build(self):
        return checker_ui()

if __name__ == '__main__':
    Checker().run()

这是我的.kv文件代码

<checker_ui>:
    rows:2
    cols:1
    padding: 10
    spacing: 10
    BoxLayout:
        Button:
            id:this_week_btn
            text: 'This Week Report'
            size_hint:(None,None)
            size: root.width/3,root.height/12
        Button:
            id:last_week_btn
            text: 'Last Week Report'
            size_hint:(None,None)
            size: root.width/3,root.height/12
        Button:
            id:confirm_btn
            text: 'Start Checking'
            size_hint:(None,None)
            size: root.width/3,root.height/12
    BoxLayout:
        Label:
            id:entry
            text:'test'
            font_size:18
            multiline:True
            background_color:1,50,0,1

我的当前输出在按钮上方有一个大黑色空间,标签没有背景颜色。我期望的输出是让按钮从屏幕顶部开始而不是居中。

当前输出


在Kivy中,如果它不是从(0, 0)开始,这意味着什么?坐标(0, 0)是左下角的位置,也许这意味着按钮必须在顶部? - eyllanesc
1个回答

2

当在布局中设置小部件而不为小部件设置size_hint时,布局将设置等距大小,因此请注意两个BoxLayout占据窗口的一半。解决方法是将size_hint_y设置为None,以便将其放置在顶部,并且高度是最小的。

另一方面,如果您想设置背景颜色,则必须使用画布。除了rgba的组件在01的范围内。

对于按钮,宽度必须由布局确定,如果您设置它并且大于布局允许的宽度,则会看到不合适的设计,如图像中的第三个按钮所示,您的样本不响应填充。

<checker_ui>:
    rows:2
    cols:1
    padding: 10
    spacing: 10
    BoxLayout:
        size_hint_y: None
        height: self.minimum_height
        Button:
            id:this_week_btn
            text: 'This Week Report'
            size_hint:(1, None)
            height: root.height/12
        Button:
            id:last_week_btn
            text: 'Last Week Report'
            size_hint:(1, None)
            height: root.height/12
        Button:
            id:confirm_btn
            text: 'Start Checking'
            size_hint:(1, None)
            height: root.height/12
    BoxLayout:
        Label:
            id:entry
            text:'test\nTEST'
            font_size:18
            multiline:True
            canvas.before:
                Color:
                    rgba: 1, .5, 0, 1
                Rectangle:
                    pos: self.pos
                    size: self.size

enter image description here


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