【问题标题】:python/kivy : How to pass text value from another class in .kv filepython/kivy:如何从 .kv 文件中的另一个类传递文本值
【发布时间】:2018-01-06 08:45:55
【问题描述】:

我有两个文件 demo.pydemo.kv。当运行 demo.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 次。我的屏幕看起来像 [![在此处输入图片说明][1]][1]
现在我想在 TextBox 中显示值,例如

 Number name  value
    1.  test1 BAG1
    2.  test2 BAG2
    3.  test3 BAG3

有人可以帮我如何将值 root.col_data[3]root.col_data[4] 从另一个类 (class Rows(BoxLayout):) 传递到 for 循环中吗?

demo.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.mode = obj.mode
        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):
        #fetching from database
        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()

demo.kv

<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"
            #padding : 10, 10
            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()

【问题讨论】:

    标签: python python-3.x python-2.7 kivy kivy-language


    【解决方案1】:
    Row().col_data[3] = row[0]
    Row().col_data[4] = row[1]
    

    当您编写 Row() 时,您会创建新的 Row 实例。您想创建一次然后进行修改。

    像这样更改您的 add_row 函数:

    def add_row(self):
        #fetching from database
        rows = [('test1', 'BAG1'), ('test2', 'BAG2'), ('test3', 'BAG3')]
        for row in rows:
            self.row_count += 1
            r = Row(button_text=str(self.row_count))
            r.col_data[3] = row[0]
            r.col_data[4] = row[1]
            self.add_widget(r)
    

    结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-21
      • 1970-01-01
      • 1970-01-01
      • 2018-05-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多