所以我弄清楚如何向Kivy动态添加标签,但后来我意识到了 Kivy已经有了listview模块,这会让事情变得更容易。我发现这个Python Kivy ListView: How to delete selected ListItemButton?答案对于学习实现listview很有用,但我想知道如何添加一个用两个文本框中的文本更新的两列列表视图。我编辑了示例中的代码,作为我想要做的概念的证明,到目前为止我所做的是:
# main.py
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.uix.listview import ListItemButton
class TaskButton(ListItemButton):
pass
class TodoRoot(BoxLayout):
task_input = ObjectProperty()
task_list = ObjectProperty()
def add_task(self):
self.task_list.adapter.data.extend([self.task_input_1.text + " | " + self.task_input_2.text])
self.task_list._trigger_reset_populate()
def del_task(self, *args):
if self.task_list.adapter.selection:
selection = self.task_list.adapter.selection[0].text
self.task_list.adapter.data.remove(selection)
self.task_list._trigger_reset_populate()
class TodoApp(App):
def build(self):
return TodoRoot()
if __name__ == '__main__':
TodoApp().run()
和
# todo.kv
#: import main main
#: import ListAdapter kivy.adapters.listadapter.ListAdapter
#: import ListItemButton kivy.uix.listview.ListItemButton
TodoRoot:
<TodoRoot>:
orientation: "vertical"
task_input_1: task_input_view_1
task_input_2: task_input_view_1
task_list: tasks_list_view
BoxLayout:
size_hint_y: None
height: "40dp"
TextInput:
id: task_input_view_1
size_hint_x: 70
TextInput:
id: task_input_view_2
size_hint_x: 70
Button:
text: "Add"
size_hint_x: 15
on_press: root.add_task()
Button:
text: "Del"
size_hint_x: 15
on_press: root.del_task()
ListView:
id: tasks_list_view
adapter:
ListAdapter(data=[], cls=main.TaskButton)
那种作品,但它有点笨重,我想让两个文本输入中的文本进入列表视图的单独列,但每行仍然可以选择为一行。
答案 0 :(得分:0)
不确定它是否与您要求的完全一致;如果你需要别的东西,写下评论:
test.kv:
#:kivy 1.9.1
Screen:
GridLayout:
size_hint_y: .7
pos_hint: {'y': .3}
id: labels_grid
cols: 2
canvas: # visualize grid layout
Color:
rgb: 0.3, 0.5, 0.3
Rectangle:
size: self.size
pos: self.pos
GridLayout:
size_hint_y: .3
cols: 1
TextInput:
id: input_1
TextInput:
id: input_2
MyButton:
text: 'add labels to grid'
text_1: input_1.text # binding labels text for easy use
text_2: input_2.text # with self.
on_press: self.add_labels(labels_grid)
main.py:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
class MyButton(Button):
def add_labels(self, grid_handle):
text_1 = self.text_1
text_2 = self.text_2
label_1 = Label(text=text_1)
label_2 = Label(text=text_2)
grid_handle.add_widget(label_1)
grid_handle.add_widget(label_2)
class Test(App):
pass
Test().run()