如何使用 kivy 和 RecycleView 编辑 SQLite 数据库值

2022-01-15 00:00:00 python kivy sqlite

我正在尝试使用 kivy 在 python 中显示 sqlite3 数据库.测试了 ikolim 的解决方案

当按下按钮时,会按预期显示弹出和编辑功能:

它确实会编辑所选按钮的文本,如下面的屏幕截图所示..:

..但不是 sqlite 数据库值.是否可以将用户在 Kivy 应用程序中所做的更改反映到 sqlite 数据库中的值?

代码如下:

test.py

导入 sqlite3从 kivy.app 导入应用程序从 kivy.uix.boxlayout 导入 BoxLayout从 kivy.uix.recycleview.views 导入 RecycleDataViewBehavior从 kivy.uix.button 导入按钮从 kivy.properties 导入 BooleanProperty、ListProperty、StringProperty、ObjectProperty从 kivy.uix.recyclegridlayout 导入 RecycleGridLayout从 kivy.uix.behaviors 导入 FocusBehavior从 kivy.uix.recycleview.layout 导入 LayoutSelectionBehavior从 kivy.uix.popup 导入弹出窗口连接 = sqlite3.connect("demo.db",isolation_level=None)游标 = 连接.游标()类TextInputPopup(弹出):obj = ObjectProperty(无)obj_text = StringProperty("")def __init__(self, obj, **kwargs):super(TextInputPopup, self).__init__(**kwargs)自我.obj = objself.obj_text = obj.text类 SelectableRecycleGridLayout(FocusBehavior,LayoutSelectionBehavior,回收网格布局):''' 将选择和焦点行为添加到视图.'''类 SelectableButton(RecycleDataViewBehavior,按钮):''' 为按钮添加选择支持 '''索引 = 无选择 = BooleanProperty(False)可选 = BooleanProperty(True)def refresh_view_attrs(self, rv, index, data):''' 捕捉并处理视图变化 '''self.index = 索引return super(SelectableButton, self).refresh_view_attrs(rv, index, data)def on_touch_down(自我,触摸):'''在触地时添加选择'''if super(SelectableButton, self).on_touch_down(touch):返回真如果 self.collide_point(*touch.pos) 和 self.selectable:返回 self.parent.select_with_touch(self.index, touch)def apply_selection(self, rv, index, is_selected):''' 响应视图中项目的选择.'''self.selected = is_selecteddef on_press(self):popup = TextInputPopup(self)popup.open()def update_changes(self, txt):self.text = txtRV类(BoxLayout):data_items = ListProperty([])def __init__(self, **kwargs):超级(房车,自我).__init__(**kwargs)self.get_users()def get_users(self):cursor.execute("CREATE TABLE IF NOT EXISTS Callbacks(cName TEXT, cID INT, cbTime INT, cbRems TEXT)")cursor.execute("插入回调值 ('Client1','1','1500','Test1')")cursor.execute("插入回调值 ('Client2','2','1600','Test2')")cursor.execute("插入回调值 ('Client3','3','1700','Test3')")连接.commit()cursor.execute("SELECT * FROM Callbacks ORDER BY ROWID DESC")行 = cursor.fetchall()# 创建数据项对于行中的行:对于列中的列:self.data_items.append(col)对于行中的行:打印(self.data_items)类测试应用程序(应用程序):title = "Kivy RecycleView & SQLite3 演示"定义构建(自我):返回 RV()如果 __name__ == "__main__":TestApp().run()

test.kv

#:kivy 1.10.0<TextInputPopup>:标题:弹出"size_hint:无,无尺寸:400、400auto_dismiss: 假盒子布局:方向:垂直"文本输入:id: txt输入文本:root.obj_text按钮:大小提示:1, 0.2文本:保存更改"on_release:root.obj.update_changes(txtinput.text)root.dismiss()按钮:大小提示:1, 0.2文本:取消更改"on_release: root.dismiss()<可选按钮>:# 画一个背景来表示选择画布之前:颜色:rgba: (.0, 0.9, .1, .3) if self.selected else (0, 0, 0, 1)长方形:pos: self.pos尺寸:self.size<房车>:盒子布局:方向:垂直"网格布局:大小提示:1,无size_hint_y:无身高:25列数:4标签:文字:姓名"标签:文字:身份证"标签:文字:CB时间"标签:文字:备注"盒子布局:回收视图:视图类:可选按钮"数据:[{'text': str(x)} for x in root.data_items]可选回收网格布局:列数:4default_size:无,dp(26)default_size_hint:1,无size_hint_y:无高度:self.minimum_height方向:垂直"多选:真touch_multiselect:真

解决方案

为了反映 sql 的更改,您需要使用 update 命令更新 sql 表.

I'm trying to display an sqlite3 database in python using kivy. Tested out ikolim's solution here and it works as intended in displaying the database contents into the RecycleView's button labels:

And when a button is pressed, a popup and edit function appears as intended:

And it does edit the selected button's text as shown in the next screenshot.. :

..but not the sqlite database values. Would it be possible to reflect the changes the user makes inside the Kivy app to the values inside the sqlite database?

Here are the codes:

test.py

import sqlite3

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.button import Button
from kivy.properties import BooleanProperty, ListProperty, StringProperty, ObjectProperty
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

connection = sqlite3.connect("demo.db", isolation_level=None)
cursor = connection.cursor()

class TextInputPopup(Popup):
    obj = ObjectProperty(None)
    obj_text = StringProperty("")

    def __init__(self, obj, **kwargs):
        super(TextInputPopup, self).__init__(**kwargs)
        self.obj = obj
        self.obj_text = obj.text


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)

    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

    def on_press(self):
        popup = TextInputPopup(self)
        popup.open()

    def update_changes(self, txt):
        self.text = txt


class RV(BoxLayout):
    data_items = ListProperty([])

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

    def get_users(self):

        cursor.execute("CREATE TABLE IF NOT EXISTS Callbacks(cName TEXT, cID INT, cbTime INT, cbRems TEXT)")
        cursor.execute("INSERT INTO Callbacks VALUES ('Client1','1','1500','Test1')")
        cursor.execute("INSERT INTO Callbacks VALUES ('Client2','2','1600','Test2')")
        cursor.execute("INSERT INTO Callbacks VALUES ('Client3','3','1700','Test3')")
        connection.commit()
        cursor.execute("SELECT * FROM Callbacks ORDER BY ROWID DESC")

        rows = cursor.fetchall()

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

        for row in rows:
            print(self.data_items)



class TestApp(App):
    title = "Kivy RecycleView & SQLite3 Demo"

    def build(self):
        return RV()


if __name__ == "__main__":
    TestApp().run()

test.kv

#:kivy 1.10.0

<TextInputPopup>:
    title: "Popup"
    size_hint: None, None
    size: 400, 400
    auto_dismiss: False

    BoxLayout:
        orientation: "vertical"
        TextInput:
            id: txtinput
            text: root.obj_text
        Button:
            size_hint: 1, 0.2
            text: "Save Changes"
            on_release:
                root.obj.update_changes(txtinput.text)
                root.dismiss()
        Button:
            size_hint: 1, 0.2
            text: "Cancel Changes"
            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"

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

            Label:
                text: "Name"
            Label:
                text: "ID"
            Label:
                text: "CB Time"
            Label:
                text: "Remarks"

        BoxLayout:
            RecycleView:
                viewclass: 'SelectableButton'
                data: [{'text': str(x)} for x in root.data_items]
                SelectableRecycleGridLayout:
                    cols: 4
                    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

解决方案

In order to reflect the changes to the sql, you'll need to use the update command to update the sql table.

相关文章