K
K
Konstantin Ermolaev2022-01-18 00:08:04
Python
Konstantin Ermolaev, 2022-01-18 00:08:04

How to sync sqlite db and tkinter ttk.treeview widget?

There is an application in tkinter. It represents the window on which we are interested in the ttk.treeview widget. This widget is organized like a table (that is, devoid of child rows). This table is loaded with data from the cqlite3 database. When a new row is added to the database, it is not immediately loaded into the table. You have to close the window and open it again so that everything is loaded and you can work with new lines. My question is: how to make the data in the table updated immediately after adding a new position to the database?

Now to the specifics:
61e5d8dda335a568718040.png

This is how the application window looks like. The top and bottom tables are formed by the same class:

class Table(Frame):
    def __init__(self, parent = None, headings = tuple(), rows = tuple(), Table_Data_Class = Table_Data()):
        super().__init__(parent)

        table = Treeview(self, show="headings", selectmode="browse")
        table["columns"] = headings
        table["displaycolumns"] = headings

        for head in headings:
            table.heading(head, text=head, anchor=CENTER)
            table.column(head, anchor=CENTER, width=20)

        for row in rows:
            table.insert('', END, values=tuple(row))

        table.bind("<ButtonRelease-1>", lambda event, tree = table: Table_Data_Class.getting_data(event, tree))

        scrolltable = Scrollbar(self, command=table.yview)
        scrolltable1 = Scrollbar(self, command= table.xview)
        table.configure(yscrollcommand=scrolltable.set)
        table.configure(xscrollcommand=scrolltable1.set)
        scrolltable.pack(side=RIGHT, fill=Y)
        scrolltable1.pack(side=BOTTOM, fill=X)
        table.pack(expand=YES, fill=BOTH)


Further in the code, they are called and transferred to the window as follows:
data = (',')
    with sqlite3.connect('data.sqlite') as connection:
        cursor = connection.cursor()
        cursor.execute("SELECT * FROM active_application WHERE [Вид обслуживания] ='РЕМОНТ'")
        data = (row for row in cursor.fetchall())
    
    data1 = (',')
    with sqlite3.connect('data.sqlite') as connection:
        cursor = connection.cursor()
        cursor.execute("SELECT * FROM active_application WHERE [Вид обслуживания] = 'TO'")
        data1 = (row for row in cursor.fetchall())

    table = Table(root, headings=('Номер заявки', 'Дата заявки', 'Гаражный номер', 'Вид ТС', 'Модель ТС', 'Гос. рег. знак', 'ДЭУ', 'Вид обслуживания', 'Категория обслуживания', 'Описание неисправности'), rows=data, Table_Data_Class = Table_Data_Class)
    table.pack ( fill=BOTH, padx=150, pady=80)
    table1 = Table(root, headings=('Номер заявки', 'Дата заявки', 'Гаражный номер', 'Вид ТС', 'Модель ТС', 'Гос. рег. знак', 'ДЭУ', 'Вид обслуживания', 'Категория обслуживания'), rows=data1, Table_Data_Class = Table_Data_Class)
    table1.pack ( fill=BOTH,  padx=150, pady=60)


Note: "Table_Data_Class" is a class that allows me to select the first position of a table row (that is, the order number) on click

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2022-01-18
@Lomonos1917

The Python module for sqlite does not do this, although sqlite itself provides an update_hook. See stackoverflow .
An easier solution is to periodically poll the server, but this can be stressful.
But considering your other question, PostgreSQL implements the LISTEN/NOTIFY syntax . There are modules for python that allow you to use this. Periodic polling will still be required, but it will consume much less resources than SELECT.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question