Advertisement
teslariu

calculadora

Feb 2nd, 2022
1,430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.50 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import tkinter as tk
  5. from math import sqrt, factorial, pi
  6.  
  7. ##################     Funciones        ###############################
  8.  
  9. def clic(tecla):
  10.     """Función que almacena los clics de una tecla"""
  11.     global operacion
  12.     operacion = operacion + tecla   # lado consola
  13.     calculo.set(operacion)          # lado gráfico
  14.    
  15.  
  16. def limpieza():
  17.     """Función que limpia la pantalla borrando todo su contenido
  18.     y mostrando un cero en su lugar"""
  19.     global operacion
  20.     operacion = ""       # borro la cuenta de la consola
  21.     calculo.set("0")     # borro la cuenta de la pantalla
  22.  
  23.  
  24. def hacer_cuenta():
  25.     """Función que realiza el cálculo ingresado en la pantalla de
  26.     la calculadora y lo muestra en ella"""
  27.     global operacion
  28.     try:
  29.         resultado = str(eval(operacion))
  30.     except Exception:
  31.         limpieza()
  32.         resultado = "ERROR"
  33.     calculo.set(resultado)
  34.    
  35.  
  36. def borrar_caracter():
  37.     """Función que borra el último caracter ingresado en la pantalla"""
  38.     global operacion
  39.    
  40.     # creo una lista para guardar la secuencia de teclas presionadas
  41.     lista = []
  42.    
  43.     # guardo la secuencia tecla a tecla en la lista
  44.     for caracter in operacion:
  45.         lista.append(caracter)
  46.    
  47.     # borro el último caracter de la lista
  48.     lista = lista[:-1]
  49.    
  50.     # rearmo la cadena de la operación sin el último caracter
  51.     operacion = "".join(lista)
  52.    
  53.     # muestro en pantalla
  54.     calculo.set(operacion)
  55.    
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62. #######################################################################
  63.  
  64. # defino las dimensiones de las teclas
  65. ancho = 10
  66. alto = 2
  67.  
  68. # creo una variable de consola para almacenar la cuenta a realizar
  69. operacion = ""
  70.  
  71.  
  72. # ventana para contener la calculadora
  73. ventana = tk.Tk()
  74. ventana.title("Calculadora ACME")
  75. ventana.config(width=390, height=600, bg="Light Steel Blue")
  76. ventana.resizable(0,0)
  77.  
  78. # creo la variable gráfica 'calculo' para almacenar la cuenta a realizar
  79. calculo = tk.StringVar()
  80.  
  81.  
  82. # creamos una pantalla
  83. pantalla = tk.Entry(
  84.                 font = ['arial',20,'bold'],
  85.                 width = 20,
  86.                 bd = 20,      # grosor del borde de la ventana
  87.                 bg = 'powder blue',
  88.                 justify = 'right',
  89.                 state = tk.DISABLED,   # inhabilita el ingreso de datos
  90.                 textvariable = calculo # variable a mostrar (calculo)
  91.         )
  92. pantalla.place(x=20, y=50)
  93.  
  94.  
  95.  
  96. ##### Botones de la calculadora
  97.  
  98. # boton para borrar el último caracter ingresado en pantalla
  99. boton = tk.Button(text="DEL", width=ancho, height=alto, bg="medium aquamarine", command=borrar_caracter)
  100. boton.place(x= 287, y=140)
  101.  
  102.  
  103.  
  104. # primera fila: 1 2 3 +
  105. boton = tk.Button(text="1", width=ancho, height=alto, command=lambda:clic("1"))
  106. boton.place(x=17, y=200)
  107. boton = tk.Button(text="2", width=ancho, height=alto, command=lambda:clic("2"))
  108. boton.place(x=107, y=200)
  109. boton = tk.Button(text="3", width=ancho, height=alto, command=lambda:clic("3"))
  110. boton.place(x=197, y=200)
  111. boton = tk.Button(text="+", width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic("+"))
  112. boton.place(x=287, y=200)
  113.  
  114. # segunda fila: 4 5 6 -
  115. boton = tk.Button(text="4", width=ancho, height=alto, command=lambda:clic("4"))
  116. boton.place(x=17, y=260)
  117. boton = tk.Button(text="5", width=ancho, height=alto, command=lambda:clic("5"))
  118. boton.place(x=107, y=260)
  119. boton = tk.Button(text="6", width=ancho, height=alto, command=lambda:clic("6"))
  120. boton.place(x=197, y=260)
  121. boton = tk.Button(text="-", width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic("-"))
  122. boton.place(x=287, y=260)
  123.  
  124. # tercera fila: 7 8 9 x
  125. boton = tk.Button(text="7", width=ancho, height=alto, command=lambda:clic("7"))
  126. boton.place(x=17, y=320)
  127. boton = tk.Button(text="8", width=ancho, height=alto, command=lambda:clic("8"))
  128. boton.place(x=107, y=320)
  129. boton = tk.Button(text="9", width=ancho, height=alto, command=lambda:clic("9"))
  130. boton.place(x=197, y=320)
  131. boton = tk.Button(text="x", width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic("*"))
  132. boton.place(x=287, y=320)
  133.  
  134. # cuarta fila: ( 0 ) /
  135. boton = tk.Button(text="(", width=ancho, height=alto, bg="sky blue", command=lambda:clic("("))
  136. boton.place(x=17, y=380)
  137. boton = tk.Button(text="0", width=ancho, height=alto, command=lambda:clic("0"))
  138. boton.place(x=107, y=380)
  139. boton = tk.Button(text=")", width=ancho, height=alto, bg="sky blue", command=lambda:clic(")"))
  140. boton.place(x=197, y=380)
  141. boton = tk.Button(text="/", width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic("/"))
  142. boton.place(x=287, y=380)
  143.  
  144. # quinta fila: raiz, coma decimal potencia  resto
  145. boton = tk.Button(text="RAIZ", width=ancho, height=alto, bg="sky blue", command=lambda:clic("sqrt("))
  146. boton.place(x=17, y=440)
  147. boton = tk.Button(text=".", width=ancho, height=alto, command=lambda:clic("."))
  148. boton.place(x=107, y=440)
  149. boton = tk.Button(text="POWER", width=ancho, height=alto, bg="sky blue", command=lambda:clic("**"))
  150. boton.place(x=197, y=440)
  151. boton = tk.Button(text="%", width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic("%"))
  152. boton.place(x=287, y=440)
  153.  
  154. # sexta fila: Clear factorial PI =
  155. boton = tk.Button(text="CL", width=ancho, height=alto, bg="medium aquamarine", command=limpieza)
  156. boton.place(x=17, y=500)
  157. boton = tk.Button(text="!", width=ancho, height=alto, command=lambda:clic("factorial("))
  158. boton.place(x=107, y=500)
  159. boton = tk.Button(text="PI", width=ancho, height=alto, bg="sky blue", command=lambda:clic(str(pi)))
  160. boton.place(x=197, y=500)
  161. boton = tk.Button(text="=", width=ancho, height=alto, bg="medium aquamarine", command=hacer_cuenta)
  162. boton.place(x=287, y=500)
  163.  
  164.  
  165. # Lo primero que hago es llamar a limpieza
  166. limpieza()
  167.  
  168.  
  169. ventana.mainloop()
  170.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement