Python / Kivy:如何在kivy中删除行时删除空格

时间:2018-01-05 07:07:59

标签: python python-3.x python-2.7 kivy kivy-language

我正在从+ Add More按钮添加动态行。我想从删除按钮中删除当前行。我正在使用删除当前行

def remove(self):
        self.clear_widgets()


    self.clear_widgets()删除当前行。但如何删除空格也删除行?如果我删除第二行然后显示空格。

demo.py

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.clearcolor = (0.5, 0.5, 0.5, 1)
Window.size = (500, 400)

class User(Screen):

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


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

    def remove(self):
        self.clear_widgets()


class Rows(BoxLayout):
    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_file('demo.kv')
        return self.root


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

demo.kv

<Row>:
    size_hint_y: None
    height: self.minimum_height
    height: 40

    Button:
        text: root.button_text
        size_hint_x: None
        top: 200

    Button:
        text: 'World 1'
        width: 300

    Button:
        text: "Remove"
        size_hint_x: .4
        on_press : root.remove()

<Rows>:
    size_hint_y: None
    height: self.minimum_height
    orientation: "vertical"

User:
    BoxLayout:
        orientation: "vertical"
        padding : 20, 5


        BoxLayout:
            orientation: "horizontal"
            #padding : 10, 10
            spacing: 10, 10
            size: 450, 40
            size_hint: None, None

            Label:
                size_hint_x: .2
                text: "Test 1"
                text_size: self.size
                valign: 'bottom'
                halign: 'center'

            Label:
                size_hint_x: .8
                text: "Test 2"
                text_size: self.size
                valign: 'bottom'
                halign: 'center'


        ScrollView:
            Rows:
                id: rows


        BoxLayout:
            orientation: "horizontal"
            size_hint_x: .2
            size_hint_y: .2

            Button:
                text: "+Add More"
                on_press: root.add_more()


        BoxLayout:
            orientation: "horizontal"
            padding : 10, 5
            spacing: 10, 10
            size_hint: .5, .35
            pos_hint: {'x': .25, 'y':.25}

            Button:
                text: 'Ok'

            Button:
                text: 'Cancel'

0 个答案:

没有答案