How to edit SQLite database values using kivy and RecycleView

885 views
Skip to first unread message

Requiem

unread,
Mar 25, 2020, 5:35:53 AM3/25/20
to Kivy users support
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:

1.PNG





















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

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

3.PNG

..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:

TestApp.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)



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

    def build(self):
        return RV()


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


TestApp.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




Elliot Garbus

unread,
Mar 25, 2020, 9:44:44 AM3/25/20
to kivy-...@googlegroups.com

What appears on the buttons is defined by the attribute data in the RecycleView.

 

RecycleView:
    viewclass: 'SelectableButton'
    data: [{'text': str(x)} for x in root.data_items]

 

The attribute ‘data’, is a list of dictionaries.  The key in the dict, in this case ‘text’, is applied to the widgets in the RecycleView. Providing the text value.  You need to read from your database and create a list of dicts to assign those values to the buttons.

 

Here is a different example of a RecycleView.  Note the routine to convert a list to a list of dicts.  Also note that the viewclass, TestButton is simply a button.

 
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import ListProperty
from kivy.uix.boxlayout import BoxLayout

kv =
"""
<TestButton@Button>
    on_release: print(f'Button: {self.text} pressed')
TestBox:
    orientation: 'vertical'
    RecycleView:
        viewclass: 'TestButton'
        data: root.rv_data_list
        scroll_type: ['bars','content']
        bar_width: dp(20)
        do_scroll_x: False
        RecycleBoxLayout:
            default_size: None, dp(56)

            default_size_hint: 1, None
            size_hint_y: None
            height: self.minimum_height
            orientation: 'vertical'
    BoxLayout:
        size_hint_y: None
        height: dp(48)
        Button:
            text: 'Add pets'
            on_release: root.add_pets()
        Button:
            text: 'Remove pets'
            on_release: root.remove_pets()
"""


class TestBox(BoxLayout):
    rv_data_list = ListProperty([{
'text': 'A' + str(n)} for n in range(4)] +
                                [{
'text': 'B' + str(n)} for n in range(4)])

    pets = [
'dog', 'cat', 'hamster', 'guinea pig', 'rabbit', 'parrot', 'snake']

   
def list_to_rv_data(self, str_data):
       
#  convert a list of strings to a list of dictionaries for the rv list
       
return [{'text': s} for s in str_data]

   
def rv_data_to_list(self, rv_data):
       
# convert a list of  rv_data to a list of strings
       
return [list(item.values())[0] for item in rv_data]

   
def add_pets(self):
       
self.rv_data_list = self.rv_data_list + self.list_to_rv_data(self.pets)

   
def remove_pets(self):
        entries =
self.rv_data_to_list(self.rv_data_list)
        no_pets = [d
for d in entries if d not in self.pets]
       
self.rv_data_list = self.list_to_rv_data(no_pets)


class RVTestApp(App):
   
def build(self):
       
return Builder.load_string(kv)


RVTestApp().run()

 

 

 

From: Requiem
Sent: Wednesday, March 25, 2020 2:36 AM
To: Kivy users support
Subject: [kivy-users] How to edit SQLite database values using kivy and RecycleView

 

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:

 

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/d903a75f-473b-4a66-a320-67cb419b98b9%40googlegroups.com.

 

Requiem

unread,
Mar 25, 2020, 10:10:16 AM3/25/20
to Kivy users support
Sorry, I'm still a novice and I'm having a difficult time understanding and relating your solution to my code. I understand that solution for my problem would be to add these:
#.kv file
on_release
:
    app
.update_db()
#.py file
app
.update_db():
    cursor
.execute(UPDATE table SET...WHERE...)

to my code, but herein lies my predicament. Using the update command would require two things: 1) The value of the entry which I would like to change; 2) a value/s for WHERE. Number 1 is quite easy, but I don't know what to put for WHERE. Could you help me with this please? Thanks

To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.

Elliot Garbus

unread,
Mar 25, 2020, 12:08:23 PM3/25/20
to kivy-...@googlegroups.com

I believe you are asking for an index related to each data element.  Is that correct?

The easiest way to do this would be to create a new attribute in your viewclass, that holds the index. 

 

In the code below I changed TestButton to add an index property.  When the button is pressed the index is printed.

 

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.properties import NumericProperty

kv =
"""
<TestButton>
    on_release: print(f'Button: {self.text} pressed, with index {self.index}')


TestBox:
    orientation: 'vertical'
    RecycleView:
        viewclass: 'TestButton'
        data: root.rv_data_list
        scroll_type: ['bars','content']
        bar_width: dp(20)
        do_scroll_x: False
        RecycleBoxLayout:
            default_size: None, dp(56)
            default_size_hint: 1, None
            size_hint_y: None
            height: self.minimum_height
            orientation: 'vertical'
  
"""

class TestButton(Button):
    index = NumericProperty()

class TestBox(BoxLayout):
    rv_data_list = [{
'text': 'A' + str(n), 'index': n + 200} for n in range(20)]


To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/f6671b31-1c59-46b6-a6b2-7da033d6328c%40googlegroups.com.

 

Reply all
Reply to author
Forward
0 new messages