嗨,大家好我的kivy新手,我正在制作一个小代码,它将获取输入值并将其设置为小部件子值。
目标是,当我单击添加按钮时,将添加第1行小部件并将获取TextInput的值。如果我更改TextInput值并单击“添加”按钮,将添加第二行小部件,它将具有新的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()
但我的问题是它无法使用我的代码而且卡住了,每当我更改TextInput的值时,所有小部件子项都将具有相同的值。
谢谢你,我希望得到解决方案或任何建议。
答案 0 :(得分:0)
我会创建一个名为Row的类。然后,当按下该行中的添加按钮时,它将调用根类中的方法,以便将其他Row添加到自身,并带有所需的参数。
像这样:
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()