Advertisement
teslariu

calculadora_

Feb 16th, 2022
1,126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.53 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 escribe cada tecla en la pantalla"""
  11.     global operacion
  12.     operacion = operacion + tecla
  13.     # ahora lo muestro en pantalla
  14.     calculo.set(operacion)
  15.  
  16.  
  17. def limpieza():
  18.     """Función que limpia la pantalla"""
  19.     global operacion
  20.     operacion = ""      # borro la cuenta del lado de la consola
  21.     calculo.set("0")    # borro la pantalla y pongo un cero
  22.    
  23.  
  24. def hacer_cuenta():
  25.     """Función que realiza el cálculo y lo imprime en la pantalla"""
  26.     global operacion
  27.    
  28.     try:
  29.         total = str(eval(operacion))
  30.     except:
  31.         limpieza()
  32.         total = "ERROR"
  33.        
  34.     calculo.set(total)
  35.  
  36. def borrar_caracter():
  37.     """Borra el último caracter ingresado"""
  38.     global operacion
  39.     lista = []
  40.    
  41.     if operacion:
  42.         # relleno la lista con los caracteres de la cuenta
  43.         for caracter in operacion:
  44.             lista.append(caracter)
  45.        
  46.         # borro el último caracter
  47.         del lista[-1]
  48.        
  49.         # rearmo la cuenta a partir de la lista
  50.         operacion = "".join(lista)
  51.        
  52.         # muestro la operación por pantalla
  53.         calculo.set(operacion)
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62. #########  Lado consola    ##############################
  63.  
  64. # creo una variable para almacenar el cálculo del lado de la consola
  65. operacion = ""
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72. #############  Lado gráfico    ###################################
  73.  
  74. ventana = tk.Tk()
  75. ventana.title("Calculadora ACME")
  76. ventana.config(width=400, height=600, bg="Light Steel Blue")
  77. ventana.resizable(0,0)
  78.  
  79. # creo una variable de cadena en entorno gráfico para almacenar el
  80. # cálculo que debo mostrar en pantalla
  81. calculo = tk.StringVar()
  82.  
  83. # al iniciar la calculadora, debo mostrar un cero en pantalla
  84. limpieza()
  85.  
  86. #### pantalla #####
  87. pantalla = tk.Entry(
  88.                 font = ('arial', 20, 'bold'),
  89.                 width = 20,
  90.                 bd = 20,
  91.                 bg = "powder blue",
  92.                 justify = "right",
  93.                 state = tk.DISABLED,
  94.                 textvariable = calculo
  95.             )
  96. pantalla.place(x=25, y=50)
  97.  
  98. #### teclado #####
  99.  
  100. # defino las dimensiones de las teclas
  101. ancho = 9
  102. alto = 2
  103. grosor = 3
  104.  
  105. # tecla para borrar un caracter
  106. tecla = tk.Button(text='<<', bd=grosor, width=ancho, height=alto, bg="medium aquamarine", command=borrar_caracter)
  107. tecla.place(x=295, y=140)
  108.  
  109.  
  110. # 1º fila de teclas: 1 2 3 +
  111. tecla = tk.Button(text='1', bd=grosor, width=ancho, height=alto, command=lambda:clic('1'))
  112. tecla.place(x=25, y=200)
  113. tecla = tk.Button(text='2', bd=grosor, width=ancho, height=alto, command=lambda:clic('2'))
  114. tecla.place(x=115, y=200)
  115. tecla = tk.Button(text='3', bd=grosor, width=ancho, height=alto, command=lambda:clic('3'))
  116. tecla.place(x=205, y=200)
  117. tecla = tk.Button(text='+', bd=grosor, width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic('+'))
  118. tecla.place(x=295, y=200)
  119.  
  120. # 2º fila de teclas: 4 5 6 -
  121. tecla = tk.Button(text='4', bd=grosor, width=ancho, height=alto, command=lambda:clic('4'))
  122. tecla.place(x=25, y=260)
  123. tecla = tk.Button(text='5', bd=grosor, width=ancho, height=alto, command=lambda:clic('5'))
  124. tecla.place(x=115, y=260)
  125. tecla = tk.Button(text='6', bd=grosor, width=ancho, height=alto, command=lambda:clic('6'))
  126. tecla.place(x=205, y=260)
  127. tecla = tk.Button(text='-', bd=grosor, width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic('-'))
  128. tecla.place(x=295, y=260)
  129.  
  130. # 3º fila de teclas: 7 8 9 x
  131. tecla = tk.Button(text='7', bd=grosor, width=ancho, height=alto, command=lambda:clic('7'))
  132. tecla.place(x=25, y=320)
  133. tecla = tk.Button(text='8', bd=grosor, width=ancho, height=alto, command=lambda:clic('8'))
  134. tecla.place(x=115, y=320)
  135. tecla = tk.Button(text='9', bd=grosor, width=ancho, height=alto, command=lambda:clic('9'))
  136. tecla.place(x=205, y=320)
  137. tecla = tk.Button(text='x', bd=grosor, width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic('x'))
  138. tecla.place(x=295, y=320)
  139.  
  140. # 4º fila de teclas: ( 0 ) /
  141. tecla = tk.Button(text='(', bd=grosor, width=ancho, height=alto, bg="sky blue", command=lambda:clic('('))
  142. tecla.place(x=25, y=380)
  143. tecla = tk.Button(text='0', bd=grosor, width=ancho, height=alto, command=lambda:clic('0'))
  144. tecla.place(x=115, y=380)
  145. tecla = tk.Button(text=')', bd=grosor, width=ancho, height=alto, bg="sky blue", command=lambda:clic(')'))
  146. tecla.place(x=205, y=380)
  147. tecla = tk.Button(text='/', bd=grosor, width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic('/'))
  148. tecla.place(x=295, y=380)
  149.  
  150. # 5º fila de teclas: Raiz coma potencia resto
  151. tecla = tk.Button(text='\u221A', bd=grosor, width=ancho, height=alto, bg="sky blue", command=lambda:clic('sqrt('))
  152. tecla.place(x=25, y=440)
  153. tecla = tk.Button(text='.', bd=grosor, width=ancho, height=alto, command=lambda:clic('.'))
  154. tecla.place(x=115, y=440)
  155. tecla = tk.Button(text='^', bd=grosor, width=ancho, height=alto, bg="sky blue", command=lambda:clic('**'))
  156. tecla.place(x=205, y=440)
  157. tecla = tk.Button(text='%', bd=grosor, width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic('%'))
  158. tecla.place(x=295, y=440)
  159.  
  160. # 6º fila de teclas: Clear Factorial PI =
  161. tecla = tk.Button(text='CL', bd=grosor, width=ancho, height=alto, bg="sky blue", command=limpieza)
  162. tecla.place(x=25, y=500)
  163. tecla = tk.Button(text='!', bd=grosor, width=ancho, height=alto, bg="sky blue", command=lambda:clic('factorial('))
  164. tecla.place(x=115, y=500)
  165. tecla = tk.Button(text='\u03C0', bd=grosor, width=ancho, height=alto, bg="sky blue", command=lambda:clic(str(pi)))
  166. tecla.place(x=205, y=500)
  167. tecla = tk.Button(text='=', bd=grosor, width=ancho, height=alto, bg="medium aquamarine", command=hacer_cuenta)
  168. tecla.place(x=295, y=500)
  169.  
  170.  
  171. ventana.mainloop()
  172.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement