我是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'
答案 0 :(得分: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()