首页 > 其他 > 详细

relativeLayout相对布局的嵌套在py中的引用

时间:2021-04-01 23:21:14      阅读:23      评论:0      收藏:0      [点我收藏+]
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.graphics import Color,Rectangle


class MyButton(Button):
    """自定义一个按扭,公共属性
    """
    def __init__(self,**kwargs):
        super().__init__(**kwargs)
        self.font_size = 20 # 指定按扭的大小
        self.size_hint = [0.3,0.3]   # 按扭大小


# 布局类用于BoxLayout子布局
class RelativeLayoutWidget(RelativeLayout):
    pass


# 父布局
class BoxLayoutWidget(BoxLayout):
    def __init__(self,**kwargs):
        super().__init__(**kwargs)
        # 设置颜色
        with self.canvas:
            Color(1,1,1,1)
            self.rect = Rectangle(pos = self.pos,size=self.size)
            self.bind(pos=self.update_rect,size=self.update_rect)
        # 创建一个relative布局
        relative_layout= RelativeLayout()
        # 使用自定义按扭

        bt0 = MyButton(text=bt1,pos_hint={right:1,top:1},background_color=(0.1,0.5,0.6,1))   # 正确
        bt1 = MyButton(text=bt2,pos_hint={x:0,top:1},background_color=(.1,.5,.6,1))
        bt3 = MyButton(text=relative,pos_hint={center_x:.5,center_y:.5},background_color=(0.4,0.5,.6,1))
        bt4 = MyButton(text=bt4,pos_hint={x:0,y:0},background_color=(0,0,1,1))
        bt5 = MyButton(text=bt5, pos_hint={right: 1, y: 0}, background_color=(0.8,0.8,0.2,1))

        # 都加进新的相对布局中去
        for i in [bt0,bt1,bt3,bt4,bt5]:
            relative_layout.add_widget(i)
        # 占个位
        self.add_widget(BoxLayout())
        # 把这个新的相对布局加到Boxlayout中
        self.add_widget(relative_layout)

    # 此处多值参数多次忘记!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    def update_rect(self,*args):
        self.rect.pos=self.pos
        self.rect.size=self.size


class RelativeApp(App):
    def build(self):
        # 返回父布局
        return BoxLayoutWidget()


if __name__ == __main__:
    RelativeApp().run()

 

relativeLayout相对布局的嵌套在py中的引用

原文:https://www.cnblogs.com/vip136510786/p/14607297.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!