-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculadoraV4.py
More file actions
148 lines (109 loc) · 4.56 KB
/
CalculadoraV4.py
File metadata and controls
148 lines (109 loc) · 4.56 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
from tkinter import *
#-----------------------------------------------------------------------------
raiz = Tk()
raiz.title("Calculator")
miFrame = Frame(raiz, bg="black")
miFrame.pack()
operacion = ""
resultado = 0.0
reset_pantalla = False
#---------------------------- PANTALLA ------------------------
numeroPantalla = StringVar()
pantalla = Entry(
miFrame,
textvariable=numeroPantalla,
bg="black",
fg="#03f943",
justify="right",
font=("Arial", 14)
)
pantalla.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
#-------------------- PULSACIONES ------------------------------
def pulsados(num):
global reset_pantalla
if reset_pantalla:
numeroPantalla.set(num)
reset_pantalla = False
return
if num == "." and "." in numeroPantalla.get():
return
numeroPantalla.set(numeroPantalla.get() + num)
#---------------------- CÁLCULO CENTRAL ------------------------
def calcular():
global resultado, operacion
num_actual = float(numeroPantalla.get())
if operacion == "suma":
resultado += num_actual
elif operacion == "resta":
resultado -= num_actual
elif operacion == "multi":
resultado *= num_actual
elif operacion == "div":
resultado /= num_actual
#---------------------- OPERACIONES ----------------------------
def suma(num):
global resultado, operacion, reset_pantalla
if operacion != "":
calcular()
else:
resultado = float(num)
operacion = "suma"
reset_pantalla = True
def resta(num):
global resultado, operacion, reset_pantalla
if operacion != "":
calcular()
else:
resultado = float(num)
operacion = "resta"
reset_pantalla = True
def multi(num):
global resultado, operacion, reset_pantalla
if operacion != "":
calcular()
else:
resultado = float(num)
operacion = "multi"
reset_pantalla = True
def div(num):
global resultado, operacion, reset_pantalla
if operacion != "":
calcular()
else:
resultado = float(num)
operacion = "div"
reset_pantalla = True
#---------------------- RESULTADO -------------------------------
def resultado_final():
global operacion
if operacion != "":
calcular()
numeroPantalla.set(resultado)
operacion = ""
#----------------------- LIMPIAR -------------------------------
def clear():
global resultado, operacion, reset_pantalla
resultado = 0.0
operacion = ""
reset_pantalla = False
numeroPantalla.set("")
#---------------------- BOTONES --------------------------------
Button(miFrame, text="7", width=5, height=2, bg="#d4af37", command=lambda:pulsados("7")).grid(row=1, column=0)
Button(miFrame, text="8", width=5, height=2, bg="#d4af37", command=lambda:pulsados("8")).grid(row=1, column=1)
Button(miFrame, text="9", width=5, height=2, bg="#d4af37", command=lambda:pulsados("9")).grid(row=1, column=2)
Button(miFrame, text="/", width=5, height=2, bg="#d4af37", command=lambda:div(numeroPantalla.get())).grid(row=1, column=3)
Button(miFrame, text="4", width=5, height=2, bg="#d4af37", command=lambda:pulsados("4")).grid(row=2, column=0)
Button(miFrame, text="5", width=5, height=2, bg="#d4af37", command=lambda:pulsados("5")).grid(row=2, column=1)
Button(miFrame, text="6", width=5, height=2, bg="#d4af37", command=lambda:pulsados("6")).grid(row=2, column=2)
Button(miFrame, text="*", width=5, height=2, bg="#d4af37", command=lambda:multi(numeroPantalla.get())).grid(row=2, column=3)
Button(miFrame, text="1", width=5, height=2, bg="#d4af37", command=lambda:pulsados("1")).grid(row=3, column=0)
Button(miFrame, text="2", width=5, height=2, bg="#d4af37", command=lambda:pulsados("2")).grid(row=3, column=1)
Button(miFrame, text="3", width=5, height=2, bg="#d4af37", command=lambda:pulsados("3")).grid(row=3, column=2)
Button(miFrame, text="-", width=5, height=2, bg="#d4af37", command=lambda:resta(numeroPantalla.get())).grid(row=3, column=3)
Button(miFrame, text="0", width=5, height=2, bg="#d4af37", command=lambda:pulsados("0")).grid(row=4, column=0)
Button(miFrame, text=".", width=5, height=2, bg="#d4af37", command=lambda:pulsados(".")).grid(row=4, column=1)
Button(miFrame, text="=", width=5, height=2, bg="#d4af37", command=resultado_final).grid(row=4, column=2)
Button(miFrame, text="+", width=5, height=2, bg="#d4af37", command=lambda:suma(numeroPantalla.get())).grid(row=4, column=3)
Button(miFrame, text="C", width=5, height=2, bg="#ff6b6b", command=clear).grid(row=0, column=3)
#----------------------------------------------------------------
raiz.mainloop()