saving textinput to sqlite3 database in python, kivy

444 views
Skip to first unread message

lstaticfirel

unread,
Apr 3, 2021, 12:01:23 PM4/3/21
to Kivy users support
Hello.

I have a problem getting sqlite3 to save the data i write into a textinput within a popup.

How do I reference the textinput id from root of a screen in kvfile to make a variable when popup also needs to be root?  


#### main.py ########################

import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.popup import Popup
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ObjectProperty
import sqlite3 as sql
kivy.require('1.11.1')


class WindowManager(ScreenManager):
    pass


class MainWindow(Screen):
    pass


class TestWindow(Screen, FloatLayout):
    
    test_input = ObjectProperty(None)

    def test_save(self):
        connection = sql.connect('test.db')
        cursor = connection.cursor()
        cursor.execute(""" INSERT INTO cleaning VALUE (?)""", self.test_input.text)
        connection.commit()
        connection.close()


class TestPopup(Popup):
    pass


GUI = Builder.load_file('main_window.kv')


class MyApp(App):

    test_win = TestWindow()
    connection = sql.connect('test.db')
    cursor = connection.cursor()
    cursor.execute(""" CREATE TABLE IF NOT EXISTS cleaning(
    test TEXT)
    """)
    connection.commit()
    connection.close()

    def build(self):
        return GUI

    def change_screen(self, screen_name):
        sm = self.root.ids['sm']
        sm.current = screen_name


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

#### main_window.kv ########################

#:include test_window.kv

#: import Factory kivy.factory.Factory
#: import FadeTransition kivy.uix.screenmanager.FadeTransition

BoxLayout:
    WindowManager:
        id: sm
        transition: FadeTransition()

        MainWindow:
            name: 'main'
        TestWindow:
            name: 'test'

<MainWindow>
    name: 'main'
    id: main

    FloatLayout:
        size: root.width, root.height

        RoundedButton:
            id: test_btn

            text: "TEST"
            color: (0,0,0,1)
            size_hint: 0.2, 0.05
            text_size: root.width, None
            font_size: root.height/45
            halign: "center"
            pos_hint:{"x":0.4, "y":0.66}
            on_press:
                app.change_screen('test')

        RoundedButton:
            id: quit_btn

            text: "QUIT"
            color: (0,0,0,1)
            size_hint: 0.2, 0.05
            text_size: root.width, None
            font_size: root.height/45
            halign: "center"
            pos_hint:{"x":0.4, "y":0.32}
            on_press:
                app.stop()

<RoundedButton@Button>
    background_color: (1,7,1,0)
    background_normal: ''
    canvas.before:
        Color:
            rgba: (0,1,1,1)
        RoundedRectangle:
            size: self.size
            pos: self.pos
            radius: [22]

#### test_window.kv ########################

#: set black 0, 0, 0, 1
#: set red 1, 0, 0, 1
#: set gray .4, .4, .4, 1
#: set blue 0, 0, 1, 1

FloatLayout:
                    ######this######
    ## test_input = test_popup_input <== How do I access this id from here? ##

    WindowManager:
        id: sm

    TestWindow:
        name: 'test'
        id: test

<TestWindow>

    name: 'test'
    id: test

    TabbedPanel:
        do_default_tab:False
        tab_pos: 'top_mid'
        background_color: (0, 0, 0, 1)

        ColorTabbedPanelItem:
            normal_color: black
            down_color: blue
            text: "TEST"
            font_size: 14
            color: 1,1,1,1

            FloatLayout:
                
                Label:
                    text: "TEXT : "
                    pos_hint:{"center_x": 0.2, "center_y": 0.5}
                 
                Label:
                    id: test_popup_task
                    text: "" ##### <== root.test_save() ######
                    pos_hint:{"center_x": 0.6, "center_y": 0.5}

                RoundedButton:
                    text: "INPUT"
                    color: (0,0,0,1)
                    size_hint: 0.06, 0.08
                    text_size: root.width, None
                    font_size: root.height/45
                    halign: "center"
                    pos_hint: {"x":0.75, "y":0.46}
                    on_press:
                        Factory.TestPopup().open()

    RoundedButton:
        text: "BACK"
        color: (0,0,0,1)
        size_hint: 0.2, 0.05
        text_size: root.width, None
        font_size: root.height/45
        halign: "center"
        pos_hint:{"x":0.4, "y":0.05}
        on_release:
            app.change_screen('main')

<ColorTabbedPanelItem@TabbedPanelItem>

    normal_color: gray
    down_color: red
    background_color: {'normal': root.normal_color, 'down': root.down_color} [self.state]
    background_down: ''
    background_normal: ''

<TestPopup@Popup>

    auto_dismiss:True
    size_hint: 0.6, 0.21
    title: "WRITE SOMETHING"

    BoxLayout:
        orientation: "vertical"
        size: root.width, root.height

        TextInput:
            id: test_popup_input
            color: (0,1,0,1)
            size_hint: 1, 0.04
            multiline:True

        RoundedButton:
            id: test_popup_save
            text: "SAVE"
            color: (0,0,0,1)
            font_size: 14
            size_hint: 0.37, 0.04
            pos_hint:{"x":0.3, "y":0.05}
            on_press:
                app.root.ids.sm.get_screen('test').ids.test_popup_task.text=test_popup_input.text
            on_release:
                app.test_win.test_save()
                root.dismiss()

<RoundedButton@Button>
    background_color: (1,7,1,0)
    background_normal: ''
    canvas.before:
        Color:
            rgba: (0,1,1,1)
        RoundedRectangle:
            size: self.size
            pos: self.pos
            radius: [22]


Elliot Garbus

unread,
Apr 3, 2021, 1:20:14 PM4/3/21
to kivy-...@googlegroups.com

It looks like it would be easier to simply pass the text value into test_save()

 

In test_window.kv change highlighted code:

 

<TestPopup@Popup>

    auto_dismiss:True
   
size_hint: 0.6, 0.21
   
title: "WRITE SOMETHING"

   
BoxLayout:
       
orientation: "vertical"
       
size: root.width, root.height

       
TextInput:
           
id: test_popup_input
           
color: (0,1,0,1)
           
size_hint: 1, 0.04
           
multiline:True

        RoundedButton:
           
id: test_popup_save
           
text: "SAVE"
           
color: (0,0,0,1)
           
font_size: 14
           
size_hint: 0.37, 0.04
            
pos_hint:{"x":0.3, "y":0.05}
#            on_press:
#                app.root.ids.sm.get_screen('test').ids.test_popup_task.text=test_popup_input.text

           
on_release:
               
app.root.ids.sm.get_screen('test').test_save(test_popup_input.text)  # passing text to test_save()
               
root.dismiss()

 

In main.py change:

 

class TestWindow(Screen):  # no need for multiple inheritance here, removed FloatLayout
    # test_input = ObjectProperty(None)

   
def test_save(self, text):
       
# connection = sql.connect('test.db')  # I commented out db code for my testing
        # cursor = connection.cursor()
        # cursor.execute(""" INSERT INTO cleaning VALUE (?)""", self.test_input.text)  # use passed in value text to write to db
        # connection.commit()
        # connection.close()
       
print(f'test_save() {text}')

--
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/fb137da2-b214-4c4f-bcce-7b90f6f59fa6n%40googlegroups.com.

 

lstaticfirel

unread,
Apr 3, 2021, 8:09:02 PM4/3/21
to Kivy users support
Thanks you very much, that did it! :) also, seems like I have been forgetting to put a comma after the value in cursor.execute that has been preventing me from storing to database. It should be ## cursor.execute(""" INSERT INTO cleaning VALUE (?)""", f''{text}' ,)) ## , anyway,
thanks for the  help.

ElliotG

unread,
Apr 4, 2021, 9:04:35 AM4/4/21
to Kivy users support
The variable text is a string.  I don't think you need to use the f string in the cursor call: 
## cursor.execute(""" INSERT INTO cleaning VALUE (?)""", text,)) ##

Dewan Syed Mohsin Bukhari

unread,
Apr 6, 2021, 5:14:39 AM4/6/21
to Kivy users support
I would suggest you to use Peewee: http://docs.peewee-orm.com/en/latest/

lstaticfirel

unread,
Apr 7, 2021, 3:42:27 PM4/7/21
to Kivy users support
You're right ElliotG, no need for f. 



syedmo: Thanks for the tip, I'll check out peewee.

Reply all
Reply to author
Forward
0 new messages