我正在尝试将浮动版式放置在Boxlayout内。当我尝试此操作时,内部的标签会相互堆叠。我究竟做错了什么?谢谢!
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
def add_entry(bl):
fl = FloatLayout()
# add label left
_lbl = Label()
_lbl.id = '_lbl0'
_lbl.text = 'LEFT'
_lbl.pos_hint = {'x': 0, 'center_y': .5}
fl.add_widget(_lbl)
# add label center
_lbl1 = Label()
_lbl1.id = '_lbl1'
_lbl1.text = 'CENTER'
_lbl1.pos_hint = {'center_x': .5, 'center_y': .5}
fl.add_widget(_lbl1)
# add label right
_lbl2 = Label()
_lbl2.id = '_lbl2'
_lbl2.text = 'RIGHT'
_lbl2.pos_hint = {'right': 1, 'center_y': .5}
fl.add_widget(_lbl2)
bl.add_widget(fl)
class MyApp(App):
def build(self):
bl = BoxLayout()
bl.orientation = 'vertical'
for g in range(3):
add_entry(bl)
return bl
if __name__ == '__main__':
MyApp().run()
我认为原因是FloatLayout的大小。尺寸似乎为0:这可以解释为什么标签彼此重叠。
您只需要向每个size_hint_x
添加一个Label
。类似于:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
def add_entry(bl):
fl = FloatLayout()
# add label left
_lbl = Label()
_lbl.id = '_lbl0'
_lbl.text = 'LEFT'
_lbl.size_hint_x = 0.3
_lbl.pos_hint = {'x': 0, 'center_y': .5}
fl.add_widget(_lbl)
# add label center
_lbl1 = Label()
_lbl1.id = '_lbl1'
_lbl1.text = 'CENTER'
_lbl1.size_hint_x = 0.3
_lbl1.pos_hint = {'center_x': .5, 'center_y': .5}
fl.add_widget(_lbl1)
# add label right
_lbl2 = Label()
_lbl2.id = '_lbl2'
_lbl2.text = 'RIGHT'
_lbl2.size_hint_x = 0.3
_lbl2.pos_hint = {'right': 1, 'center_y': .5}
fl.add_widget(_lbl2)
bl.add_widget(fl)
class MyApp(App):
def build(self):
bl = BoxLayout()
bl.orientation = 'vertical'
for g in range(3):
add_entry(bl)
return bl
if __name__ == '__main__':
MyApp().run()
[默认size_hint
为(1, 1)
,因此每个Label
都会尝试填充FloatLayout
的整个宽度。通过将size_hint_x
设置为0.3
,每个Label
仅占据宽度的三分之一。