【问题标题】:get value of dynamic add textbox获取动态添加文本框的值
【发布时间】:2017-12-29 11:26:03
【问题描述】:

我有两个文件 test.pytest.kv
在 test.kv text: "+Add More" 添加行动态垂直位置。我在 .py 中创建动态 id

self.add_widget(Row(button_text=str(self.row_count),id=str("test"+str(self.row_count))))

在 .kv 文件中我正在分配 id

id : root.id

谁能告诉我如何在.py 文件中获取所有动态值value1,value2,value3
任何建议或指导将不胜感激..!!

[![在此处输入图片描述][1]][1]

test.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 BooleanProperty, ListProperty, StringProperty, ObjectProperty, NumericProperty

Window.size = (450, 525)


class display(Screen):

    def add_more(self):
        self.ids.rows.add_row()

    def insert_value(self):
        print(Row().id)
        #print(self.ids.)



class Row(BoxLayout):
    button_text = StringProperty("")
    id = ObjectProperty(None)



class Rows(BoxLayout):
    orientation = "vertical"
    row_count = 0

    def __init__(self, **kwargs):
        super(Rows, self).__init__(**kwargs)
        self.add_row()

    def add_row(self):
        self.row_count += 1
        self.add_widget(Row(button_text=str(self.row_count),id=str("test"+str(self.row_count))))


class test(App):

    def build(self):
        self.root = Builder.load_file('test.kv')
        return self.root

if __name__ == '__main__':
    test().run()

test.kv

<Row>:
    #state1 : state1
    orientation: "horizontal"
    spacing: 0, 5

    Button:
        text: root.button_text
        size_hint_x: .2

    TextInput:
        #text : root.id
        size_hint_x: .8
        id : root.id

display:

    BoxLayout:
        orientation: "vertical"
        padding : 20, 20

        BoxLayout:
            orientation: "horizontal"

            Button:
                size_hint_x: .2
                text: "+Add More"
                valign: 'bottom'
                on_press: root.add_more()


        BoxLayout:
            orientation: "horizontal"

            Label:
                size_hint_x: .2
                text: "SN"
                valign: 'bottom'

            Label:
                size_hint_x: .8
                text: "Value"
                valign: 'bottom'


        Rows:
            id: rows

        BoxLayout:
            orientation: "horizontal"
            padding : 10, 0
            spacing: 10, 10
            size_hint: .5, .7
            pos_hint: {'x': .25, 'y':.25}

            Button:
                text: 'Ok'
                on_release:
                    root.insert_value()

            Button:
                text: 'Cancel'
                on_release: root.dismiss()

【问题讨论】:

    标签: python kivy kivy-language


    【解决方案1】:

    首先你必须访问Rows,因为他有一个id,所以我们使用ids:

        Rows:
            id: rows
    

    然后你通过孩子访问每个Row,因为当你使用add_widget时存储在这个ListProperty中,对Row也是如此,但isinstance()用于过滤TextInput,之后文本是获得并添加到列表中。

    需要注意的是,最大的孩子在最后,因为reversed()被使用了。

    from kivy.uix.textinput import TextInput
    [...]
    
        def insert_value(self):
            values = []
            rows = self.ids.rows
            for row in reversed(rows.children):
                for ch in row.children:
                    if isinstance(ch, TextInput):
                        values.append(ch.text)
            print(values)
    

    【讨论】:

    • 感谢您的 Efford 对我帮助很大:)
    猜你喜欢
    • 1970-01-01
    • 2019-03-27
    • 1970-01-01
    • 2019-11-08
    • 2013-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多