Advertisement
teslariu

calculadora

Dec 23rd, 2021
1,477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.14 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. # defino una función limpieza para borrar el contenido del cálculo
  8. def limpieza():
  9.     global operacion
  10.     calculo.set("0") # pone un cero en la pantalla
  11.     operacion = ""   # borra la cuenta de la consola
  12.    
  13. # defino la función que almacena los clics de cada tecla
  14. def clic(tecla):
  15.     global operacion
  16.     operacion = operacion + tecla
  17.     calculo.set(operacion)
  18.    
  19. def hacer_cuenta():
  20.     global operacion
  21.     try:
  22.         total = str(eval(operacion))
  23.     except Exception:
  24.         limpieza()
  25.         total = "ERROR"
  26.     calculo.set(total)
  27.     operacion = total
  28.    
  29. def borrar_caracter():
  30.     global operacion
  31.     lista = []
  32.     # creo una lista con los valores de operacion
  33.     for i in range(len(operacion)):
  34.         lista.append(operacion[i])
  35.     # borro el último caracter
  36.     lista = lista[:-1]
  37.     # rearmo la cadena sin el último caracter a partir de la lista
  38.     operacion = "".join(lista)
  39.     # muestro la cadena en pantalla
  40.     calculo.set(operacion)
  41.  
  42. # Ventana para contener la calculadora
  43. ventana = tk.Tk()
  44. ventana.title("Calculadora ACME")
  45. ventana.config(width=390, height=600, bg="Light Steel Blue")
  46. ventana.resizable(0,0)
  47.  
  48. # Creamos una variable para mostrar el cálculo en la pantalla
  49. calculo = tk.StringVar()
  50.  
  51. # llamo a la funcion limpieza para inicializar la calculadora
  52. limpieza()
  53.  
  54. # creamos una pantalla
  55. pantalla = tk.Entry(
  56.                 font = ('arial',20,'bold'),
  57.                 width = 20,
  58.                 bd = 20,
  59.                 bg = 'powder blue',
  60.                 justify = 'right',
  61.                 state = tk.DISABLED,
  62.                 textvariable = calculo
  63.     )
  64. pantalla.place(x = 20, y = 50)
  65.  
  66. # defino las dimensiones de las teclas
  67. ancho = 10
  68. alto = 2
  69.  
  70. # creo una variable para almacenar el cálculo del lado de consola
  71. operacion = ""
  72.  
  73. ############ BOTONES ##################
  74.  
  75. #### Primera fila: 1 2 3 +
  76. boton1 = tk.Button(text='1', width=ancho, height=alto, command=lambda:clic("1"))
  77. boton1.place(x=17, y=180)
  78.  
  79. boton2 = tk.Button(text='2', width=ancho, height=alto, command=lambda:clic("2"))
  80. boton2.place(x=107, y=180)
  81.  
  82. boton3 = tk.Button(text='3', width=ancho, height=alto, command=lambda:clic("3"))
  83. boton3.place(x=197, y=180)
  84.  
  85. boton_Suma = tk.Button(text='+', width=ancho, height=alto, bg="steel blue2", command=lambda:clic("+"))
  86. boton_Suma.place(x=287, y=180)
  87.  
  88. #### Segunda fila: 4 5 6 -
  89. boton4 = tk.Button(text='4', width=ancho, height=alto, command=lambda:clic("4"))
  90. boton4.place(x=17, y=240)
  91.  
  92. boton5 = tk.Button(text='5', width=ancho, height=alto, command=lambda:clic("5"))
  93. boton5.place(x=107, y=240)
  94.  
  95. boton6 = tk.Button(text='6', width=ancho, height=alto, command=lambda:clic("6"))
  96. boton6.place(x=197, y=240)
  97.  
  98. boton_Resta = tk.Button(text='-', width=ancho, height=alto, bg="steel blue2", command=lambda:clic("-"))
  99. boton_Resta.place(x=287, y=240)
  100.  
  101. #### Tercera fila: 7 8 9 x
  102. boton7 = tk.Button(text='7', width=ancho, height=alto, command=lambda:clic("7"))
  103. boton7.place(x=17, y=300)
  104.  
  105. boton8 = tk.Button(text='8', width=ancho, height=alto, command=lambda:clic("8"))
  106. boton8.place(x=107, y=300)
  107.  
  108. boton9 = tk.Button(text='9', width=ancho, height=alto)
  109. boton9.place(x=197, y=300)
  110.  
  111. boton_Por = tk.Button(text='x', width=ancho, height=alto, bg="steel blue2", command=lambda:clic("*"))
  112. boton_Por.place(x=287, y=300)
  113.  
  114. #### Cuarta fila: ( 0 ) /
  115. boton_par_izq = tk.Button(text='(', width=ancho, height=alto, bg="sky blue", command=lambda:clic("("))
  116. boton_par_izq.place(x=17, y=360)
  117.  
  118. boton0 = tk.Button(text='0', width=ancho, height=alto, command=lambda:clic("0"))
  119. boton0.place(x=107, y=360)
  120.  
  121. boton_par_der = tk.Button(text=')', width=ancho, height=alto, bg="sky blue", command=lambda:clic(")"))
  122. boton_par_der.place(x=197, y=360)
  123.  
  124. boton_Division = tk.Button(text='/', width=ancho, height=alto, bg="steel blue2", command=lambda:clic("/"))
  125. boton_Division.place(x=287, y=360)
  126.  
  127. #### Quinta fila: Raiz, coma decimal, potencia, resto
  128. boton_raiz = tk.Button(text='RAIZ', width=ancho, height=alto, bg="sky blue", command=lambda:clic("sqrt("))
  129. boton_raiz.place(x=17, y=420)
  130.  
  131. boton_coma = tk.Button(text='.', width=ancho, height=alto, command=lambda:clic("."))
  132. boton_coma.place(x=107, y=420)
  133.  
  134. boton_potencia = tk.Button(text='POWER', width=ancho, height=alto, bg="sky blue", command=lambda:clic("**"))
  135. boton_potencia.place(x=197, y=420)
  136.  
  137. boton_resto = tk.Button(text='%', width=ancho, height=alto, bg="steel blue2", command=lambda:clic("%"))
  138. boton_resto.place(x=287, y=420)
  139.  
  140. #### Sexta fila: Clear, Factorial PI =
  141. boton_clear = tk.Button(text='CL', width=ancho, height=alto, bg="medium aquamarine", command=limpieza)
  142. boton_clear.place(x=17, y=480)
  143.  
  144. boton_factorial = tk.Button(text='!', width=ancho, height=alto, command=lambda:clic("factorial("))
  145. boton_factorial.place(x=107, y=480)
  146.  
  147. boton_pi = tk.Button(text='PI', width=ancho, height=alto, bg="sky blue", command=lambda:clic(str(pi)))
  148. boton_pi.place(x=197, y=480)
  149.  
  150. boton_igual = tk.Button(text='=', width=ancho, height=alto, bg="medium aquamarine", command=hacer_cuenta)
  151. boton_igual.place(x=287, y=480)
  152.  
  153. # botón para borrar un caracter
  154. boton_del = tk.Button(text='DEL', width=ancho, height=alto, bg="medium aquamarine", command=borrar_caracter)
  155. boton_del.place(x=17, y=540)
  156.  
  157.  
  158. ventana.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement