Kivy-在其他行之间插入一行小部件

时间:2018-12-08 10:09:04

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

我有一个类似于excel电子表格的应用程序,其中包含一行小部件,最后一行可以使用“-”按钮删除,也可以使用“ +”添加到顶部。

我想通过能够从列表中的任意位置插入/删除行来增强其功能,就像使用Excel电子表格一样。

我该如何处理-我必须开始给每行一个ID并可能从Scrollview更改为Gridlayout吗?还是可以通过每行单独的+/-按钮进行动态添加,从而知道如何与相邻行进行交互。

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.uix.button import Button
from kivy.clock import Clock

from sql_update_data import update_db

kivy.require('1.10.1')


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

    def __init__(self, **kwargs):
        super(GUILayout, self).__init__(**kwargs)
        Clock.schedule_once(self.add_grid)

    def add_grid(self, *args):

        for i in range(20):
            self.row_count += 1
            self.grid.add_widget(Button(text=str(self.row_count), size_hint_y=None,
                                    height=80))

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

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

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

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

    def inject_row(self):
        self.rows.insert_row()


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


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

    def __init__(self, **kwargs):
        super(Rows, self).__init__(**kwargs)
        for clock in range(6):
            Clock.schedule_once(self.add_row)

        for clock in range(1):
            Clock.schedule_once(self.add_row_contact)

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

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

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

    def insert_row(self):
        self.row_count += 1
        print("working")
        #self.content.add_widget(self.content.children[0])


class TestApp(App):

    def build(self):
        return GUILayout()


GUIApp = TestApp()
GUIApp.run()

TEST.KV

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

    BoxLayout:
        orientation: 'vertical'
        size_hint_x: .05

        Button:
            text: "-"
            font_size: 40

        Button:
            text: "+"

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

<Rows>:
    content: content
    do_scroll_y: False

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


GUILayout:


<GUILayout>:
    rows: rows
    orientation: "vertical"
    grid: Grid
    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'

                        BoxLayout:
                            orientation: 'horizontal'

                            ScrollView:
                                size_hint_x: .05

                                GridLayout:
                                    id: Grid
                                    cols: 1
                                    rows: 20


                            BoxLayout:
                                orientation: 'vertical'

                                Rows:
                                    id: rows

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


                            Button:
                                text: " - "
                                font_size: 70
                                size_hint_x: .1
                                on_press: root.subtract_row()

                            Button:
                                text: " + "
                                font_size: 50
                                size_hint_x: .1
                                on_press: root.plus_row()

                            Button:
                                text: "Update Maestro"
                                size_hint_x: .4
                                on_press: root.update_sql()


                            Button:
                                text: "Settings"
                                font_size: 30
                                size_hint_x: .2
                                on_press: root.inject_row()

0 个答案:

没有答案