【问题标题】:Kivy putting buttons on the corner of layoutKivy 将按钮放在布局的角落
【发布时间】:2016-08-30 10:16:24
【问题描述】:

我是 Kivy 的新手,我已经尝试了几天来寻找合适的布局,但我似乎没有得到结果。 我希望图片中的按钮“2”和“3”保持在按钮“1”和“4”等角落。我该怎么办?

http://i.stack.imgur.com/Y6Rjo.png

这是我的代码,但它不能按需要工作:

# Main
BoxLayout:
    size_hint: 1, .85
    # Outer
    canvas:
        Color:
            rgba: 1, 1, 1, .3
        Rectangle:
            pos: self.pos
            size: self.size
    # Inner
    BoxLayout:
        AnchorLayout:
            canvas:
                Color:
                    rgba: 1, 1, 1, .6
                Rectangle:
                    pos: self.center_x / 2, self.center_y / 2
                    size: self.width / 2, self.height / 2
            BoxLayout:
                size_hint: .5, .5
                AnchorLayout:
                    anchor_x: 'left'
                    anchor_y: 'top'
                    Button:
                        size_hint: None, None
                        text: '1'

                AnchorLayout:
                    anchor_x: 'right'
                    anchor_y: 'top'
                    Button:
                        size_hint: None, None
                        text: '2'

                AnchorLayout:
                    anchor_x: 'left'
                    anchor_y: 'bottom'
                    Button:
                        size_hint: None, None
                        text: '3'

                AnchorLayout:
                    anchor_x: 'right'
                    anchor_y: 'bottom'
                    Button:
                        size_hint: None, None
                        text: '4'

【问题讨论】:

    标签: python-2.7 kivy kivy-language


    【解决方案1】:

    我建议将这些按钮放在相对布局上,然后操纵它们的pos_hint 属性进行定位。截图:

    代码:

    #!/usr/bin/env python3.5
    # -*- coding: utf-8 -*-
    from kivy.app import App
    from kivy.lang import Builder
    
    gui = '''
    Screen
    
        RelativeLayout
            size_hint: None, None
            size: 500, 500
            pos_hint: {'center_x': 0.5, 'center_y': 0.5}
    
            canvas:
                Color:
                    rgba: 1, 1, 1, 0.3
                Rectangle:
                    pos: 0, 0
                    size: self.size
    
            MyButton
                pos_hint: {'left': 1, 'top': 1}
                text: 'top left'
    
            MyButton
                pos_hint: {'right': 1, 'top': 1}
                text: 'top right'
    
            MyButton
                pos_hint: {'left': 1, 'bottom': 1}
                text: 'bottom left'
    
            MyButton
                pos_hint: {'right': 1, 'bottom': 1}
                text: 'bottom right'
    
    <MyButton@Button>
        size_hint: None, None
        size: 100, 100
    '''
    
    
    class Test(App):
    
        def build(self):
            return Builder.load_string(gui)
    
    
    Test().run()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-10
      • 1970-01-01
      • 2021-04-17
      • 2013-10-01
      相关资源
      最近更新 更多