Advertisement
teslariu

calcu.py

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