forked from FaztWeb/python-tkinter-sqlite-crud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
142 lines (121 loc) · 5.42 KB
/
index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from tkinter import ttk
from tkinter import *
import sqlite3
class Product:
# connection dir property
db_name = 'database.db'
def __init__(self, window):
# Initializations
self.wind = window
self.wind.title('Products Application')
# Creating a Frame Container
frame = LabelFrame(self.wind, text = 'Register new Product')
frame.grid(row = 0, column = 0, columnspan = 3, pady = 20)
# Name Input
Label(frame, text = 'Name: ').grid(row = 1, column = 0)
self.name = Entry(frame)
self.name.focus()
self.name.grid(row = 1, column = 1)
# Price Input
Label(frame, text = 'Price: ').grid(row = 2, column = 0)
self.price = Entry(frame)
self.price.grid(row = 2, column = 1)
# Button Add Product
ttk.Button(frame, text = 'Save Product', command = self.add_product).grid(row = 3, columnspan = 2, sticky = W + E)
# Output Messages
self.message = Label(text = '', fg = 'red')
self.message.grid(row = 3, column = 0, columnspan = 2, sticky = W + E)
# Table
self.tree = ttk.Treeview(height = 10, columns = 2)
self.tree.grid(row = 4, column = 0, columnspan = 2)
self.tree.heading('#0', text = 'Name', anchor = CENTER)
self.tree.heading('#1', text = 'Price', anchor = CENTER)
# Buttons
ttk.Button(text = 'DELETE', command = self.delete_product).grid(row = 5, column = 0, sticky = W + E)
ttk.Button(text = 'EDIT', command = self.edit_product).grid(row = 5, column = 1, sticky = W + E)
# Filling the Rows
self.get_products()
# Function to Execute Database Querys
def run_query(self, query, parameters = ()):
with sqlite3.connect(self.db_name) as conn:
cursor = conn.cursor()
result = cursor.execute(query, parameters)
conn.commit()
return result
# Get Products from Database
def get_products(self):
# cleaning Table
records = self.tree.get_children()
for element in records:
self.tree.delete(element)
# getting data
query = 'SELECT * FROM product ORDER BY name DESC'
db_rows = self.run_query(query)
# filling data
for row in db_rows:
self.tree.insert('', 0, text = row[1], values = row[2])
# User Input Validation
def validation(self):
return len(self.name.get()) != 0 and len(self.price.get()) != 0
def add_product(self):
if self.validation():
query = 'INSERT INTO product VALUES(NULL, ?, ?)'
parameters = (self.name.get(), self.price.get())
self.run_query(query, parameters)
self.message['text'] = 'Product {} added Successfully'.format(self.name.get())
self.name.delete(0, END)
self.price.delete(0, END)
else:
self.message['text'] = 'Name and Price is Required'
self.get_products()
def delete_product(self):
self.message['text'] = ''
try:
self.tree.item(self.tree.selection())['text'][0]
except IndexError as e:
self.message['text'] = 'Please select a Record'
return
self.message['text'] = ''
name = self.tree.item(self.tree.selection())['text']
query = 'DELETE FROM product WHERE name = ?'
self.run_query(query, (name, ))
self.message['text'] = 'Record {} deleted Successfully'.format(name)
self.get_products()
def edit_product(self):
self.message['text'] = ''
try:
self.tree.item(self.tree.selection())['values'][0]
except IndexError as e:
self.message['text'] = 'Please, select Record'
return
name = self.tree.item(self.tree.selection())['text']
old_price = self.tree.item(self.tree.selection())['values'][0]
self.edit_wind = Toplevel()
self.edit_wind.title = 'Edit Product'
# Old Name
Label(self.edit_wind, text = 'Old Name:').grid(row = 0, column = 1)
Entry(self.edit_wind, textvariable = StringVar(self.edit_wind, value = name), state = 'readonly').grid(row = 0, column = 2)
# New Name
Label(self.edit_wind, text = 'New Price:').grid(row = 1, column = 1)
new_name = Entry(self.edit_wind)
new_name.grid(row = 1, column = 2)
# Old Price
Label(self.edit_wind, text = 'Old Price:').grid(row = 2, column = 1)
Entry(self.edit_wind, textvariable = StringVar(self.edit_wind, value = old_price), state = 'readonly').grid(row = 2, column = 2)
# New Price
Label(self.edit_wind, text = 'New Name:').grid(row = 3, column = 1)
new_price= Entry(self.edit_wind)
new_price.grid(row = 3, column = 2)
Button(self.edit_wind, text = 'Update', command = lambda: self.edit_records(new_name.get(), name, new_price.get(), old_price)).grid(row = 4, column = 2, sticky = W)
self.edit_wind.mainloop()
def edit_records(self, new_name, name, new_price, old_price):
query = 'UPDATE product SET name = ?, price = ? WHERE name = ? AND price = ?'
parameters = (new_name, new_price,name, old_price)
self.run_query(query, parameters)
self.edit_wind.destroy()
self.message['text'] = 'Record {} updated successfylly'.format(name)
self.get_products()
if __name__ == '__main__':
window = Tk()
application = Product(window)
window.mainloop()