2017-12-19 1 views
0

저는 python/Kivy를 처음 사용합니다.
나는 test.py와 test.ky의 두 파일을 가지고있다.
이제 일련 번호가 1과 2 인 두 개의 정적 행을 사용하고 있습니다.
누구든지 말해 줄 수 있습니까? '+ 더 추가'버튼을 클릭하면 동적 행을 추가하는 방법. 2 행은 일련 번호가 증가하는 정적 행을 보여줍니다.
일련 번호를 1에서 10까지 동적으로 추가하고 싶습니다.
Python : 동적 행을 추가하는 방법

test.py

import kivy 

from kivy.uix.screenmanager import Screen 
from kivy.app import App 
from kivy.lang import Builder 
from kivy.core.window import Window 

Window.size = (450, 525) 

class display(Screen): 

    def add_more(self): 
     print('test') 

class test(App): 

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


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

test.kv

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' 

     BoxLayout: 
      orientation: "horizontal" 
      spacing: 0, 5 

      Button: 
       text: '1' 
       size_hint_x: .2 

      TextInput: 
       size_hint_x: .8 

     BoxLayout: 
      orientation: "horizontal" 
      spacing: 0, 5 

      Button: 
       text: '2' 
       size_hint_x: .2 

      TextInput: 
       size_hint_x: .8 

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

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

누군가가 나를 도울 수 있습니까?

답변

2

Rows에 대한 맞춤 클래스를 만든 다음 행을 추가하는 방법을 사용할 수 있습니다.
예제를 조금 수정했습니다. 사용해보기 :

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 StringProperty 

Window.size = (450, 525) 


class display(Screen): 

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


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


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


class test(App): 

    def build(self): 
     self.root = Builder.load_string(KV) 
     return self.root 



KV = """ 

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

    Button: 
     text: root.button_text 
     size_hint_x: .2 

    TextInput: 
     size_hint_x: .8 


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

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

""" 


test().run() 
관련 문제