在循环中创建 Kivy 小部件
Creating Kivy widgets in a loop
我有一个 kivy 屏幕,我需要通过循环在其中创建元素。我可以这样做:
class HomeScreen(Screen):
def show_tasks(self):
global user
tasks = DB.get_tasks(user) # Returns an array of tuples
for task in tasks:
self.add_widget(Label(text=task[1]))
但是,当我这样做时,标签相互重叠 - 实际上是在 z 轴上,使它们都不可读。相反,我希望他们将一个填充在另一个之上(在 y 轴上)。不仅如此,最终我还想从数据中创建一个类似于 table 的结构。
这是我的 kv:
<HomeScreen>:
name: 'home'
FloatLayout:
BoxLayout:
orientation: "horizontal"
pos_hint: {"x": 0, "y": 0}
GridLayout:
id: grid
rows: 4
cols: 1
padding: 10
spacing: 10
row_force_default: True
row_default_height: 40
Label:
text: 'Your Tasks:'
size_hint_x: None
width: 200
font_size: 24
任何帮助或深入了解我如何解决这个问题都将不胜感激!
在下面的代码中:
self.add_widget(Label(text=task[1]))
self
指的是 HomeScreen
class 的实例,因此您要将 child 添加到 HomeScreen
,而不是 GridLayout
,也就是说,它相当于:
<HomeScreen>:
name: 'home'
FloatLayout:
...
Label:
...
Label:
...
解决办法是在GridLayout
中添加一个class的属性网格来做到这一点:
<HomeScreen>:
name: 'home'
grid: grid # <---
FloatLayout:
BoxLayout:
orientation: "horizontal"
pos_hint: {"x": 0, "y": 0}
GridLayout:
id: grid
# rows: 4
cols: 1
padding: 10
spacing: 10
row_force_default: True
row_default_height: 40
Label:
text: 'Your Tasks:'
size_hint_x: None
width: 200
font_size: 24
然后我们在 python 部分使用 grid
添加它:
class HomeScreen(Screen):
def show_tasks(self):
global user
tasks = DB.get_tasks(user) # Returns an array of tuples
for task in tasks:
self.grid.add_widget(Label(text=task[1])) # <---
我有一个 kivy 屏幕,我需要通过循环在其中创建元素。我可以这样做:
class HomeScreen(Screen):
def show_tasks(self):
global user
tasks = DB.get_tasks(user) # Returns an array of tuples
for task in tasks:
self.add_widget(Label(text=task[1]))
但是,当我这样做时,标签相互重叠 - 实际上是在 z 轴上,使它们都不可读。相反,我希望他们将一个填充在另一个之上(在 y 轴上)。不仅如此,最终我还想从数据中创建一个类似于 table 的结构。
这是我的 kv:
<HomeScreen>:
name: 'home'
FloatLayout:
BoxLayout:
orientation: "horizontal"
pos_hint: {"x": 0, "y": 0}
GridLayout:
id: grid
rows: 4
cols: 1
padding: 10
spacing: 10
row_force_default: True
row_default_height: 40
Label:
text: 'Your Tasks:'
size_hint_x: None
width: 200
font_size: 24
任何帮助或深入了解我如何解决这个问题都将不胜感激!
在下面的代码中:
self.add_widget(Label(text=task[1]))
self
指的是 HomeScreen
class 的实例,因此您要将 child 添加到 HomeScreen
,而不是 GridLayout
,也就是说,它相当于:
<HomeScreen>:
name: 'home'
FloatLayout:
...
Label:
...
Label:
...
解决办法是在GridLayout
中添加一个class的属性网格来做到这一点:
<HomeScreen>:
name: 'home'
grid: grid # <---
FloatLayout:
BoxLayout:
orientation: "horizontal"
pos_hint: {"x": 0, "y": 0}
GridLayout:
id: grid
# rows: 4
cols: 1
padding: 10
spacing: 10
row_force_default: True
row_default_height: 40
Label:
text: 'Your Tasks:'
size_hint_x: None
width: 200
font_size: 24
然后我们在 python 部分使用 grid
添加它:
class HomeScreen(Screen):
def show_tasks(self):
global user
tasks = DB.get_tasks(user) # Returns an array of tuples
for task in tasks:
self.grid.add_widget(Label(text=task[1])) # <---