2017-04-18 1 views
0

안녕하세요 여러분, kivy로 새로운 기능을 추가했습니다. im은 입력 값을 받아서 위젯 자식 값으로 설정하는 작은 코드를 만듭니다.특정 위젯에 값을 설정하는 방법 children

목표는 추가 버튼을 클릭하면 첫 번째 행 위젯이 추가되고 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

예를 들어 행이라는 하나의 클래스를 만듭니다. 그런 다음 해당 행의 추가 단추를 누르면 루트 클래스의 메서드를 호출하여 원하는 다른 매개 변수와 함께 다른 행을 추가합니다.
마찬가지로 :

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() 
관련 문제