Advertisement
teslariu

calgraf

Jun 2nd, 2021
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.00 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.  
  8. # Ventana que contiene a la calculadora
  9. ventana = tk.Tk()
  10. ventana.title("CALCULADORA")
  11. ventana.config(
  12.             width = 392,
  13.             height = 600,
  14.             bg = "Light Steel Blue"
  15.             )
  16. ventana.resizable(0, 0)
  17.  
  18. # función que almacena los clics en cada tecla
  19. def clic(tecla):
  20.     global operador
  21.     operador = operador + str(tecla) # op = "1*2"
  22.     ingresa_texto.set(operador)  #
  23.  
  24. # Función para limpieza
  25. def limpieza():
  26.     global operador
  27.     ingresa_texto.set("0")
  28.     operador = ""
  29.    
  30.    
  31. # Función que efectúa la operación
  32. # eval evalúa una cadena como un sentencia de python
  33. # print(eval("2+2") arroja 4
  34. # También se manejan excepciones:
  35. # try realiza el cálculo si no hay errores
  36. # en caso contrario se ejecuta except
  37. def operacion():
  38.     global operador
  39.     try:
  40.         valor_operacion = str(eval(operador))
  41.     except Exception:
  42.         limpieza()
  43.         valor_operacion = "ERROR"
  44.     finally:
  45.         ingresa_texto.set(valor_operacion)
  46.  
  47.  
  48. ################### Programa principal  ##########################
  49.  
  50.  
  51.  
  52. # Definimos las dimensiones de las teclas
  53. ancho = 9
  54. alto = 3
  55.  
  56. # variable que almacena la operación a realizar (por ejemplo, 1+2)
  57. operador = "" # "12.35+89/58"
  58.  
  59. # Creación de una cadena en Tkinter
  60. ingresa_texto = tk.StringVar()  # Hola"
  61.  
  62.  
  63. # Limpia todo antes de empezar un cálculo
  64. limpieza()
  65.  
  66. # Creamos nuestros botones
  67. # La función lambda define funciones inline (en una línea)
  68. # primera fila: 1,2,3,+
  69. # Por ejemplo:
  70. # def suma(x,y):
  71. #   return x+y
  72. # Se escribe en notación lambda como:
  73. # b = lambda x, y : x + y
  74.  
  75. # primera fila
  76. boton1 = tk.Button(
  77.             ventana,
  78.             text = '1',
  79.             width = ancho, height = alto,
  80.             command = lambda:clic(1)
  81.             )
  82. boton1.place(x = 17, y = 180)
  83.  
  84. boton2 = tk.Button(
  85.             ventana,
  86.             text = '2',
  87.             width = ancho, height = alto,
  88.             command = lambda:clic(2)
  89.             )
  90. boton2.place(x = 107, y = 180)
  91.  
  92. boton3 = tk.Button(
  93.             ventana,
  94.             text = '3',
  95.             width = ancho, height = alto,
  96.             command = lambda:clic(3)
  97.             )
  98. boton3.place(x = 197, y = 180)
  99.  
  100. botonSuma = tk.Button(
  101.                 ventana,
  102.                 text = '+',
  103.                 width = ancho, height = alto,
  104.                 bg = "white",
  105.                 command = lambda:clic('+')
  106.                 )
  107. botonSuma.place(x = 287, y = 180)
  108.  
  109.  
  110. # segunda fila; 4,5,6,-
  111. boton4 = tk.Button(
  112.             ventana,
  113.             text = '4',
  114.             width = ancho, height = alto,
  115.             command = lambda:clic(4)
  116.             )
  117. boton4.place(x = 17, y = 240)
  118.  
  119. boton5 = tk.Button(
  120.             ventana,
  121.             text = '5',
  122.             width = ancho, height = alto,
  123.             command = lambda:clic(5)
  124.             )
  125. boton5.place(x = 107, y = 240)
  126.  
  127. boton6 = tk.Button(
  128.             ventana,
  129.             text = '6',
  130.             width = ancho, height = alto,
  131.             command=lambda:clic(6)
  132.             )
  133. boton6.place(x = 197, y = 240)
  134.  
  135. botonResta = tk.Button(
  136.                 ventana,
  137.                 text = '-',
  138.                 width = ancho, height = alto,
  139.                 bg = "white",
  140.                 command = lambda:clic('-')
  141.                 )
  142. botonResta.place(x = 287, y = 240)
  143.  
  144. # tercera fila; 7,8,9,x
  145. boton7 = tk.Button(
  146.             ventana,
  147.             text = '7',
  148.             width = ancho, height = alto,
  149.             command = lambda:clic(7)
  150.             )
  151. boton7.place(x = 17, y = 300)
  152.  
  153. boton8 = tk.Button(
  154.             ventana,
  155.             text = '8',
  156.             width = ancho, height = alto,
  157.             command = lambda:clic(8)
  158.             )
  159. boton8.place(x = 107, y = 300)
  160.  
  161. boton9 = tk.Button(
  162.             ventana,
  163.             text = '9',
  164.             width = ancho, height = alto,
  165.             command = lambda:clic(9)
  166.             )
  167. boton9.place(x = 197, y = 300)
  168.  
  169. botonMult = tk.Button(
  170.                 ventana,
  171.                 text = 'x',
  172.                 width = ancho, height = alto,
  173.                 bg = "white",
  174.                 command = lambda:clic('*')
  175.                 )
  176. botonMult.place(x = 287, y = 300)
  177.  
  178. # cuarta fila: (,0,),/
  179. botonParenIzq = tk.Button(
  180.                     ventana,
  181.                     text = '(',
  182.                     width = ancho, height = alto,
  183.                     bg = "gainsboro",
  184.                     command = lambda:clic('(')
  185.                     )
  186. botonParenIzq.place(x = 17, y = 360)
  187.  
  188. boton0 = tk.Button(
  189.             ventana,
  190.             text = '0',
  191.             width = ancho, height = alto,
  192.             command = lambda:clic(0)
  193.             )
  194. boton0.place(x = 107, y = 360)
  195.  
  196. botonParenDer = tk.Button(
  197.                     ventana,
  198.                     text = ')',
  199.                     width = ancho, height = alto,
  200.                     bg = "gainsboro",
  201.                     command = lambda:clic(')')
  202.                     )
  203. botonParenDer.place(x = 197, y = 360)
  204.  
  205. botonDiv = tk.Button(
  206.                 ventana,
  207.                 text = '/',
  208.                 width = ancho, height = alto,
  209.                 bg = "white",
  210.                 command = lambda:clic('/')
  211.                 )
  212. botonDiv.place(x = 287, y = 360)
  213.  
  214. # quinta fila: Raiz,coma decimal,Potencia,%
  215. botonRaiz = tk.Button(
  216.                 ventana,
  217.                 text = 'RAIZ',
  218.                 width = ancho, height = alto,
  219.                 bg = "gainsboro",
  220.                 command = lambda:clic('sqrt(')
  221.                 )
  222. botonRaiz.place(x = 17, y = 420)
  223.  
  224. botonComa = tk.Button(
  225.                 ventana,
  226.                 text = '.',
  227.                 width = ancho, height = alto,
  228.                 command = lambda:clic('.')
  229.                 )
  230. botonComa.place(x = 107, y = 420)
  231.  
  232. botonPotencia = tk.Button(
  233.                     ventana,
  234.                     text = 'POWER',
  235.                     width = ancho, height = alto,
  236.                     bg = "gainsboro",
  237.                     command = lambda:clic('**')
  238.                     )
  239. botonPotencia.place(x = 197, y = 420)
  240.  
  241. botonResto = tk.Button(
  242.                 ventana,
  243.                 text = '%',
  244.                 width = ancho, height = alto,
  245.                 bg = "gainsboro",
  246.                 command = lambda:clic('%')
  247.                 )
  248. botonResto.place(x = 287, y = 420)
  249.  
  250. # sexta fila: Clear,Exp,PI,=
  251. botonClear = tk.Button(
  252.                 ventana,
  253.                 text = 'CLEAR',
  254.                 width = ancho, height = alto,
  255.                 bg = "cadet blue",
  256.                 command = limpieza
  257.                 )
  258. botonClear.place(x = 17, y = 480)
  259.  
  260. botonFact = tk.Button(
  261.                 ventana,
  262.                 text = '!',
  263.                 width = ancho, height = alto,
  264.                 bg = "gainsboro",
  265.                 command = lambda:clic('factorial(')
  266.                 )
  267. botonFact.place(x = 107, y = 480)
  268.  
  269. botonPi = tk.Button(
  270.                 ventana,
  271.                 text = 'PI',
  272.                 width = ancho, height = alto,
  273.                 bg = "gainsboro",
  274.                 command = lambda:clic(pi)
  275.                 )
  276. botonPi.place(x = 197, y = 480)
  277.  
  278. botonIgual = tk.Button(
  279.                 ventana,
  280.                 text = '=',
  281.                 width = ancho, height = alto,
  282.                 bg = "cadet blue",
  283.                 command = operacion
  284.                 )
  285. botonIgual.place(x = 287, y = 480)
  286.  
  287. # Creamos la pantalla de nuestra calculadora
  288. # font formatea el tipo y tamaño de los caracteres
  289. # width y bd el ancho y tamaño del borde de la pantalla
  290. # bg el color de fondo, justify la alineación
  291. # tk.DISABLED impide el ingreso de datos manualmente en la pantalla
  292. pantalla = tk.Entry(
  293.             font = ('arial',20,'bold'),
  294.             width = 22,
  295.             textvariable = ingresa_texto,
  296.             bd = 18,
  297.             bg = "powder blue",
  298.             justify = "right",
  299.             state = tk.DISABLED
  300.             )
  301. pantalla.place(x = 10, y = 60)
  302.  
  303. ventana.mainloop()
  304.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement