-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
151 lines (123 loc) · 6.51 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
import tkinter as tk
import functions as myfns
from tkinter import ttk
from Credit import Credit
class App:
def __init__(self, master):
self.master = master
# DC - Titulo barra principal
master.title("Formula de Prestamo")
# DC - Dimensiones de la ventana principal
master.geometry("900x500")
self.put_frames()
self.put_title()
self.put_form()
self.put_result()
self.master.bind('<Return>', lambda event=None: self.btnCalculate.invoke())
def put_frames(self):
# Frames or panels
self.frame_title = tk.Frame(self.master, bg="green", height=40)
self.frame_form = tk.Frame(self.master)
self.frame_result = tk.Frame(self.frame_form)
self.frame_title.pack(side="top", fill='both')
self.frame_form.pack(side="bottom", expand=True)
self.frame_result.grid(row=5, columnspan=2, sticky="NSEW")
self.frame_result.grid_forget()
def put_title(self):
lbl_title = tk.Label(self.frame_title, text="Formulas de Prestamos", bg="green", foreground="white",
font="white")
lbl_title.pack(expand=True)
def add_dots(self, action, text_val, P, old_text, index):
P = P.replace('.', '') # quitamos los puntos, nos quedamos solo con los dígitos
if P.isdigit() or len(P) == 0:
P = P[::-1] # damos vuelta el string
dotted_string = '.'.join(P[i:i + 3] for i in range(0, len(P), 3))
formatted_string = dotted_string[::-1] # volvemos a dar vuelta la cadena
old_idx = self.txtValCredit.index(tk.INSERT)
old_txt = self.txtValCredit.get()
old_num_dots = old_txt.count(".")
num_dots = formatted_string.count(".")
self.txtValCredit.delete(0, tk.END)
self.txtValCredit.insert(0, formatted_string)
if len(old_txt) + 2 == len(formatted_string):
old_idx = old_idx + 2
else:
old_idx = old_idx + 1
if old_idx == len(formatted_string):
if "." in formatted_string:
self.txtValCredit.icursor(old_idx + num_dots + 1)
else:
self.txtValCredit.icursor(old_idx + 1)
else:
# Modificando en el medio de la cadena
self.txtValCredit.icursor(old_idx)
if action == "0":
if num_dots < old_num_dots:
self.txtValCredit.icursor(int(index) - 1)
else:
self.txtValCredit.icursor(int(index))
return True
else:
return False
def put_form(self):
self.lblValCredit = tk.Label(self.frame_form, text="Valor del crédito $:")
self.valTextValCredit = tk.StringVar()
vcmd = (self.frame_form.register(self.add_dots), "%d", "%S", '%P', "%s", "%i")
self.txtValCredit = ttk.Entry(self.frame_form, font="15", textvariable=self.valTextValCredit, validate='key',
validatecommand=vcmd)
self.lblValCredit.grid(pady=15, row=1, column=0, sticky="W")
self.txtValCredit.grid(pady=15, row=1, column=1)
self.lblInterest = tk.Label(self.frame_form, text="Tasa de interes (%):")
vcmd_float = (self.frame_form.register(myfns.validate_float), '%P')
self.txtInterest = tk.Entry(self.frame_form, font="15", validate='key', validatecommand=vcmd_float)
self.lblInterest.grid(pady=15, row=2, column=0, sticky="W")
self.txtInterest.grid(pady=15, row=2, column=1)
lblNumCuotas = tk.Label(self.frame_form, text="Num de cuotas (meses):")
vcmd_int = (self.frame_form.register(myfns.validate_int), '%P')
self.txtNumCuotas = tk.Entry(self.frame_form, font="15", validate='key', validatecommand=vcmd_int)
lblNumCuotas.grid(pady=15, row=3, column=0, sticky="W")
self.txtNumCuotas.grid(pady=15, row=3, column=1)
self.btnCalculate = tk.Button(self.frame_form, text="Calcular", bg="green", fg="white", command=self.calculate)
self.btnLimpiar = tk.Button(self.frame_form, text="Limpiar", bg="orange",
command=lambda: myfns.clean_form([self.frame_form, self.frame_result]))
self.btnLimpiar.grid(pady=15, row=4, column=0, sticky="W")
self.btnCalculate.grid(pady=15, row=4, column=1, sticky="NSEW")
def put_result(self):
self.lblPlazo = tk.Label(self.frame_result, text="Plazo:")
self.txtPlazo = tk.Entry(self.frame_result, font="15", state='readonly')
self.lblPlazo.grid(pady=15, row=0, column=0, sticky="W")
self.txtPlazo.grid(pady=15, row=0, column=1)
self.lblCuota = tk.Label(self.frame_result, text="Cuota mensual a pagar:", anchor='w')
self.txtCuota = tk.Entry(self.frame_result, font="15", state='readonly')
self.lblCuota.grid(pady=15, row=1, column=0, sticky="W")
self.txtCuota.grid(pady=15, row=1, column=1)
self.lblCostoFinanciacion = tk.Label(self.frame_result, text="Total valor de la financiación:", anchor='e')
self.txtCostoFinanciacion = tk.Entry(self.frame_result, font="15", state='readonly')
self.lblCostoFinanciacion.grid(pady=15, row=2, column=0, sticky="W")
self.txtCostoFinanciacion.grid(pady=15, row=2, column=1)
def calculate(self):
print("Calculando...")
try:
# DC - Show frame resultados
self.frame_result.grid(row=5, columnspan=2, sticky="NSEW")
# DC - Limpia valores de resultados
# self.clean_results()
myfns.clean_form([self.frame_result])
myfns.change_state_entry_form([self.frame_result], "normal")
cant_prestamo = int(self.txtValCredit.get().replace(".", "").replace(",", ""))
num_cuotas = int(self.txtNumCuotas.get())
interes = float(self.txtInterest.get()) / 100
# DC - Crea objeto Prestamo
prestamo = Credit(cant_prestamo, num_cuotas, interes)
self.txtPlazo.insert(0, prestamo.num_payments)
self.txtCuota.insert(0, myfns.format_currency(prestamo.payment))
self.txtCostoFinanciacion.insert(0, myfns.format_currency(prestamo.financial_cost))
except Exception as e:
self.txtPlazo.insert(0, 'ERROR')
self.txtCuota.insert(0, 'ERROR')
self.txtCostoFinanciacion.insert(0, 'ERROR')
else:
myfns.change_state_entry_form([self.frame_result], "readonly")
root = tk.Tk()
main_gui = App(root)
root.mainloop()