teslariu

calcu

Sep 17th, 2021 (edited)
198
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. # archivo calculadora.py
  4. """
  5. para instalar la libreria pyinstaller:
  6. python -m pip install pyinstaller
  7.  
  8. para crear un ejecutable portable:
  9. pyinstaller --noconsole --onefile calculadora.py
  10.  
  11.  
  12. import tkinter as tk
  13. from math import sqrt, factorial, pi
  14.  
  15. ###########  funciones ###############################################
  16.  
  17.  
  18. def clic(tecla):
  19.     global operador
  20.     operador = operador + str(tecla) # "23+5"
  21.     ver_en_pantalla.set(operador)
  22.    
  23.    
  24. # Función para limpieza
  25. def limpieza():
  26.    global operador
  27.    ver_en_pantalla.set("0")
  28.    operador = ""
  29.    
  30. def calcular():
  31.     global operador
  32.     try:
  33.         valor_operacion = str(eval(operador))
  34.     except Exception:
  35.         limpieza()
  36.         valor_operacion = "ERROR"
  37.     ver_en_pantalla.set(valor_operacion)
  38.  
  39.  
  40.  
  41. # Ventana que contiene a la calculadora
  42. ventana = tk.Tk()
  43. ventana.title("CALCULADORA")
  44. ventana.config(width = 392, height = 600, bg = "Light Steel Blue")    
  45. ventana.resizable(0, 0)
  46.  
  47.  
  48. ###########  variables  #############################################
  49.  
  50. # Definimos las dimensiones de las teclas
  51. ancho = 9
  52. alto = 3
  53.  
  54.  
  55. # variable que almacena la operación a realizar (por ejemplo, 1+2)
  56. operador = "" # "12.35+89/58"
  57.  
  58.  
  59. # Creación de la cadena en Tkinter para mostrar en pantalla
  60. ver_en_pantalla = tk.StringVar()
  61.  
  62.  
  63. ##############  main    ##############################################
  64.  
  65. # Limpia todo antes de empezar
  66. limpieza()
  67.  
  68.  
  69. # Creamos la pantalla de nuestra calculadora
  70. # font formatea el tipo y tamaño de los caracteres
  71. # width y bd el ancho y tamaño del borde de la pantalla
  72. # bg el color de fondo, justify la alineación
  73. # tk.DISABLED impide el ingreso de datos manualmente en la pantalla
  74. pantalla = tk.Entry(
  75.            font = ['arial',20,'bold'],
  76.            width = 22,
  77.            textvariable = ver_en_pantalla,
  78.            bd = 18,
  79.            bg = "powder blue",
  80.            justify = "right",
  81.            state = tk.DISABLED
  82.            )
  83. pantalla.place(x = 10, y = 60)
  84.  
  85.  
  86. # Creamos nuestros botones
  87. # La función lambda define funciones inline (en una línea)
  88. # primera fila: 1,2,3,+
  89. # Por ejemplo:
  90. # def suma(x,y):
  91. #   return x+y
  92. # Se escribe en notación lambda como:
  93. # b = lambda x, y : x + y
  94.  
  95.  
  96. # primera fila
  97. boton1 = tk.Button(text='1', width=ancho, height=alto, command=lambda:clic(1))
  98. boton1.place(x=17, y=180)
  99. boton2 = tk.Button(text='2', width=ancho, height=alto, command=lambda:clic(2))
  100. boton2.place(x=107, y=180)
  101. boton3 = tk.Button(text='3', width=ancho, height=alto, command=lambda:clic(3))
  102. boton3.place(x=197, y=180)
  103. botonSuma = tk.Button(text = '+', width=ancho, height=alto, bg="white", command=lambda:clic('+'))
  104. botonSuma.place(x=287, y=180)
  105.  
  106.  
  107. # segunda fila; 4,5,6,-
  108. boton4 = tk.Button(text='4', width=ancho, height=alto, command = lambda:clic(4))
  109. boton4.place(x=17, y=240)
  110. boton5 = tk.Button(text = '5', width = ancho, height = alto, command = lambda:clic(5))
  111. boton5.place(x = 107, y = 240)
  112. boton6 = tk.Button(text = '6', width = ancho, height = alto, command=lambda:clic(6))
  113. boton6.place(x = 197, y = 240)
  114. botonResta = tk.Button(text= '-', width=ancho, height=alto, bg = "white", command=lambda:clic('-'))
  115. botonResta.place(x = 287, y = 240)
  116.  
  117.  
  118. # tercera fila; 7,8,9,x
  119. boton7 = tk.Button(text = '7',width = ancho, height = alto,command = lambda:clic(7))
  120. boton7.place(x = 17, y = 300)
  121. boton8 = tk.Button(text = '8',width = ancho, height = alto,command = lambda:clic(8))
  122. boton8.place(x = 107, y = 300)
  123. boton9 = tk.Button(text = '9', width = ancho, height = alto,command = lambda:clic(9))
  124. boton9.place(x = 197, y = 300)
  125. botonMult = tk.Button(text = 'x', width = ancho, height = alto,bg = "white",command = lambda:clic('*'))
  126. botonMult.place(x = 287, y = 300)
  127.  
  128.  
  129. # cuarta fila: (,0,),/
  130. botonParenIzq = tk.Button(text = '(', width = ancho, height = alto,bg = "gainsboro",command = lambda:clic('('))
  131. botonParenIzq.place(x = 17, y = 360)
  132. boton0 = tk.Button(text = '0',width = ancho, height = alto,command = lambda:clic(0))
  133. boton0.place(x = 107, y = 360)
  134. botonParenDer = tk.Button(text = ')',width = ancho, height = alto,bg = "gainsboro",command = lambda:clic(')'))
  135. botonParenDer.place(x = 197, y = 360)
  136. botonDiv = tk.Button(text = '/',width = ancho, height = alto,bg = "white",command = lambda:clic('/'))
  137. botonDiv.place(x = 287, y = 360)
  138.  
  139.  
  140. # quinta fila: Raiz,coma decimal,Potencia,%
  141. botonRaiz = tk.Button(text = 'RAIZ',width = ancho, height = alto, bg = "gainsboro",command = lambda:clic('sqrt('))
  142. botonRaiz.place(x = 17, y = 420)
  143. botonComa = tk.Button(text = '.',width = ancho, height = alto,command = lambda:clic('.'))
  144. botonComa.place(x = 107, y = 420)
  145. botonPotencia = tk.Button(text = 'POWER', width = ancho, height = alto,bg = "gainsboro",command = lambda:clic('**'))
  146. botonPotencia.place(x = 197, y = 420)
  147. botonResto = tk.Button(text = '%',width = ancho, height = alto,bg = "gainsboro",command = lambda:clic('%'))
  148. botonResto.place(x = 287, y = 420)
  149.  
  150.  
  151. # sexta fila: Clear,Exp,PI,=
  152. botonClear = tk.Button(text = 'CLEAR',width = ancho, height = alto,bg = "cadet blue",command = limpieza)
  153. botonClear.place(x = 17, y = 480)
  154. botonFact = tk.Button(text = '!',width = ancho, height = alto,bg = "gainsboro",command = lambda:clic('factorial('))
  155. botonFact.place(x = 107, y = 480)
  156. botonPi = tk.Button(text = 'PI', width = ancho, height = alto, bg = "gainsboro",command = lambda:clic(pi))
  157. botonPi.place(x = 197, y = 480)
  158. botonIgual = tk.Button(text = '=', width = ancho, height = alto, bg = "cadet blue", command = calcular)
  159. botonIgual.place(x = 287, y = 480)
  160.  
  161.  
  162. ventana.mainloop()
  163.  
  164.  
Add Comment
Please, Sign In to add comment