Python 3.x 如何为特定小部件子项设置值

Python 3.x 如何为特定小部件子项设置值,python-3.x,kivy,Python 3.x,Kivy,大家好,我是kivy的新手,我正在编写一个小代码,它将获取输入值并将其设置为widget子项值 目标是,每当我单击add按钮时,第一行小部件将被添加,并获取TextInput的值。如果我更改TextInput值并单击add按钮,将添加第二行小部件,它将具有新的TextInput值 请参阅我的代码: from kivy.uix.widget import Widget from kivy.uix.boxlayout import BoxLayout from kivy.uix.textinput

大家好,我是kivy的新手,我正在编写一个小代码,它将获取输入值并将其设置为widget子项值

目标是,每当我单击add按钮时,第一行小部件将被添加,并获取TextInput的值。如果我更改TextInput值并单击add按钮,将添加第二行小部件,它将具有新的TextInput值

请参阅我的代码:

from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.button import Button


kv_string = Builder.load_string(''' 

<CustomRemBtn>:
    Button:
        id: remBtn
        text: 'Remove'

<CostumWidget>:
    TextInput:
        size_hint_x: None
        width: 150
    TextInput:
        size_hint_x: None
        width: 150

<MScreen>:
    FloatLayout:
        Button:
            pos: root.x,root.top-self.height
            id: main_add
            text: 'Add'
            size_hint: .1,.05
            on_release: root.add_customWidget(True)
        TextInput:
            pos: root.x,root.top-main_add.height-self.height
            id: textIn
            size_hint: .3,.05

            ''')

class CustomRemBtn(BoxLayout):
    def remove(self):
        self.parent.removeFunction()

class CostumWidget(BoxLayout):
    def labelers(self):
        self.parent.floatbase()

class Float_Layout(FloatLayout):
    def floatbase(self):
        self.parent.add_customWidget()
    def removeFunction(self):
        self.parent.remove_customWidget()

class AddRem(Widget):
    def __init__(self,**kwargs):
        super(AddRem,self).__init__(**kwargs)
        self.orientation = "vertical"
        self.count = 0
        self.customLayout = Float_Layout()
        self.add_widget(self.customLayout)

class MScreen(Screen,AddRem):
    def add_customWidget(self,*args):
        self.y1 = (self.top - ((self.count+1)*40)-110)

        self.count += 1
        self.custWidgets = CostumWidget(size_hint=(.1,.05), pos=(100, 480-(self.count*30))) 
        self.removeBtn = CustomRemBtn(size_hint=(.1,.05), pos=(400, 480-(self.count*30)))

        if self.count == 1:
            self.customLayout.add_widget(self.custWidgets)
            self.customLayout.add_widget(self.removeBtn)
        else:
            self.customLayout.remove_widget(self.customLayout.children[0])
            self.customLayout.add_widget(self.custWidgets)
            self.customLayout.add_widget(self.removeBtn)

        for i2 in self.customLayout.children:
            for x in range(len(i2.children)):
                i2.children[x].text=self.ids.textIn.text
                print(i2.children[x].text)


class myApp1(App):
    def build(self):
        return SManage

SManage = ScreenManager()
SManage.add_widget(MScreen())

if __name__ == "__main__":
    myApp1().run()
来自kivy.uix.widget导入小部件
从kivy.uix.boxlayout导入boxlayout
从kivy.uix.textinput导入textinput
从kivy.uix.floatlayout导入floatlayout
从kivy.uix.label导入标签
从kivy.app导入应用程序
从kivy.uix.screenmanager导入screenmanager,屏幕
从kivy.lang导入生成器
从kivy.uix.button导入按钮
kv_串=Builder.load_串(“”)
:
按钮:
id:remBtn
文本:“删除”
:
文本输入:
大小提示:无
宽度:150
文本输入:
大小提示:无
宽度:150
:
浮动布局:
按钮:
位置:root.x,root.top-self.height
id:main_add
文本:“添加”
大小提示:.1.05
发布时:root.add\u customWidget(True)
文本输入:
位置:root.x,root.top-main\u add.height-self.height
id:textIn
尺寸提示:.3.05
''')
类别CustomRemBtn(BoxLayout):
def移除(自):
self.parent.removeFunction()函数
类CostumWidget(BoxLayout):
def标签机(自身):
self.parent.floatbase()
类浮动布局(浮动布局):
def浮动底座(自身):
self.parent.add_customWidget()
def移除功能(自):
self.parent.remove_customWidget()
类AddRem(小部件):
定义初始(自我,**kwargs):
超级(AddRem,self)。\uuuuuu初始化(**kwargs)
self.orientation=“垂直”
self.count=0
self.customLayout=Float\u Layout()
self.add\u小部件(self.customLayout)
MScreen类(屏幕,添加REM):
def add_customWidget(self,*args):
self.y1=(self.top-((self.count+1)*40)-110)
self.count+=1
self.custWidgets=CostumWidget(大小提示=(.1.05),位置=(100480-(self.count*30)))
self.removeBtn=CustomRemBtn(大小提示=(.1.05),位置=(400480-(self.count*30)))
如果self.count==1:
self.customLayout.add_小部件(self.custWidgets)
self.customLayout.add_小部件(self.removeBtn)
其他:
self.customLayout.remove_小部件(self.customLayout.children[0])
self.customLayout.add_小部件(self.custWidgets)
self.customLayout.add_小部件(self.removeBtn)
对于self.customLayout.children中的i2:
对于范围内的x(len(i2.子项)):
i2.children[x].text=self.ids.textIn.text
打印(i2.children[x].text)
类别myApp1(应用程序):
def生成(自):
退货管理
SManage=ScreenManager()
SManage.add_小部件(MScreen())
如果名称=“\uuuuu main\uuuuuuuu”:
myApp1().run()
但我的问题是,它不适用于我的代码,而且我被卡住了,每次我更改TextInput的值,所有小部件的子部件都将具有相同的值


谢谢您,我希望您能提供解决方案或任何建议。

例如,我将创建一个名为Row的类。然后,当按下该行中的add按钮时,它将调用根类中的一个方法,使用所需的参数将另一行添加到自身中。
像这样:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
from kivy.lang import Builder


Builder.load_string('''

<Row>:
    orientation: 'horizontal'
    Label:
        text: root.inp
    Button:
        text: 'Add'
        on_release:
            app.root.add(input.text)
    TextInput:
        id: input

''')


class Row(BoxLayout):
    inp = StringProperty("")

    def __init__(self,inp,**kwargs):
        super(Row,self).__init__(**kwargs)
        self.inp = inp


class MyLayout(BoxLayout):

    def __init__(self,**kwargs):
        super(MyLayout,self).__init__(**kwargs)
        self.orientation = "vertical"
        self.add_widget(Row("First added"))

    def add(self,inp):
        self.add_widget(Row(inp))


class MyApp(App):

    def build(self):
        return MyLayout()


MyApp().run()
从kivy.app导入应用
从kivy.uix.boxlayout导入boxlayout
从kivy.properties导入StringProperty
从kivy.lang导入生成器
Builder.load_字符串(“”)
:
方向:“水平”
标签:
文本:root.inp
按钮:
文本:“添加”
发布时:
app.root.add(input.text)
文本输入:
id:输入
''')
类行(BoxLayout):
inp=StringProperty(“”)
定义初始值(自我,输入,**kwargs):
超级(行,自).\uuuu初始值(**kwargs)
self.inp=inp
类MyLayout(框布局):
定义初始(自我,**kwargs):
超级(MyLayout,self)。\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
self.orientation=“垂直”
self.add_小部件(行(“首次添加”))
def添加(自我,inp):
self.add_小部件(行(inp))
类别MyApp(应用程序):
def生成(自):
返回MyLayout()
MyApp().run()