Let us continue with the final touch up of the python class uses to create SQL database and submits values to the SQL database’s earning table.
What we have modified from the previous Input class.
- The main program will only call the Input class once to create the SQL database and the earning table if they have not been created yet!
- We will pass the action button into the Input class and disable the action button if there is an ongoing committing job.
- The main program will pass the description and earning into the submit method of the Input class.
Below is the final revised version for both the main program and the Input class.
import tkinter as tk
from tkinter import ttk
from Input import Input
win = tk.Tk()
win.title("Earning Input")
def submit():
if(description.get()!='' and earning.get()!=""):
sub_mit.submit(description.get(), earning.get())
else:
print("You need to enter a value!")
#create label frame for ui
earn= ttk.Labelframe(win, text = "Daily Earning Input")
earn.grid(column=0, row=0, padx=4, pady=4)
# create label for description
dLabel = ttk.Label(earn, text="Description:").grid(column=0, row=0)
# create text box for description
description = tk.StringVar()
descriptionEntry = ttk.Entry(earn, width=13, textvariable=description)
descriptionEntry.grid(column=1, row=0)
# create label for earning
eLabel = ttk.Label(earn, text="Earning:").grid(column=2, row=0)
# create text box for earning
earning = tk.StringVar()
earningEntry = ttk.Entry(earn, width=13, textvariable=earning)
earningEntry.grid(column=3, row=0)
# create the action button
action = ttk.Button(earn, text="submit", command=submit)
action.grid(column=5, row=0)
win.resizable(0,0)
sub_mit = Input(action)
sub_mit.setting()
win.mainloop()
import sqlite3
class Input:
def __init__(self, action):
self.action = action
def setting(self):
self.action["state"] = "disabled"
conn = sqlite3.connect('daily_earning.db')
print("Opened database successfully")
try:
conn.execute('''CREATE TABLE DAILY_EARNING_CHART
(ID INTEGER PRIMARY KEY AUTOINCREMENT,
DESCRIPTION TEXT (50) NOT NULL,
EARNING TEXT NOT NULL,
TIME TEXT NOT NULL);''')
except:
pass
self.action["state"] = "enable"
conn.close()
def submit(self,description, earning): # Insert values into earning table
self.action["state"] = "disabled"
self.description = description
self.earning = earning
try:
sqliteConnection = sqlite3.connect('daily_earning.db')
cursor = sqliteConnection.cursor()
print("Successfully Connected to SQLite")
sqlite_insert_query = "INSERT INTO DAILY_EARNING_CHART (DESCRIPTION,EARNING,TIME) VALUES ('" + self.description + "','"+ self.earning + "',datetime('now', 'localtime'))"
count = cursor.execute(sqlite_insert_query)
sqliteConnection.commit()
print("Record inserted successfully into DAILY_EARNING_CHART table", cursor.rowcount)
cursor.close()
except sqlite3.Error as error:
print("Failed to insert earning data into sqlite table", error)
finally:
if (sqliteConnection):
sqliteConnection.close()
self.action["state"] = "enable"
The python program works alright!
More shoes have been sold!That is all, in the next chapter we will include more features into the above python class file!
from Planet Python
via read more
No comments:
Post a Comment