How to create an editable table in Python for a desktop app?

Viewed 1743

I'm trying to do this table as a desktop app (not web). It should be an editable table, allowing the user to introduce his own data (inputs), saving it at the database (connected to the database with SQLite) and if there is already some information in the database, show it into the table and not letting the user change it. This makes a table like I want but I don't know how to use grid with it because it takes all screen and I want it below the select list option. Thank you so much! I will let my code below.

from tkinter import ttk
from tkinter import *
from tkcalendar import Calendar, DateEntry

import sqlite3

class Client:

    dbName = 'Database.db' 

    def __init__(self, window):
        self.wind = window
        self.wind.title('Aplicación cliente')

    #Container
        frame = LabelFrame(self.wind, text = 'Registrar un nuevo cliente')
        frame.grid(row=0, column=0, columnspan=1, padx=20, pady=20)

    #Name input
        Label(frame, text = 'Nombre: ').grid(row=1, column=0)
        self.name = Entry(frame)
        self.name.focus
        self.name.grid(row=1, column=1, padx=10, pady=5)

    #Birth date input
        Label(frame, text = 'Fecha de nacimiento: ').grid(row=2, column=0)
        self.calendar = DateEntry(frame,width=30,bg="darkblue",fg="white")
        self.calendar.grid(row=2, column=1, padx=10, pady=5)

        Label(frame, text = 'Fecha de bautismo: ').grid(row=3, column=0)
        self.calendar = DateEntry(frame,width=30,bg="darkblue",fg="white")
        self.calendar.grid(row=3, column=1, padx=10, pady=5)

    #Select list
        var = StringVar()

        Label(frame, text = 'Grupo: ').grid(row=4, column=0)
        self.dropdownList = OptionMenu(frame, var, 'Hombre', 'Mujer', 'Anciano', 'Siervo ministerial', 'Precursor regular', 'Ungido', 'Otras ovejas')
        self.dropdownList.grid(row=4, column=1, padx=10, pady=5)

        rows = []
        for i in range(12):
            cols = []
            for j in range(7):
                e = Entry(relief=RIDGE)
                e.grid(row=i, column=j, sticky=NSEW)
                e.insert(END, '%d.%d' % (i, j))
                cols.append(e)
            rows.append(cols)
            
        def onPress():
            for row in rows:
                for col in row:
                    print(col.get())
                print
        
        Button(text='Fetch', command=onPress).grid()

    def run_query(self, query, parameters=()):
        with sqlite3.connect(self.dbName) as conn:
            cursor = conn.cursor()
            result = cursor.execute(query, parameters)
            conn.commit()
        return result

    def getClient(self):

        records = self.tree.get_children()
        for element in records:
            self.tree.delete(element)

        query = 'SELECT * FROM Client'
        dbRows = self.run_query(query)
        for row in dbRows:
            self.tree.insert('',0, text=row[1], values=row[2])

if __name__ == '__main__':
    window = Tk()
    application = Client(window)
    window.mainloop()
0 Answers
Related