【问题标题】:How to use same form code for add,update如何使用相同的表单代码进行添加、更新
【发布时间】:2017-11-17 14:31:16
【问题描述】:

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. 当点击任何一行然后编辑表单打开。我可以使用相同的表单添加用户吗。

此时我点击添加用户,然后运行 ​​user.py 文件并打开一个新表单。如何使用相同的表单进行添加更新。

【问题讨论】:

    标签: python python-2.7 kivy kivy-language


    【解决方案1】:

    是的,您可以使用相同的表格来添加城市。在示例中,我添加了一个按钮、一个方法 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
    

    输出

    【讨论】:

    • 感谢回复。你能告诉我如何使用吗?我很困惑,当我添加时如何隐藏编辑值。我对此很困惑。你能告诉我如何使用相同add,update.thanks 的代码
    • 谢谢。它正在工作 请你再告诉我一件事如何在我添加状态时隐藏 userId 标签。因为当我添加状态时它是我的主要 id 然后它将自动递增所以不需要 userId 标签.
    • 添加记录时 self.label_rec_id = " " 所以显示空白。我可以删除空白吗?
    • 我已经更新了我的代码以使用 opacity 属性来隐藏小部件。
    猜你喜欢
    • 2023-03-22
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    • 2019-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-03
    相关资源
    最近更新 更多