如何从行列表中删除最后一行

时间:2018-12-05 13:13:08

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

我正在尝试制作一个具有一排行的应用程序,您可以通过按一下按钮来添加/删除这些行。

我的“添加行”按钮运行良好,现在我需要“删除行”功能。

我在列表'(self.rows.content.children)'中有行,我只需要知道如何从列表中弹出最后一个,然后继续随意添加/减去。

感谢您的光临。

test.py

import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.scrollview import ScrollView
from kivy.properties import ObjectProperty, StringProperty, ListProperty
from kivy.clock import Clock

from sql_update_data import update_db

kivy.require('1.10.1')


class GUILayout(BoxLayout, GridLayout):
    rows = ObjectProperty(None)

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

    def remove_row(self):
        print("Remove last row")

    def insert_value(self):
        values = [row.values for row in reversed(self.rows.content.children)]

    for category, id, strap in values:
        update_db(category, id, strap)


class Row(BoxLayout):
    button_text = StringProperty("")
    id = ObjectProperty(None)
    values = ListProperty()


class Rows(ScrollView):
    row_count = 0
    content = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(Rows, self).__init__(**kwargs)
        Clock.schedule_once(self.add_row)

    def add_row(self, *args):
        self.row_count += 1
        self.content.add_widget(Row(button_text=str(self.row_count),
                            id=str(self.row_count)))


class TestApp(App):

    def build(self):
        return GUILayout()


GUIApp = TestApp()
GUIApp.run()

test.kv

#: import main test
<Row>:
    values: row_id.text, col1.text, col2.text
    orientation: "horizontal"
    spacing: 0, 5
    size_hint_y: None
    height: "60dp"
    spacing: 2
    pos_hint: {'center_x': .50, 'y': .80}

    Button:
        id: row_id
        text: root.button_text
        size_hint_x: .1
    Spinner:
        id: col1
        text: 'Select Category'
        values: ['One', 'Two', 'Three']
        size_hint_x: .3
    TextInput:
        id: col2
        size_hint_x: .8

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


GUILayout:


<GUILayout>:
    rows: rows
    orientation: "vertical"
    padding: 10
    spacing: 10

    BoxLayout:
        orientation: "horizontal"
        height: 60

        BoxLayout:
            orientation: "horizontal"
            size_hint_x: .25

            TabbedPanel:
                do_default_tab: False


                # ----------- TAB 1 ------------

                TabbedPanelItem:
                    text: "tab1"
                    BoxLayout:
                        orientation: 'vertical'

                        Rows:
                            id: rows

                        GridLayout:
                            rows: 1
                            cols: 6
                            padding: 1
                            spacing: 5
                            size_hint_y: None
                            height: 50

                            # --------- MINUS ---------
                            Button:
                                text: " - "
                                font_size: 70
                                size_hint_x: .1
                                on_press: root.remove_row()

                            # -------- SUBTRACT -------
                            Button:
                                text: " + "
                                font_size: 50
                                size_hint_x: .1
                                on_press: root.add_more()


                            # ----- UPDATE MAESTRO -----
                            Button:
                                text: "Update Maestro"
                                size_hint_x: .4
                                on_press: root.insert_value()


                            # -------- SETTINGS --------
                            Button:
                                text: "Settings"
                                font_size: 30
                                size_hint_x: .2

1 个答案:

答案 0 :(得分:1)

您应该做的是使用remove_widget方法从内容中删除最后一个子窗口小部件,另一方面,请勿将id用作变量的名称,因为它是保留字:

另一方面,GUILayout不能继承2个小部件,只需要来自BoxLayout。

# ..

class GUILayout(BoxLayout):
    rows = ObjectProperty(None)

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

    def remove_row(self):
        self.rows.remove_row()

    def insert_value(self):
        values = [row.values for row in reversed(self.rows.content.children)]
        for category, _id, strap in values:
            update_db(category, _id, strap)

#...

class Rows(ScrollView):
    content = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(Rows, self).__init__(**kwargs)
        self.row_count = 0
        Clock.schedule_once(lambda _: self.add_row())

    def add_row(self):
        self.row_count += 1
        self.content.add_widget(Row(button_text=str(self.row_count), 
            id=str(self.row_count)))

    def remove_row(self):
        if self.content.children:
            self.content.remove_widget(self.content.children[0])
            self.row_count -= 1
# ...