Repository navigation
Expand file tree
/
Copy pathRPN-CALC4.py
More file actions
204 lines (189 loc) · 8.42 KB
/
Copy pathRPN-CALC4.py
File metadata and controls
204 lines (189 loc) · 8.42 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
201
202
203
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from tkinter import *
ventana=Tk()
ventana.title("RPN-CALC4")
ventana.configure(background="gray20")
ventana.geometry("392x488")
color_boton=("gray50")
cn=("white")
actb="LightCyan3" #activebackground
from math import *
def digit(n):
global numero
global l_numeros
global blocked_ce
blocked_ce=False
long=len(l_numeros)
if long<2 and numero!=str(pi):
if numero=="0":
numero=numero.replace("0",n)
else:
numero=numero+n
input_text.set(numero)
def loga():
global l_numeros
global numero
if len(l_numeros)==2:
try:
numero=str(eval("log("+l_numeros[0]+")/log("+l_numeros[1]+")")) #l_numeros[0] es el numero y l_numeros[1] es la base
input_text.set(numero)
l_numeros[0]=numero
l_numeros.pop()
except:
input_text.set("ERROR")
l_numeros=[]
numero=""
def pee():
global numero
global l_numeros
global comas
global blocked_ce
if len(l_numeros)<2 and numero=="":
numero=str(pi)
input_text.set(numero)
comas+=1
blocked_ce=False
def coma():
global numero
global comas
if numero!="" and comas==0:
numero=numero+"."
input_text.set(numero)
comas+=1
def enter():
global numero
global l_numeros
global comas
global blocked_ce
global active_round
if numero!="" and numero!="0.":
if active_round==True:
numero=str(eval("round("+str(numero)+")"))
l_numeros.append(numero)
active_round=False
else:
l_numeros.append(numero)
input_text.set(numero)
numero=""
comas=0
blocked_ce=True
def operacion(s):
global numero
global l_numeros
global prev_sign
global reep
if len(l_numeros)==2:
try:
numero=str(eval(l_numeros[0]+s+l_numeros[1]))
input_text.set(numero)
l_numeros[0]=numero
reep=l_numeros[1]
l_numeros.pop()
prev_sign=s
print(l_numeros)
except:
input_text.set("ERROR")
l_numeros=[]
numero=""
elif len(l_numeros)==1 and prev_sign==s:
numero=eval(l_numeros[0]+s+reep)
input_text.set(numero)
l_numeros[0]=str(numero)
print(l_numeros)
numero=""
def funci(s):
global numero
global l_numeros
if len(l_numeros)==1:
try:
numero=str(eval(s+"("+l_numeros[0]+")"))#[0]
input_text.set(numero)
l_numeros[0]=numero
prev_sign=s
except:
input_text.set("ERROR")
l_numeros=[]
numero=""
def rounded():
global numero
global active_round
global l_numeros
if not numero.endswith("."):
active_round=True
if numero!="":
numero=eval("round("+str(numero)+")")
input_text.set(numero)
else:
l_numeros[-1]=str(eval("round("+l_numeros[-1]+")"))
input_text.set(l_numeros[-1])
def cambia_signo():
global numero
global l_numeros
if numero!="0" and numero!="":
numero=str(eval(numero+"*(-1)"))
input_text.set(numero)
elif numero=="" and len(l_numeros)==1: #nuevo
if l_numeros[0]!="0":
l_numeros[0]=str(eval(l_numeros[0]+"*(-1)"))
input_text.set(l_numeros[0])
def clear():
global numero
global l_numeros
global comas
numero=""
l_numeros=[]
input_text.set("0")
comas=0
def clear_error():
global numero
global comas
global blocked_ce
if blocked_ce==False:
numero=""
input_text.set("0")
comas=0
blocked_ce=True
ancho_boton=6
active_round=False
numero=("")
blocked_ce=False
comas=0
reep=""
alto_boton=2
prev_sign=""
input_text=StringVar()
clear()#MUESTRA VALOR "0" AL INICIAR LA CALCULADORA
bd=10
Button(ventana,text="0",bg=color_boton,fg=cn,activebackground=actb,width=ancho_boton,height=alto_boton,command=lambda:digit("0")).place(x=21,y=180)
Button(ventana,text="1",bg=color_boton,fg=cn,activebackground=actb,width=ancho_boton,height=alto_boton,command=lambda:digit("1")).place(x=80,y=180)
Button(ventana,text="2",bg=color_boton,fg=cn,activebackground=actb,width=ancho_boton,height=alto_boton,command=lambda:digit("2")).place(x=139,y=180)
Button(ventana,text="3",bg=color_boton,fg=cn,activebackground=actb,width=ancho_boton,height=alto_boton,command=lambda:digit("3")).place(x=198,y=180)
Button(ventana,text="4",bg=color_boton,fg=cn,activebackground=actb,width=ancho_boton,height=alto_boton,command=lambda:digit("4")).place(x=21,y=228)
Button(ventana,text="5",bg=color_boton,fg=cn,activebackground=actb,width=ancho_boton,height=alto_boton,command=lambda:digit("5")).place(x=80,y=228)
Button(ventana,text="6",bg=color_boton,fg=cn,activebackground=actb,width=ancho_boton,height=alto_boton,command=lambda:digit("6")).place(x=139,y=228)
Button(ventana,text="7",bg=color_boton,fg=cn,activebackground=actb,width=ancho_boton,height=alto_boton,command=lambda:digit("7")).place(x=198,y=228)
Button(ventana,text="8",bg=color_boton,fg=cn,activebackground=actb,width=ancho_boton,height=alto_boton,command=lambda:digit("8")).place(x=257,y=228)
Button(ventana,text="9",bg=color_boton,fg=cn,activebackground=actb,width=ancho_boton,height=alto_boton,command=lambda:digit("9")).place(x=316,y=228)
Button(ventana,text="π",bg=color_boton,fg=cn,activebackground=actb,width=ancho_boton,height=alto_boton,command=pee).place(x=21,y=276)
Button(ventana,text=".",bg=color_boton,fg=cn,activebackground=actb,width=ancho_boton,height=alto_boton,command=coma).place(x=80,y=276)
Button(ventana,text="+",bg=color_boton,fg=cn,activebackground=actb,width=ancho_boton,height=alto_boton,command=lambda:operacion("+")).place(x=139,y=276)
Button(ventana,text="-",bg=color_boton,fg=cn,activebackground=actb,width=ancho_boton,height=alto_boton,command=lambda:operacion("-")).place(x=198,y=276)
Button(ventana,text="*",bg=color_boton,fg=cn,activebackground=actb,width=ancho_boton,height=alto_boton,command=lambda:operacion("*")).place(x=257,y=276)
Button(ventana,text="/",bg=color_boton,fg=cn,activebackground=actb,width=ancho_boton,height=alto_boton,command=lambda:operacion("/")).place(x=316,y=276)
Button(ventana,text="√",bg=color_boton,fg=cn,activebackground=actb,width=ancho_boton,height=alto_boton,command=lambda:funci("sqrt")).place(x=21,y=324)
Button(ventana,text="1/x",bg=color_boton,fg=cn,activebackground=actb,width=ancho_boton,height=alto_boton,command=lambda:funci("1/")).place(x=198,y=324)
Button(ventana,text="log",bg=color_boton,fg=cn,activebackground=actb,width=ancho_boton,height=alto_boton,command=loga).place(x=257,y=324)
Button(ventana,text="%",bg=color_boton,fg=cn,activebackground=actb,width=ancho_boton,height=alto_boton,command=lambda:operacion("%")).place(x=80,y=324)
Button(ventana,text="ln",bg=color_boton,fg=cn,activebackground=actb,width=ancho_boton,height=alto_boton,command=lambda:funci("log")).place(x=21,y=372)
Button(ventana,text="sin",bg=color_boton,fg=cn,activebackground=actb,width=ancho_boton,height=alto_boton,command=lambda:funci("sin")).place(x=80,y=372)
Button(ventana,text="cos",bg=color_boton,fg=cn,activebackground=actb,width=ancho_boton,height=alto_boton,command=lambda:funci("cos")).place(x=139,y=372)
Button(ventana,text="tan",bg=color_boton,fg=cn,activebackground=actb,width=ancho_boton,height=alto_boton,command=lambda:funci("tan")).place(x=198,y=372)
Button(ventana,text="R",bg=color_boton,fg=cn,activebackground=actb,width=ancho_boton,height=alto_boton,command=rounded).place(x=257,y=372)
Button(ventana,text="CE",bg="red",fg=cn,activebackground="indianred1",width=ancho_boton,height=alto_boton,command=clear_error).place(x=257,y=180)
Button(ventana,text="+/-",bg=color_boton,fg=cn,activebackground=actb,width=ancho_boton,height=alto_boton,command=cambia_signo).place(x=139,y=324)
Button(ventana,text="C",bg="red",fg=cn,activebackground="indianred1",width=ancho_boton,height=alto_boton,command=clear).place(x=316,y=180)
Button(ventana,text="EXP",bg=color_boton,fg=cn,activebackground=actb,width=ancho_boton,height=alto_boton,command=lambda:operacion("**")).place(x=316,y=324)
Button(ventana,text="ENTER",bg=color_boton,fg=cn,activebackground=actb,width=ancho_boton,height=alto_boton,command=enter).place(x=316,y=372)
Entry(ventana,font=('Arial',20,"bold"),width=21,textvariable=input_text,bd=20,insertwidth=4,bg="lavender",justify="right").place(x=16,y=60)
ventana.mainloop()