获取动态添加文本框的值 [英] get value of dynamic add textbox

查看:56
本文介绍了获取动态添加文本框的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two file test.py and test.kv
In test.kv text: "+Add More" which add row dynamic vertical position.I am creating dynamic id in .py

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

在.kv文件中,我正在分配ID

and in .kv file i am assigning id

id : root.id

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

Can anyone tell me how to get all dynamic value value1,value2,value3 in .py file
Any advise or guidance would be greatly appreciated..!!

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

[![enter image description here][1]][1]

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()

推荐答案

首先,您必须访问 Rows ,因为我们使用了id,因为他具有id:

First you must access Rows for it we use ids since he has an id:

    Rows:
        id: rows

然后,您将通过子级访问每个 Row ,因为当您使用add_widget时,该行存储在此 ListProperty 中,并且对Row进行相同的操作,但 isinstance()用于过滤 TextInput ,之后获取文本并将其添加到列表中.

Then you access each Row through children since when you use add_widget is stored in this ListProperty, and the same is done with Row but isinstance() is used to filter the TextInput, after that the text is obtained and added to the list.

应该注意的是,由于使用了 reversed(),年龄最大的孩子在最后.

It should be noted that the oldest children are at the end because of it reversed() is used.

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)

这篇关于获取动态添加文本框的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆