teslariu

calc.py

Apr 20th, 2021
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.24 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. """ Programa que implementa una calculadora"""
  5.  
  6. import tkinter as tk
  7. from math import factorial, sqrt, pi
  8.  
  9. # creo la ventana
  10. ventana = tk.Tk()
  11. ventana.title("Calculadora")
  12. ventana.config(
  13.         width = 390,
  14.         height = 600,
  15.         bg = "Light Steel Blue"
  16.         )
  17. ventana.resizable(0, 0)
  18.  
  19. # funciones
  20. def clic(tecla):
  21.     '''Función que almacena los clics de cada tecla'''
  22.     global operacion
  23.     operacion = operacion + str(tecla)
  24.     calculo.set(operacion)
  25.    
  26. def limpieza():
  27.     '''Función que limpia la pantalla'''
  28.     global operacion # uso la variable operacion del prog. principal
  29.     operacion = ""   # borro lo escrito en la pantalla
  30.     calculo.set("0")
  31.    
  32.    
  33. def calcular():
  34.     '''Efectua la operacion mostrada en la pantalla
  35.    a traves de la función eval cuando es llamada por clic("=")'''
  36.     global operacion
  37.     try:
  38.         total = str(eval(operacion))
  39.     except:
  40.         total = "ERROR"
  41.     calculo.set(total)
  42.  
  43. # definimos el tamaño de las teclas
  44. ancho_tecla = 11
  45. alto_tecla = 3
  46.  
  47. # creacion de la variable que muestra el cálculo a
  48. # realizar en la pantalla de la calculadora
  49. calculo = tk.StringVar()
  50.  
  51. # variable que almacena la operación matemàtica a realizar
  52. operacion = ""
  53.  
  54. # Inicio la calculadora poniendo un cero en pantalla
  55. limpieza()
  56.  
  57.  
  58. ################# Layout de la calculadora  #######################  
  59.  
  60. # pantalla de la calculadora
  61. pantalla = tk.Entry(
  62.             font = ('arial', 20, 'bold'),
  63.             width = 22,
  64.             bd = 20, # tamaño del borde
  65.             bg = "powder blue",
  66.             justify = "right",
  67.             state = tk.DISABLED,
  68.             textvariable = calculo
  69.             )
  70. pantalla.place(x=10, y=60)
  71.  
  72. # primera fila de teclas: 1 2 3 +
  73. boton1 = tk.Button(text='1', width=ancho_tecla, height=alto_tecla, command=lambda:clic(1))
  74. boton1.place(x=17, y=180)
  75. boton2 = tk.Button(text='2', width=ancho_tecla, height=alto_tecla, command=lambda:clic(2))
  76. boton2.place(x=107, y=180)
  77. boton3 = tk.Button(text='3', width=ancho_tecla, height=alto_tecla, command=lambda:clic(3))
  78. boton3.place(x=197, y=180)
  79. botonSuma = tk.Button(text='+', width=ancho_tecla, height=alto_tecla, bg="gainsboro", command=lambda:clic('+'))
  80. botonSuma.place(x=287, y=180)        
  81.  
  82. # segunda fila de teclas: 4 5 6 -
  83. boton4 = tk.Button(text='4', width=ancho_tecla, height=alto_tecla, command=lambda:clic(4))
  84. boton4.place(x=17, y=240)
  85. boton5 = tk.Button(text='5', width=ancho_tecla, height=alto_tecla, command=lambda:clic(5))
  86. boton5.place(x=107, y=240)
  87. boton6 = tk.Button(text='6', width=ancho_tecla, height=alto_tecla, command=lambda:clic(6))
  88. boton6.place(x=197, y=240)
  89. botonResta = tk.Button(text='-', width=ancho_tecla, height=alto_tecla, bg="gainsboro", command=lambda:clic('-'))
  90. botonResta.place(x=287, y=240)
  91.  
  92. # tercera fila de teclas: 7 8 9 x
  93. boton7 = tk.Button(text='7', width=ancho_tecla, height=alto_tecla, command=lambda:clic(7))
  94. boton7.place(x=17, y=300)
  95. boton8 = tk.Button(text='8', width=ancho_tecla, height=alto_tecla, command=lambda:clic(8))
  96. boton8.place(x=107, y=300)
  97. boton9 = tk.Button(text='9', width=ancho_tecla, height=alto_tecla, command=lambda:clic(9))
  98. boton9.place(x=197, y=300)
  99. botonMult = tk.Button(text='x', width=ancho_tecla, height=alto_tecla, bg="gainsboro", command=lambda:clic('*'))
  100. botonMult.place(x=287, y=300)        
  101.  
  102. # cuarta fila de teclas: ( 0 ) /
  103. botonParenDer = tk.Button(text='(', width=ancho_tecla, height=alto_tecla, bg="gainsboro", command=lambda:clic('('))
  104. botonParenDer.place(x=17, y=360)
  105. boton0 = tk.Button(text='0', width=ancho_tecla, height=alto_tecla, command=lambda:clic(0))
  106. boton0.place(x=107, y=360)
  107. botonParenIzq = tk.Button(text=')', width=ancho_tecla, height=alto_tecla, bg="gainsboro", command=lambda:clic(')'))
  108. botonParenIzq.place(x=197, y=360)
  109. botonDiv = tk.Button(text='/', width=ancho_tecla, height=alto_tecla, bg="gainsboro", command=lambda:clic('/'))
  110. botonDiv.place(x=287, y=360)
  111.  
  112. # quinta fila de teclas: Raiz coma decimal potencia %
  113. botonRaiz = tk.Button(text='RAIZ', width=ancho_tecla, height=alto_tecla, bg="gainsboro", command=lambda:clic('sqrt('))
  114. botonRaiz.place(x=17, y=420)
  115. botonComa = tk.Button(text='.', width=ancho_tecla, height=alto_tecla, bg="gainsboro", command=lambda:clic('.'))
  116. botonComa.place(x=107, y=420)
  117. botonPower = tk.Button(text='POWER', width=ancho_tecla, height=alto_tecla, bg="gainsboro", command=lambda:clic('**'))
  118. botonPower.place(x=197, y=420)
  119. botonResto = tk.Button(text='%', width=ancho_tecla, height=alto_tecla, bg="gainsboro", command=lambda:clic('%'))
  120. botonResto.place(x=287, y=420)
  121.  
  122. # sexta fila de teclas: Clear Factorial PI =
  123. botonClear = tk.Button(text='CLEAR', width=ancho_tecla, height=alto_tecla, bg="cadet blue", command=limpieza)
  124. botonClear.place(x=17, y=480)
  125. botonFact = tk.Button(text='!', width=ancho_tecla, height=alto_tecla, bg="gainsboro", command=lambda:clic('factorial('))
  126. botonFact.place(x=107, y=480)
  127. botonPi = tk.Button(text='Pi', width=ancho_tecla, height=alto_tecla, bg="gainsboro", command=lambda:clic('pi'))
  128. botonPi.place(x=197, y=480)
  129. botonIgual = tk.Button(text='=', width=ancho_tecla, height=alto_tecla, bg="cadet blue", command=calcular)
  130. botonIgual.place(x=287, y=480)
  131.  
  132.  
  133.  
  134.  
  135.  
  136.        
  137. ventana.mainloop()
  138.  
Advertisement
Add Comment
Please, Sign In to add comment