Kivy背景色

问题描述

想要设置背景色,但是BoxLayout直接生成的背景色只在左下角占据了一小块,尺寸是[100,100]

代码

class RootWidget(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.orientation = 'horizontal'
        print('BoxLayout.size: %s' % self.size)
        with self.canvas.before:
            Color(1,0,0,1)
            self.rect = Rectangle(size=self.size,
                                  pos=self.pos)
                                  
class TestApp(App):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.title = 'QuickBOM'  # 更改标题文本
    def build(self):
        root = RootWidget()
        root.add_widget(Label(text='中文'))
        root.add_widget(Button(text='试试看'))
        root.add_widget(TextInput())
        return root
        
TestApp().run()

Kivy BoxLayout默认尺寸(100,100)

解决方案一

参考说明文档,绑定事件刷新事件

代码

class RootWidget(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.orientation = 'horizontal'
        print('BoxLayout.size: %s' % self.size)
        self.bind(size=self._update_rect, pos=self._update_rect)
        with self.canvas.before:
            Color(1,0,0,1)
            self.rect = Rectangle(size=self.size,
                                  pos=self.pos)
    def _update_rect(self, instance, value):
        self.rect.pos = instance.pos
        self.rect.size = instance.size

Kivy BoxLayout背景色方案1

解决方案二

利用Window.size

代码

class RootWidget(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.orientation = 'horizontal'
        print('Window.size: %s' % str(Window.size))
        with self.canvas.before:
            Color(1,0,0,1)
            self.rect = Rectangle(size=Window.size,
                                  pos=self.pos)

Kivy BoxLayout背景色方案2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值