Advertisement
teslariu

calculadora

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