为什么向 floatLayout 添加高度会使其从屏幕底部开始添加以下块?
Why adding height to floatLayout will make it start adding following blocks from the bottom of the screen?
我在 kivy 中有以下代码,我想知道为什么 FloatLayout 开始从下往上添加它的子项,我怎样才能让它再次自上而下?
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())
Kivy中的(0,0)坐标在左下方。因此,当您制作 height: 400
时,预计 FloatLayout
会下降而不是上升。
在第二个示例中,您再次设置了FloatLayout
的height: 200
,并将其推到顶部。但是 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
Builder.load_string('''
<SweetWidget>:
canvas:
Color:
rgba: 1,0,0,1
Rectangle:
size: self.size
pos: self.pos
size_hint: 1,None
height: 100
pos_hint: {'top': 0.8}
BoxLayout:
canvas:
Color:
rgba: 0,0,1,1
Rectangle:
size: self.size
pos: self.pos
#pos_hint:
# {'top': 1
# }
orientation: 'vertical'
pos: self.parent.pos
Button:
text: "2"
Button:
text: "6"
''')
我在 kivy 中有以下代码,我想知道为什么 FloatLayout 开始从下往上添加它的子项,我怎样才能让它再次自上而下?
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())
Kivy中的(0,0)坐标在左下方。因此,当您制作 height: 400
时,预计 FloatLayout
会下降而不是上升。
在第二个示例中,您再次设置了FloatLayout
的height: 200
,并将其推到顶部。但是 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
Builder.load_string('''
<SweetWidget>:
canvas:
Color:
rgba: 1,0,0,1
Rectangle:
size: self.size
pos: self.pos
size_hint: 1,None
height: 100
pos_hint: {'top': 0.8}
BoxLayout:
canvas:
Color:
rgba: 0,0,1,1
Rectangle:
size: self.size
pos: self.pos
#pos_hint:
# {'top': 1
# }
orientation: 'vertical'
pos: self.parent.pos
Button:
text: "2"
Button:
text: "6"
''')