我有两个文件
demo.py
和
demo.kv
.运行时
演示。py公司
然后显示3行。因为我正在从数据库中提取数据。它的表现就像。
rows = [('test1', 'BAG1'), ('test2', 'BAG2'), ('test3', 'BAG3')]
for row in rows:
self.row_count += 1
self.add_widget(Row(button_text=str(self.row_count)))
所以迭代重复3次。我的sreen看起来像
[![在此输入图像描述][1][1]
现在我想在文本框中显示值
Number name value
1. test1 BAG1
2. test2 BAG2
3. test3 BAG3
有人能帮我传递价值吗
root.col_data[3]
和
root.col_data[4]
来自另一个类的in-for循环(
class Rows(BoxLayout):
)?
演示。py公司
from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ListProperty, StringProperty
Window.clearcolor = (0.5, 0.5, 0.5, 1)
Window.size = (500, 400)
class User(Screen):
def add_more(self):
self.ids.rows.add_row()
class Row(BoxLayout):
col_data = ListProperty(["?", "?", "?", "?", "?"])
button_text = StringProperty("")
def __init__(self, **kwargs):
super(Row, self).__init__(**kwargs)
self.col_data[0] = ''
self.col_data[1] = ''
self.col_data[2] = ''
self.col_data[3] = ''
self.col_data[4] = ''
class Rows(BoxLayout):
row_count = 0
def __init__(self, **kwargs):
super(Rows, self).__init__(**kwargs)
self.add_row()
def add_row(self):
rows = [('test1', 'BAG1'), ('test2', 'BAG2'), ('test3', 'BAG3')]
for row in rows:
Row().col_data[3] = row[0]
Row().col_data[4] = row[1]
self.row_count += 1
self.add_widget(Row(button_text=str(self.row_count)))
class Test(App):
def build(self):
self.root = Builder.load_file('demo.kv')
return self.root
if __name__ == '__main__':
Test().run()
演示。千伏
<Row>:
size_hint_y: None
height: self.minimum_height
height: 40
Button:
text: root.button_text
size_hint_x: None
top: 200
TextInput:
text: root.col_data[3]
width: 300
TextInput:
text: root.col_data[4]
width: 300
<Rows>:
size_hint_y: None
height: self.minimum_height
orientation: "vertical"
User:
BoxLayout:
orientation: "vertical"
padding : 20, 5
BoxLayout:
orientation: "horizontal"
spacing: 10, 10
size: 450, 40
size_hint: None, None
Label:
size_hint_x: .2
text: "Number"
text_size: self.size
valign: 'bottom'
halign: 'center'
Label:
size_hint_x: .4
text: "name"
text_size: self.size
valign: 'bottom'
halign: 'center'
Label:
size_hint_x: .4
text: "Value"
text_size: self.size
valign: 'bottom'
halign: 'center'
ScrollView:
Rows:
id: rows
BoxLayout:
orientation: "horizontal"
size_hint_x: .2
size_hint_y: .2
Button:
text: "+Add More"
on_press: root.add_more()