我在kivy中有以下代码,我想知道为什么Float Layout开始从下往上添加它的孩子,我怎样才能让它再次自上而下?
from kivy.app import App
from kivy.lang import Builder
from kivy.base import runTouchApp
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
Builder.load_string('''
<SweetWidget>:
size_hint: 1, None
height: 400
BoxLayout:
orientation: 'vertical'
Button:
text: "1"
Button:
text: "2"
Button:
text: "3"
Button:
text: "4"
Button:
text: "5"
Button:
text: "6"
''')
class SweetWidget(FloatLayout):
pass
runTouchApp(SweetWidget())
如果我在FloatLayout中使用pos_hint {'top':1}
,那么最奇怪的事情就会发生from kivy.app import App
from kivy.lang import Builder
from kivy.base import runTouchApp
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.graphics.vertex_instructions import Rectangle
import sys
Builder.load_string('''
<SweetWidget>:
canvas:
Color:
rgba: 1,0,0,1
Rectangle:
size: self.size
pos: self.pos
size_hint: 1,None
height: 200
pos_hint: {'top': 1}
BoxLayout:
canvas:
Color:
rgba: 0,0,1,1
Rectangle:
size: self.size
pos: self.pos
#pos_hint:
# {'top': 1
# }
orientation: 'vertical'
Button:
text: "2"
Button:
text: "6"
''')
class SweetWidget(FloatLayout):
def build(self):
data = sys.stdin.readline()
print("Counted", len(data), "lines.")
pass
runTouchApp(SweetWidget())
答案 0 :(得分:1)
Kivy中的(0,0)坐标位于左下角。因此,当您创建height: 400
时,预计FloatLayout
将会下降而不会上升。
在第二个示例中,您再次设置height: 200
的{{1}},并将其推到顶部。但是,FloatLayout
仍然使用绝对值(您可能想尝试FloatLayout
),因此您必须添加RelativeLayout
属性才能重新定位pos
以下是修复示例的两种方法。我在下面,我将高度重新定位到BoxLayout
并将其移到顶部。
Boxlayout
在这篇文章中,我将介绍Builder.load_string('''
<SweetWidget>:
BoxLayout:
canvas:
Color:
rgba: 0,0,1,1
Rectangle:
size: self.size
pos: self.pos
height: 400
size_hint: 1, None
pos_hint: {'top': 1}
orientation: 'vertical'
Button:
text: "1"
Button:
text: "2"
Button:
text: "3"
Button:
text: "4"
Button:
text: "5"
Button:
text: "6"
''')
pos: self.parent.pos