-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
233 lines (192 loc) · 5.1 KB
/
main.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# import modules
from tkinter import *
from tkinter import ttk
import datetime as dt
from mydb import *
from tkinter import messagebox
# object for database
data = Database(db='test.db')
# global variables
count = 0
selected_rowid = 0
# functions
def saveRecord():
global data
data.insertRecord(item_name=item_name.get(), item_price=item_amt.get(), purchase_date=transaction_date.get())
def setDate():
date = dt.datetime.now()
dopvar.set(f'{date:%d %B %Y}')
def clearEntries():
item_name.delete(0, 'end')
item_amt.delete(0, 'end')
transaction_date.delete(0, 'end')
def fetch_records():
f = data.fetchRecord('select rowid, * from expense_record')
global count
for rec in f:
tv.insert(parent='', index='0', iid=count, values=(rec[0], rec[1], rec[2], rec[3]))
count += 1
tv.after(400, refreshData)
def select_record(event):
global selected_rowid
selected = tv.focus()
val = tv.item(selected, 'values')
try:
selected_rowid = val[0]
d = val[3]
namevar.set(val[1])
amtvar.set(val[2])
dopvar.set(str(d))
except Exception as ep:
pass
def update_record():
global selected_rowid
selected = tv.focus()
# Update record
try:
data.updateRecord(namevar.get(), amtvar.get(), dopvar.get(), selected_rowid)
tv.item(selected, text="", values=(namevar.get(), amtvar.get(), dopvar.get()))
except Exception as ep:
messagebox.showerror('Error', ep)
# Clear entry boxes
item_name.delete(0, END)
item_amt.delete(0, END)
transaction_date.delete(0, END)
tv.after(400, refreshData)
def totalBalance():
f = data.fetchRecord(query="Select sum(item_price) from expense_record")
for i in f:
for j in i:
messagebox.showinfo('Current Balance: ', f"Total Expense: ' {j} \nBalance Remaining: {2000 - j}")
def refreshData():
for item in tv.get_children():
tv.delete(item)
fetch_records()
def deleteRow():
global selected_rowid
data.removeRecord(selected_rowid)
refreshData()
# create tkinter object
ws = Tk()
ws.title('Daily Expenses')
# variables
f = ('Times new roman', 14)
namevar = StringVar()
amtvar = IntVar()
dopvar = StringVar()
# Frame widget
f2 = Frame(ws)
f2.pack()
f1 = Frame(
ws,
padx=10,
pady=10,
)
f1.pack(expand=True, fill=BOTH)
# Label widget
Label(f1, text='ITEM NAME', font=f).grid(row=0, column=0, sticky=W)
Label(f1, text='ITEM PRICE', font=f).grid(row=1, column=0, sticky=W)
Label(f1, text='PURCHASE DATE', font=f).grid(row=2, column=0, sticky=W)
# Entry widgets
item_name = Entry(f1, font=f, textvariable=namevar)
item_amt = Entry(f1, font=f, textvariable=amtvar)
transaction_date = Entry(f1, font=f, textvariable=dopvar)
# Entry grid placement
item_name.grid(row=0, column=1, sticky=EW, padx=(10, 0))
item_amt.grid(row=1, column=1, sticky=EW, padx=(10, 0))
transaction_date.grid(row=2, column=1, sticky=EW, padx=(10, 0))
# Action buttons
cur_date = Button(
f1,
text='Current Date',
font=f,
bg='#04C4D9',
command=setDate,
width=15
)
submit_btn = Button(
f1,
text='Save Record',
font=f,
command=saveRecord,
bg='#42602D',
fg='white'
)
clr_btn = Button(
f1,
text='Clear Entry',
font=f,
command=clearEntries,
bg='#D9B036',
fg='white'
)
quit_btn = Button(
f1,
text='Exit',
font=f,
command=lambda:ws.destroy(),
bg='#D33532',
fg='white'
)
total_bal = Button(
f1,
text='Total Balance',
font=f,
bg='#486966',
command=totalBalance
)
total_spent = Button(
f1,
text='Total Spent',
font=f,
command=lambda:data.fetchRecord('select sum(ite)')
)
update_btn = Button(
f1,
text='Update',
bg='#C2BB00',
command=update_record,
font=f
)
del_btn = Button(
f1,
text='Delete',
bg='#BD2A2E',
command=deleteRow,
font=f
)
# grid placement
cur_date.grid(row=3, column=1, sticky=EW, padx=(10, 0))
submit_btn.grid(row=0, column=2, sticky=EW, padx=(10, 0))
clr_btn.grid(row=1, column=2, sticky=EW, padx=(10, 0))
quit_btn.grid(row=2, column=2, sticky=EW, padx=(10, 0))
total_bal.grid(row=0, column=3, sticky=EW, padx=(10, 0))
update_btn.grid(row=1, column=3, sticky=EW, padx=(10, 0))
del_btn.grid(row=2, column=3, sticky=EW, padx=(10, 0))
# Treeview widget
tv = ttk.Treeview(f2, columns=(1, 2, 3, 4), show='headings', height=8)
tv.pack(side="left")
# add heading to treeview
tv.column(1, anchor=CENTER, stretch=NO, width=70)
tv.column(2, anchor=CENTER)
tv.column(3, anchor=CENTER)
tv.column(4, anchor=CENTER)
tv.heading(1, text="Serial no")
tv.heading(2, text="Item Name", )
tv.heading(3, text="Item Price")
tv.heading(4, text="Purchase Date")
# binding treeview
tv.bind("<ButtonRelease-1>", select_record)
# style for treeview
style = ttk.Style()
style.theme_use("default")
style.map("Treeview")
# Vertical scrollbar
scrollbar = Scrollbar(f2, orient='vertical')
scrollbar.configure(command=tv.yview)
scrollbar.pack(side="right", fill="y")
tv.config(yscrollcommand=scrollbar.set)
# calling function
fetch_records()
# infinite loop
ws.mainloop()