如何使用相同的表单代码进行添加,更新

时间:2017-11-17 14:31:17

标签: python python-2.7 kivy kivy-language

test.kv

:kivy 1.10.0

<CRUD>:
    title: self.mode + " State"
    size_hint: None, None
    size: 350, 350
    auto_dismiss: False

    BoxLayout:
        orientation: "vertical"
        GridLayout:
            cols: 2
            Label:
                text: root.label_rec_id
            Label:
                id: userid
                text: root.col_data[0]  # root.userid
            Label:
                text: "First Name"
            TextInput:
                id: fname
                text: root.col_data[1]  # root.fname
            Label:
                text: "Last Name"
            TextInput:
                id: lname
                text: root.col_data[2]  # root.lname
        Button:
            size_hint: 1, 0.4
            text: "Save Changes"
            on_release:
                root.package_changes(fname.text, lname.text)
                app.root.update_changes(root)
                root.dismiss()
        Button:
            size_hint: 1, 0.4
            text: "Cancel Changes"
            on_release: root.dismiss()
  1. 点击任意一行然后编辑表格打开。我可以使用相同的表格添加用户。

  2. 此时我点击添加用户然后运行user.py文件并打开一个新的form.how以使用相同的表单添加更新。

1 个答案:

答案 0 :(得分:2)

是的,您可以使用相同的表单添加城市。在这个例子中,我添加了一个按钮,一个方法add_record,一些变量,例如StringProperty类型的模式,以及INSERT SQL命令。有关详细信息,请参阅示例。

实施例

main.py

import kivy

kivy.require('1.10.0')  # replace with your current kivy version !
import sqlite3 as lite
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import BooleanProperty, ListProperty, StringProperty, ObjectProperty, NumericProperty
from kivy.lang import Builder

from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.button import Button
from kivy.uix.recyclegridlayout import RecycleGridLayout
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.uix.popup import Popup
from kivy.core.window import Window


Window.size = (500, 500)

MAX_TABLE_COLS = 3

con = lite.connect('company.db')
# con.text_factory = str
cur = con.cursor()


class CRUD(Popup):
    """CRUD - Create, Read, Update, Delete"""
    label_id_text = ObjectProperty(None)
    label_id_data = ObjectProperty(None)

    mode = StringProperty("")
    label_rec_id = StringProperty("UserID")
    start_point = NumericProperty(0)
    col_data = ListProperty(["", "", ""])

    def __init__(self, obj, **kwargs):
        super(CRUD, self).__init__(**kwargs)
        self.mode = obj.mode
        if obj.mode == "Add":
            self.label_id_text.opacity = 0  # invisible
            self.label_id_data.opacity = 0  # invisible
        else:
            self.label_id_text.opacity = 1  # visible
            self.label_id_data.opacity = 1  # visible
            self.start_point = obj.start_point
            self.col_data[0] = obj.rv_data[obj.start_point]["text"]
            self.col_data[1] = obj.rv_data[obj.start_point + 1]["text"]
            self.col_data[2] = obj.rv_data[obj.start_point + 2]["text"]

    def package_changes(self, fname, lname):
        self.col_data[1] = fname
        self.col_data[2] = lname


class SelectableRecycleGridLayout(FocusBehavior, LayoutSelectionBehavior,
                                  RecycleGridLayout):
    ''' Adds selection and focus behaviour to the view. '''


class SelectableButton(RecycleDataViewBehavior, Button):
    ''' Add selection support to the Button '''
    index = None
    selected = BooleanProperty(False)
    selectable = BooleanProperty(True)
    rv_data = ObjectProperty(None)
    start_point = NumericProperty(0)
    mode = StringProperty("")

    def refresh_view_attrs(self, rv, index, data):
        ''' Catch and handle the view changes '''
        self.index = index
        return super(SelectableButton, self).refresh_view_attrs(rv, index, data)

    def on_touch_down(self, touch):
        ''' Add selection on touch down '''
        if super(SelectableButton, self).on_touch_down(touch):
            return True
        if self.collide_point(*touch.pos) and self.selectable:
            return self.parent.select_with_touch(self.index, touch)

    def apply_selection(self, rv, index, is_selected):
        ''' Respond to the selection of items in the view. '''
        self.selected = is_selected
        self.rv_data = rv.data

    def on_press(self):
        self.mode = "Update"
        self.start_point = 0
        end_point = MAX_TABLE_COLS
        rows = len(self.rv_data) // MAX_TABLE_COLS
        for row in range(rows):
            if self.index in list(range(end_point)):
                break
            self.start_point += MAX_TABLE_COLS
            end_point += MAX_TABLE_COLS

        popup = CRUD(self)
        popup.open()


class RV(BoxLayout):
    rv_data = ListProperty([])
    start_point = NumericProperty(0)
    mode = StringProperty("")

    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        self.get_users()

    def get_users(self):
        '''This result retrieve from database'''
        self.rv_data = []

        cur.execute("SELECT * FROM Users ORDER BY UserID ASC")
        rows = cur.fetchall()

        # create data_items
        for row in rows:
            for col in row:
                self.rv_data.append(col)

    def add_record(self):
        self.mode = "Add"
        popup = CRUD(self)
        popup.open()

    def update_changes(self, obj):
        if obj.mode == "Add":
            # insert record into Database Table
            cur.execute("INSERT INTO Users VALUES(NULL, ?, ?)",
                        (obj.col_data[1], obj.col_data[2]))
        else:
            # update Database Table
            cur.execute("UPDATE Users SET FirstName=?, LastName=? WHERE UserID=?",
                        (obj.col_data[1], obj.col_data[2], obj.col_data[0]))
        con.commit()
        self.get_users()


class ListUser(App):
    title = "Users"

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


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

main.kv

#:kivy 1.10.0

<CRUD>:
    label_id_text: label_id_text
    label_id_data: label_id_data

    title: self.mode + " State"
    size_hint: None, None
    size: 350, 350
    auto_dismiss: False

    BoxLayout:
        orientation: "vertical"
        GridLayout:
            cols: 2
            Label:
                id: label_id_text
                text: "ID"
            Label:
                id: label_id_data
                text: root.col_data[0]  # root.userid
            Label:
                text: "First Name"
            TextInput:
                id: fname
                text: root.col_data[1]  # root.fname
            Label:
                text: "Last Name"
            TextInput:
                id: lname
                text: root.col_data[2]  # root.lname
        Button:
            size_hint: 1, 0.4
            text: "Confirm " + root.mode
            on_release:
                root.package_changes(fname.text, lname.text)
                app.root.update_changes(root)
                root.dismiss()
        Button:
            size_hint: 1, 0.4
            text: "Cancel " + root.mode
            on_release: root.dismiss()

<SelectableButton>:
    # Draw a background to indicate selection
    canvas.before:
        Color:
            rgba: (.0, 0.9, .1, .3) if self.selected else (0, 0, 0, 1)
        Rectangle:
            pos: self.pos
            size: self.size

<RV>:
    BoxLayout:
        orientation: "vertical"

        Button:
            size_hint: 1, 0.1
            text: "Add Record"
            on_press: root.add_record()

        GridLayout:
            size_hint: 1, None
            size_hint_y: None
            height: 25
            cols: 3

            Label:
                text: "ID"
            Label:
                text: "First Name"
            Label:
                text: "Last Name"

        BoxLayout:
            RecycleView:
                viewclass: 'SelectableButton'
                data: [{'text': str(x)} for x in root.rv_data]
                SelectableRecycleGridLayout:
                    cols: 3
                    default_size: None, dp(26)
                    default_size_hint: 1, None
                    size_hint_y: None
                    height: self.minimum_height
                    orientation: 'vertical'
                    multiselect: True
                    touch_multiselect: True

输出

Figure 1 - App Startup Figure 2 - SQLite3 Database Figure 3 - Add Record Joe Plummer Figure 4 - RecycleView Data Updated with Joe Plummer Figure 5 - SQLite3 Database Updated with Joe Plummer Figure 6 - Update John Doe to Jane Doe Figure 7 - RecycleView Data Updated with Jane Doe Figure 8 - SQLite3 Database Updated with Jane Doe