-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp1.py
More file actions
200 lines (168 loc) · 7.49 KB
/
p1.py
File metadata and controls
200 lines (168 loc) · 7.49 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import datetime
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import mysql.connector
from tkcalendar import DateEntry
win= Tk()
win.title("Expense Tracker")
win.geometry("1200x600")
win.resizable(0,0)
#connection to the database
mydb= mysql.connector.connect(host='localhost', user='root', password='1234', database='expenseTracker')
curr= mydb.cursor()
#functionalities
def addExpense():
global curr, mydb
if not date.get() or not payee.get() or not description.get() or not amount.get() or not modeOfPayment.get():
messagebox.showerror('Error', "Please fill all the missing fields!")
else:
statement= 'INSERT INTO expenses(Date, Payee, Description, Amount, ModeOfPayment) VALUES (%s, %s, %s, %s, %s)'
tup=(date.get_date(), payee.get(), description.get(), amount.get(), modeOfPayment.get())
curr.execute(statement, tup)
mydb.commit()
getAllExpense()
messagebox.showinfo("Success!", "Expense added successfully!")
date.get_date= datetime.datetime.today()
payee.set('')
description.set('')
amount.set(0.0)
modeOfPayment.set('Cash')
def getAllExpense():
global curr, table
table.delete(*table.get_children())
statement= 'SELECT * FROM EXPENSES'
curr.execute(statement)
res= curr.fetchall()
#print(res)
for values in res:
table.insert('', END, values=values)
def deleteExpense():
global mydb, curr, table
if not table.selection():
messagebox.showerror('No record selected!', 'Please select a record to delete!')
return
currExpense= table.item(table.focus())
val= currExpense['values']
check= messagebox.askyesno("Warning", "Do you really want to delete this record?")
if check:
statement='DELETE FROM EXPENSES WHERE ID=%s'
curr.execute(statement, (val[0], ))
mydb.commit()
getAllExpense()
messagebox.showinfo("Success!", "Deleted!")
def totalExpense():
global curr
statement= 'SELECT SUM(Amount) from expenses'
curr.execute(statement)
sum= curr.fetchall()[0][0]
if sum!= None:
messagebox.showinfo("Success!", f"Total Sum of Expense Amount recorded:{sum}")
else:
messagebox.showinfo("Success!", f"Total Sum of Expense Amount recorded:{0.0}")
def deleteAllRecords():
global mydb,curr
st='select * from expenses'
curr.execute(st)
res= curr.fetchall()
if len(res)==0:
messagebox.showerror("Error", "There is no record in the table.")
else:
option= messagebox.askyesno("Warning", "Do you really want to delete all the records?")
if option:
statement='DELETE FROM expenses'
curr.execute(statement)
mydb.commit()
messagebox.showinfo("Success!", "All the records are deleted.")
getAllExpense()
def updateRecord():
global mydb, curr, table
st='select * from expenses'
curr.execute(st)
res= curr.fetchall()
if len(res)==0:
messagebox.showerror("Error", "There is no record in the table.")
else:
if not table.selection():
messagebox.showerror('No record selected!', 'Please select a record to update!')
return
currExpense= table.item(table.focus())
val= currExpense['values']
date.set_date(datetime.date(int(val[1][:4]), int(val[1][5:7]), int(val[1][8:])))
payee.set(val[2])
description.set(val[3])
amount.set(val[4])
modeOfPayment.set(val[5])
def update():
global mydb, curr
if not date.get() or not payee.get() or not description.get() or not amount.get() or not modeOfPayment.get():
messagebox.showerror('Error', "Please fill all the missing fields!")
else:
statement='update expenses set Date=%s, Payee=%s, Description=%s, Amount=%s, ModeOfPayment=%s where ID=%s'
values=(date.get_date(), payee.get(), description.get(), amount.get(), modeOfPayment.get(), val[0])
curr.execute(statement, values)
mydb.commit()
messagebox.showinfo("Success", "The records have been updated!")
date.get_date= datetime.datetime.today()
payee.set('')
description.set('')
amount.set(0.0)
modeOfPayment.set('Cash')
getAllExpense()
editButton.destroy()
editButton= Button(dataEntryFrame,text="Edit Expense" , command=update, width=36)
editButton.place(x=10, y=375)
#initialization
payee= StringVar()
description= StringVar()
amount= DoubleVar()
modeOfPayment= StringVar(value='Cash')
dataEntryFrame = Frame(win)
dataEntryFrame.place(x=0, y=30, relheight=0.95, relwidth=0.25)
buttonFrame = Frame(win)
buttonFrame.place(relx=0.25, rely=0.05, relwidth=0.75, relheight=0.21)
treeFrame = Frame(win)
treeFrame.place(relx=0.25, rely=0.26, relwidth=0.75, relheight=0.74)
#Form
Label(dataEntryFrame, text='Created Date (MM/DD/YY) :').place(x=10, y=50)
date = DateEntry(dataEntryFrame, date=datetime.datetime.now().date())
date.place(x=180, y=50)
Label(dataEntryFrame, text='Payee :').place(x=10, y=230)
Entry(dataEntryFrame, width=40, text=payee).place(x=10, y=260)
Label(dataEntryFrame, text='Description :').place(x=10, y=100)
Entry(dataEntryFrame, width=40, text=description).place(x=10, y=130)
Label(dataEntryFrame, text='Amount :').place(x=10, y=180)
Entry(dataEntryFrame, width=20, text=amount).place(x=160, y=180)
Label(dataEntryFrame, text='Mode of Payment :').place(x=10, y=300)
dd1 = OptionMenu(dataEntryFrame, modeOfPayment, *['Cash', 'Cheque', 'Credit Card', 'Debit Card', 'Paytm', 'Google Pay'])
dd1.configure(width=16)
dd1.place(x=150, y=305)
Button(dataEntryFrame,text="Add Expense" , command=addExpense, width=36).place(x=10, y=375)
#Additional Features
Button(buttonFrame, text="Get Total Amount of All Expenses", command=totalExpense).place(x=30, y=5)
Button(buttonFrame, text="Delete All Records", command=deleteAllRecords).place(x=250, y=5)
Button(buttonFrame, text="Delete Selected Expense Record", command=deleteExpense).place(x=450, y=5)
Button(buttonFrame, text="Update Selected Record", command=updateRecord).place(x=700, y=5)
table = ttk.Treeview(treeFrame, selectmode=BROWSE, columns=('ID', 'Date', 'Payee', 'Description', 'Amount', 'Mode of Payment'))
X_Scroll = Scrollbar(table, orient=HORIZONTAL, command=table.xview)
Y_Scroll = Scrollbar(table, orient=VERTICAL, command=table.yview)
X_Scroll.pack(side=BOTTOM, fill=X)
Y_Scroll.pack(side=RIGHT, fill=Y)
table.config(yscrollcommand=Y_Scroll.set, xscrollcommand=X_Scroll.set)
table.heading('ID', text='ID', anchor=CENTER)
table.heading('Date', text='Date', anchor=CENTER)
table.heading('Payee', text='Payee', anchor=CENTER)
table.heading('Description', text='Description', anchor=CENTER)
table.heading('Amount', text='Amount', anchor=CENTER)
table.heading('Mode of Payment', text='Mode of Payment', anchor=CENTER)
table.column('#0', width=0, stretch=NO)
table.column('#1', width=30, stretch=NO)
table.column('#2', width=95, stretch=NO)
table.column('#3', width=150, stretch=NO)
table.column('#4', width=300, stretch=NO)
table.column('#5', width=135, stretch=NO)
table.column('#6', width=130, stretch=NO)
table.place(relx=0, y=0, relheight=1, relwidth=1)
getAllExpense()
win.update()
win.mainloop()