teslariu

calculadoraACME2

Jun 13th, 2022
1,285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.62 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4.  
  5. import tkinter as tk
  6. from math import sqrt, factorial, pi
  7.  
  8.  
  9. ########## funciones ############################
  10. def clic(tecla):
  11.     global operacion
  12.     operacion = operacion + tecla
  13.     calculo.set(operacion)
  14.    
  15.    
  16. def limpieza():
  17.     global operacion
  18.     calculo.set("0")  # borro la pantalla
  19.     operacion = ""    # borro del lado consola
  20.    
  21.  
  22. def hacer_cuenta():
  23.     global operacion
  24.     try:
  25.         resultado = str(eval(operacion))
  26.     except Exception:
  27.         limpieza()
  28.         resultado = "ERROR"
  29.     calculo.set(resultado)
  30.  
  31.  
  32. def borrar_caracter():
  33.     global operacion
  34.     operacion = operacion[:-1]
  35.     if not operacion:
  36.         operacion="0"
  37.     calculo.set(operacion)
  38.      
  39.  
  40.  
  41. # Ventana para contener la calculadora
  42. ventana = tk.Tk()
  43. ventana.title("Calculadora ACME")
  44. ventana.config(width=390, height=600, bg="Light Steel Blue")
  45. ventana.resizable(0,0)
  46.  
  47.  
  48.  
  49.  
  50. ########### variables #################
  51. # creo una variable para mostrar el cálculo en pantalla
  52. calculo = tk.StringVar()
  53.  
  54. # creo la misma variable pero del lado consola
  55. operacion = ""
  56.  
  57. # llamo a limpieza antes de empezar
  58. limpieza()
  59.  
  60. # creo la pantalla
  61. pantalla = tk.Entry(
  62.                 font = ("arial",18,"bold"),
  63.                 width = 20,
  64.                 bd = 20, # relieve
  65.                 bg = "powder blue",
  66.                 justify = "right",
  67.                 state = tk.DISABLED,
  68.                 textvariable = calculo
  69.             )
  70. pantalla.place(x=20, y=50)
  71.  
  72. # defino las dimensiones de las teclas
  73. ancho = 10
  74. alto = 2
  75.  
  76. #####################  TECLAS  ########################################
  77.  
  78. # tecla para borrar un caracter
  79. b = tk.Button(text="DEL", width=ancho, height=alto, bg="medium aquamarine", command=borrar_caracter)
  80. b.place(x=287, y=140)
  81.  
  82.  
  83.  
  84.  
  85. ##### primera fila: 1 2 3 +
  86. b = tk.Button(text="1", width=ancho, height=alto, command=lambda:clic("1"))
  87. b.place(x=17, y=200)
  88. b = tk.Button(text="2", width=ancho, height=alto, command=lambda:clic("2"))
  89. b.place(x=107, y=200)
  90. b = tk.Button(text="3", width=ancho, height=alto, command=lambda:clic("3"))
  91. b.place(x=197, y=200)
  92. b = tk.Button(text="+", width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic("+"))
  93. b.place(x=287, y=200)
  94.  
  95. #### segunda fila: 4 5 6 -
  96. b = tk.Button(text="4", width=ancho, height=alto, command=lambda:clic("4"))
  97. b.place(x=17, y=260)
  98. b = tk.Button(text="5", width=ancho, height=alto, command=lambda:clic("5"))
  99. b.place(x=107, y=260)
  100. b = tk.Button(text="6", width=ancho, height=alto, command=lambda:clic("6"))
  101. b.place(x=197, y=260)
  102. b = tk.Button(text="-", width=ancho, height=alto,  bg="SteelBlue2", command=lambda:clic("-"))
  103. b.place(x=287, y=260)
  104.  
  105. ##### tercera fila: 7 8 9 x
  106. b = tk.Button(text="7", width=ancho, height=alto, command=lambda:clic("7"))
  107. b.place(x=17, y=320)
  108. b = tk.Button(text="8", width=ancho, height=alto, command=lambda:clic("8"))
  109. b.place(x=107, y=320)
  110. b = tk.Button(text="9", width=ancho, height=alto, command=lambda:clic("9"))
  111. b.place(x=197, y=320)
  112. b = tk.Button(text="x", width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic("*"))
  113. b.place(x=287, y=320)
  114.  
  115. #### cuarta fila: ( 0 ) /
  116. b = tk.Button(text="(", width=ancho, height=alto, bg="sky blue", command=lambda:clic("("))
  117. b.place(x=17, y=380)
  118. b = tk.Button(text="0", width=ancho, height=alto, command=lambda:clic("0"))
  119. b.place(x=107, y=380)
  120. b = tk.Button(text=")", width=ancho, height=alto, bg="sky blue", command=lambda:clic(")"))
  121. b.place(x=197, y=380)
  122. b = tk.Button(text="/", width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic("/"))
  123. b.place(x=287, y=380)
  124.  
  125. ##### quinta fila: raiz coma decimal potencia resto
  126. b = tk.Button(text="\u221A", width=ancho, height=alto, bg="sky blue", command=lambda:clic("sqrt("))
  127. b.place(x=17, y=440)
  128. b = tk.Button(text=".", width=ancho, height=alto, command=lambda:clic("."))
  129. b.place(x=107, y=440)
  130. b = tk.Button(text="Pot", width=ancho, height=alto, bg="sky blue", command=lambda:clic("**"))
  131. b.place(x=197, y=440)
  132. b = tk.Button(text="%", width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic("%"))
  133. b.place(x=287, y=440)
  134.  
  135. #### sexta fila: Clear Factorial pi =
  136. b = tk.Button(text="CL", width=ancho, height=alto, bg="medium aquamarine", command=limpieza)
  137. b.place(x=17, y=500)
  138. b = tk.Button(text="!", width=ancho, height=alto, command=lambda:clic("factorial("))
  139. b.place(x=107, y=500)
  140. b = tk.Button(text="\u03C0", width=ancho, height=alto, bg="sky blue", command=lambda:clic(str(pi)))
  141. b.place(x=197, y=500)
  142. b = tk.Button(text="=", width=ancho, height=alto, bg="medium aquamarine", command=hacer_cuenta)
  143. b.place(x=287, y=500)
  144.  
  145.  
  146.  
  147. ventana.mainloop()
  148.  
Advertisement
Add Comment
Please, Sign In to add comment