-
Notifications
You must be signed in to change notification settings - Fork 0
/
cell_class.py
162 lines (103 loc) · 3.86 KB
/
cell_class.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
import csv
import customtkinter as csTK
from datetime import date
from values import *
from CTkMessagebox import CTkMessagebox
class cell :
today = date.today()
all_lines = []
def __init__(self, row, column, location) :
self.row = row
self.column = column
self.location = location
id = cell.get_current_id()
self.id = id
cell.all_lines.append(self)
@staticmethod
def get_current_id() :
if cell.all_lines == [] :
return 1
else :
return cell.all_lines[-1].id + 1
def __create_id_label(self) :
label = csTK.CTkLabel(self.location, text=self.id, width = CELL_WIDTH, height = CELL_HEIGHT, font = CELL_FONT)
self.id_obj = label
def __create_description_enrty(self, old_description = '') :
entry = csTK.CTkEntry(self.location, width = CELL_WIDTH, height = CELL_HEIGHT, font=CELL_FONT)
if old_description :
entry.insert(-1, old_description)
self.description_obj = entry
def __create_amount_enrty(self, old_amount = '') :
entry = csTK.CTkEntry(self.location, width = CELL_WIDTH, height = CELL_HEIGHT, font=CELL_FONT)
if old_amount :
entry.insert(-1, old_amount)
self.amount_obj = entry
def __create_time_label(self, old_time = '') :
if not old_time:
time = cell.today.strftime("%d/%m/%Y")
else :
time = old_time
label = csTK.CTkLabel(self.location, text=time, width = CELL_WIDTH, height = CELL_HEIGHT, font = CELL_FONT)
self.time = time
self.time_obj = label
def generate_new_line(self) :
self.__create_id_label()
self.__create_description_enrty()
self.__create_amount_enrty()
self.__create_time_label()
@staticmethod
def read_table() :
with open('data.csv', 'r', newline='') as file :
reader = csv.DictReader(file)
return list(reader)
@staticmethod
def saved_data_amount() :
return len(cell.read_table())
def display_saved_data(self) :
reader = cell.read_table()
if reader :
row = reader[self.id - 1]
self.__create_id_label()
self.__create_description_enrty(row['description'])
self.__create_amount_enrty(row['amount'])
self.__create_time_label(row['time'])
@classmethod
def save_data(cls) :
if cell.value_check() :
with open('data.csv', 'w', newline='') as file :
field_names = ['id', 'description', 'amount', 'time']
writer= csv.DictWriter(file, fieldnames = field_names)
writer.writeheader()
for line in cell.all_lines :
line_description = line.description_obj.get()
line_amount = line.amount_obj.get()
writer.writerow({
'id' : line.id,
'description' : line_description,
'amount' : line_amount,
'time' : line.time
})
@staticmethod
def calcualte_total_money_amount() :
reader = cell.read_table()
sum = 0
for row in reader :
if row['amount'].strip() != '' :
sum += float(row['amount'])
return str(sum)
@staticmethod
def is_float(string):
try:
float(string)
return True
except ValueError:
return False
@classmethod
def value_check(cls) :
for line in cell.all_lines :
amount = line.amount_obj.get()
if amount != '' and not cell.is_float(amount) :
CTkMessagebox(title="Error", message="Money amount must be digits", icon="cancel")
return False
else :
return True