patternpythonMinor
Python TKinter data entry window GUI for SQLITE3 table
Viewed 0 times
entryguitkintersqlite3pythonforwindowdatatable
Problem
I would like to offer for review a simple tkinter GUI window in Python 3 to be used for data entry into an SQLite database table. As a noob, this is my first attempt at OOP and my first experience with
The script below builds a simple table
An instance of the class
There is obviously a lot of additional logic which needs to be added (i.e. some data validation, disabling fields for PK's where the database should be assigning the next value, etc.) but I am at a place where I feel like I need to check in before I start adding complexity to the design. Here's the code:
```
"""
A simple TKinter GUI to enter data into a given table in a database.
This program will build a small sample table into a given database
and then build a simple TKinter window with label and entry widgets
for each column in the table.
"""
import sqlite3
import tkinter as tk
from tkinter import N, S, E, W
from tkinter import TOP, BOTTOM, LEFT, RIGHT, END, ALL
def main():
"""Main function for demo."""
# define some variables for demo only.
# In full app, these will come programmatically.
db = 'foo.sqlite'
tbl = 'bar'
columns = 'ID integer primary key', 'bizz text', 'bam text'
create_table(db, tbl, *columns)
root = tk.Tk()
demo_window = EntryWindow(root, *[db, tbl])
root.mainloop()
# create a sample table for demo purposes only.
# in full app the database
tkinter, therefore, I was hoping for a thoughough review before I got too carried away. The script below builds a simple table
bar in a sqlite database foo.sqlite. This is for demo purposes. In reality, the database schema would be built elsewhere.An instance of the class
EntryWindow provides the GUI for the window. Upon initialization, the window will read the column names of the given table (bar) from the sqlite database (foo.sqlite) and the build TK labels from the column names as well as entry fields for each column. Finally, there is a button added which, when pressed will get the values from the entry fields and add a row to the database.There is obviously a lot of additional logic which needs to be added (i.e. some data validation, disabling fields for PK's where the database should be assigning the next value, etc.) but I am at a place where I feel like I need to check in before I start adding complexity to the design. Here's the code:
```
"""
A simple TKinter GUI to enter data into a given table in a database.
This program will build a small sample table into a given database
and then build a simple TKinter window with label and entry widgets
for each column in the table.
"""
import sqlite3
import tkinter as tk
from tkinter import N, S, E, W
from tkinter import TOP, BOTTOM, LEFT, RIGHT, END, ALL
def main():
"""Main function for demo."""
# define some variables for demo only.
# In full app, these will come programmatically.
db = 'foo.sqlite'
tbl = 'bar'
columns = 'ID integer primary key', 'bizz text', 'bam text'
create_table(db, tbl, *columns)
root = tk.Tk()
demo_window = EntryWindow(root, *[db, tbl])
root.mainloop()
# create a sample table for demo purposes only.
# in full app the database
Solution
There are a few things that need looking at.
There seems to be an error in the
You seem to have gotten confused with the use of
In 'main' you have
and
The asterisks (i.e.
So
would pass the columns list as the first item of the
Also
would pass the database and table list as the first item of the
and
Note the double indices. The first accesses the first item in the
You could change the call to
and then use
and
as you have done but to access the items within the
There seems to be an error in the
def add_item method. The 'Build the SQL' section joins sections for table, column names and new values from the entry boxes but the last two items in the .format method BOTH insert 'column_names' whereas I would expect the last one to be 'entries'.You seem to have gotten confused with the use of
*args.In 'main' you have
create_table(db, tbl, *columns)and
demo_window = EntryWindow(root, *[db, tbl])The asterisks (i.e.
*) is used in the method DEFINITION as a place holder for the arguments that may not appear in the method call. If they do appear then they will be accessible as a tuple in the body of the method definition.So
create_table(db, tbl, columns) # no *would pass the columns list as the first item of the
*col_defs in the create_table method and be accessible as col_defs[0].Also
demo_window = EntryWindow(root, [db, tbl]) # no *would pass the database and table list as the first item of the
*args in the EntryWindow method to be accessible as args[0][0]and
args[0][1].Note the double indices. The first accesses the first item in the
*args tuple (even if there is only one!) and the second accesses the item within your list.You could change the call to
demo_window = EntryWindow(root, db, tbl) # no list and still no *and then use
args[0]and
args[1]as you have done but to access the items within the
*args tuple not items within a list.Code Snippets
create_table(db, tbl, *columns)demo_window = EntryWindow(root, *[db, tbl])create_table(db, tbl, columns) # no *col_defs[0].demo_window = EntryWindow(root, [db, tbl]) # no *Context
StackExchange Code Review Q#87319, answer score: 2
Revisions (0)
No revisions yet.