| 6 | import sqlite3 |
| 7 | |
| 8 | class Product: |
| 9 | # connection dir property |
| 10 | db_name = 'database.db' |
| 11 | |
| 12 | def __init__(self, window): |
| 13 | # Initializations |
| 14 | self.wind = window |
| 15 | self.wind.title('Products Application') |
| 16 | |
| 17 | # Creating a Frame Container |
| 18 | frame = LabelFrame(self.wind, text = 'Register new Product') |
| 19 | frame.grid(row = 0, column = 0, columnspan = 3, pady = 20) |
| 20 | |
| 21 | # Name Input |
| 22 | Label(frame, text = 'Name: ').grid(row = 1, column = 0) |
| 23 | self.name = Entry(frame) |
| 24 | self.name.focus() |
| 25 | self.name.grid(row = 1, column = 1) |
| 26 | |
| 27 | # Price Input |
| 28 | Label(frame, text = 'Price: ').grid(row = 2, column = 0) |
| 29 | self.price = Entry(frame) |
| 30 | self.price.grid(row = 2, column = 1) |
| 31 | |
| 32 | # Button Add Product |
| 33 | ttk.Button(frame, text = 'Save Product', command = self.add_product).grid(row = 3, columnspan = 2, sticky = W + E) |
| 34 | |
| 35 | # Output Messages |
| 36 | self.message = Label(text = '', fg = 'red') |
| 37 | self.message.grid(row = 3, column = 0, columnspan = 2, sticky = W + E) |
| 38 | |
| 39 | # Table |
| 40 | self.tree = ttk.Treeview(height = 10, columns = 2) |
| 41 | self.tree.grid(row = 4, column = 0, columnspan = 2) |
| 42 | self.tree.heading('#0', text = 'Name', anchor = CENTER) |
| 43 | self.tree.heading('#1', text = 'Price', anchor = CENTER) |
| 44 | |
| 45 | # Buttons |
| 46 | ttk.Button(text = 'DELETE', command = self.delete_product).grid(row = 5, column = 0, sticky = W + E) |
| 47 | ttk.Button(text = 'EDIT', command = self.edit_product).grid(row = 5, column = 1, sticky = W + E) |
| 48 | |
| 49 | self.exits_db_file() |
| 50 | |
| 51 | # Filling the Rows |
| 52 | self.get_products() |
| 53 | |
| 54 | # Function to verify if already exist the table in database |
| 55 | # Else create the table |
| 56 | def exits_db_file(self): |
| 57 | if not Path(self.db_name).exists(): |
| 58 | sql ='''CREATE TABLE product( |
| 59 | id INTEGER PRIMARY KEY, |
| 60 | name CHAR(60) NOT NULL, |
| 61 | price FLOAT |
| 62 | )''' |
| 63 | |
| 64 | self.run_query(sql) |
| 65 | |