Kivy 嵌套的 BoxLayout 在左下角堆叠小部件
Kivy nested BoxLayout stacks widgets on bottom left corner
我正在尝试使用垂直 BoxLayout 动态构建 Kivy 布局,其中包含数量不等的自定义 MyRow
小部件,这些小部件可以在运行时更改。
每行都是一个水平的 BoxLayout
我没有使用 GridLayout,因为 MyRow
布局正在开发中,并且可以在不久的将来更改添加小部件等。就像这个例子
但是使用下面的代码,我只能在 window.
的左下角将小部件堆叠在一起
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
class MyRow(Widget):
a = StringProperty('a')
b = StringProperty('b')
def __init__(self, **kwargs):
super(MyRow, self).__init__(**kwargs)
class MainScreen(Widget):
rows = [['a1','b1'],['a2','b2']] #example data
mainLayout = BoxLayout(orientation='vertical', spacing=5)
def __init__(self, **kwargs):
super(MainScreen, self).__init__(**kwargs)
self.add_widget(self.mainLayout)
for r in self.rows:
row_widget = MyRow()
row_widget.a = r[0]
row_widget.b = r[1]
self.mainLayout.add_widget(row_widget)
class MyApp(App):
def build(self):
return MainScreen()
if __name__ == '__main__':
MyApp().run()
这是 kv 文件:
<MyRow>
BoxLayout:
orientation: "horizontal"
spacing: 30
Label:
id: a_label
text: root.a
Label:
id: b_label
text: root.b
在您的草图中,它说 MyRow
基于水平 BoxLayout。但事实并非如此。它基于 widget
简单地改变
class MyRow(Widget):
至
class MyRow(BoxLayout):
将解决您的问题。
为了获得正确或更好的间距,我会将您的代码更新为以下内容
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
from kivy.base import Builder
class MyRow(BoxLayout):
a = StringProperty('a')
b = StringProperty('b')
def __init__(self, **kwargs):
super(MyRow, self).__init__(**kwargs)
class MainScreen(BoxLayout):
rows = [['a1','b1'],['a2','b2']]*5
def __init__(self, **kwargs):
super(MainScreen, self).__init__(**kwargs)
self.orientation = "vertical"
for r in self.rows:
row_widget = MyRow()
row_widget.a = r[0]
row_widget.b = r[1]
self.add_widget(row_widget)
class MyApp(App):
def build(self):
return MainScreen()
if __name__ == '__main__':
MyApp().run()
和你的 kv
<MyRow>:
orientation: "horizontal"
spacing: 30
Label:
id: a_label
text: root.a
Label:
id: b_label
text: root.b
这给你
要从 python 中的 Boxlayot 继承,请在 kv 文件中使用 class Row(BoxLayout):
使用 <Row@BoxLayout>:
我正在尝试使用垂直 BoxLayout 动态构建 Kivy 布局,其中包含数量不等的自定义 MyRow
小部件,这些小部件可以在运行时更改。
我没有使用 GridLayout,因为 MyRow
布局正在开发中,并且可以在不久的将来更改添加小部件等。就像这个例子
但是使用下面的代码,我只能在 window.
的左下角将小部件堆叠在一起from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
class MyRow(Widget):
a = StringProperty('a')
b = StringProperty('b')
def __init__(self, **kwargs):
super(MyRow, self).__init__(**kwargs)
class MainScreen(Widget):
rows = [['a1','b1'],['a2','b2']] #example data
mainLayout = BoxLayout(orientation='vertical', spacing=5)
def __init__(self, **kwargs):
super(MainScreen, self).__init__(**kwargs)
self.add_widget(self.mainLayout)
for r in self.rows:
row_widget = MyRow()
row_widget.a = r[0]
row_widget.b = r[1]
self.mainLayout.add_widget(row_widget)
class MyApp(App):
def build(self):
return MainScreen()
if __name__ == '__main__':
MyApp().run()
这是 kv 文件:
<MyRow>
BoxLayout:
orientation: "horizontal"
spacing: 30
Label:
id: a_label
text: root.a
Label:
id: b_label
text: root.b
在您的草图中,它说 MyRow
基于水平 BoxLayout。但事实并非如此。它基于 widget
简单地改变
class MyRow(Widget):
至
class MyRow(BoxLayout):
将解决您的问题。
为了获得正确或更好的间距,我会将您的代码更新为以下内容
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
from kivy.base import Builder
class MyRow(BoxLayout):
a = StringProperty('a')
b = StringProperty('b')
def __init__(self, **kwargs):
super(MyRow, self).__init__(**kwargs)
class MainScreen(BoxLayout):
rows = [['a1','b1'],['a2','b2']]*5
def __init__(self, **kwargs):
super(MainScreen, self).__init__(**kwargs)
self.orientation = "vertical"
for r in self.rows:
row_widget = MyRow()
row_widget.a = r[0]
row_widget.b = r[1]
self.add_widget(row_widget)
class MyApp(App):
def build(self):
return MainScreen()
if __name__ == '__main__':
MyApp().run()
和你的 kv
<MyRow>:
orientation: "horizontal"
spacing: 30
Label:
id: a_label
text: root.a
Label:
id: b_label
text: root.b
这给你
要从 python 中的 Boxlayot 继承,请在 kv 文件中使用 class Row(BoxLayout):
使用 <Row@BoxLayout>: