Kivy 在 BoxLayout 中放置一个 FloatLayout
Kivy Placing a FloatLayout inside of a BoxLayout
我正在尝试将 Float Layout 放置在 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:这可以解释为什么标签彼此重叠。
这就是我想要的样子:
它是这样显示的:
您只需要在每个 Label
中添加一个 size_hint_x
。类似于:
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
只占宽度的三分之一左右。
我正在尝试将 Float Layout 放置在 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:这可以解释为什么标签彼此重叠。
这就是我想要的样子:
它是这样显示的:
您只需要在每个 Label
中添加一个 size_hint_x
。类似于:
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
只占宽度的三分之一左右。