teslariu

Untitled

Jan 2nd, 2021 (edited)
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.28 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. """
  5. Programa que implementa una calculadora científica básica
  6. """
  7.  
  8. import tkinter as tk
  9. from math import factorial, sqrt, pi
  10.  
  11. def clic(tecla):
  12.     '''Funcion que almacena los clics de cada tecla'''
  13.     global cuenta
  14.     cuenta = cuenta + str(tecla) #cuenta = "1+2" eval(1+2) --> 3
  15.     resultado.set(cuenta)
  16.    
  17. def limpieza():
  18.     global cuenta
  19.     cuenta = ""
  20.     resultado.set("0")
  21.    
  22. def hacer_cuenta():
  23.     global cuenta
  24.     try:
  25.         total = str(eval(cuenta))
  26.     except:
  27.         limpieza()
  28.         total = "ERROR"
  29.     resultado.set(total)
  30.    
  31.  
  32.  
  33. ###### programa principal #############################
  34.  
  35. ventana = tk.Tk()
  36. ventana.config(width=392, height=600, bg="light steel blue")
  37. ventana.title("Calculadora")
  38. ventana.resizable(0,0)
  39.  
  40. # variable que almacena una cadena con los clics de cada tecla
  41. cuenta = ""
  42.  
  43. # variable que muestra el resultado por pantalla
  44. resultado = tk.StringVar()
  45. resultado.set("0")
  46.  
  47. # ancho y alto de los teclas
  48. ancho = 10
  49. alto = 3
  50.  
  51. # primera fila : 1 2 3 +
  52. boton1 = tk.Button(text="1", width=ancho, height=alto, command=lambda:clic(1))
  53. boton1.place(x=17, y=180)
  54. boton2 = tk.Button(text="2", width=ancho, height=alto, command=lambda:clic(2))
  55. boton2.place(x=107, y=180)
  56. boton3 = tk.Button(text="3", width=ancho, height=alto, command=lambda:clic(3))
  57. boton3.place(x=197, y=180)
  58. botonSuma = tk.Button(text="+", bg="white", width=ancho, height=alto, command=lambda:clic('+'))
  59. botonSuma.place(x=287, y=180)
  60.  
  61. # segunda fila : 4 5 6 -
  62. boton4 = tk.Button(text="4", width=ancho, height=alto, command=lambda:clic(4))
  63. boton4.place(x=17, y=240)
  64. boton5 = tk.Button(text="5", width=ancho, height=alto, command=lambda:clic(5))
  65. boton5.place(x=107, y=240)
  66. boton6 = tk.Button(text="6", width=ancho, height=alto, command=lambda:clic(6))
  67. boton6.place(x=197, y=240)
  68. botonResta = tk.Button(text="-", bg="white", width=ancho, height=alto, command=lambda:clic('-'))
  69. botonResta.place(x=287, y=240)
  70.  
  71. # tercera fila : 7 8 9 x
  72. boton7 = tk.Button(text="7", width=ancho, height=alto, command=lambda:clic(7))
  73. boton7.place(x=17, y=300)
  74. boton8 = tk.Button(text="8", width=ancho, height=alto, command=lambda:clic(8))
  75. boton8.place(x=107, y=300)
  76. boton9 = tk.Button(text="9", width=ancho, height=alto, command=lambda:clic(9))
  77. boton9.place(x=197, y=300)
  78. botonPor = tk.Button(text="x", width=ancho, bg="white", height=alto, command=lambda:clic('*'))
  79. botonPor.place(x=287, y=300)
  80.  
  81. # cuarta fila : ( 0 ) /
  82. botonParI = tk.Button(text="(", width=ancho, height=alto, command=lambda:clic('('))
  83. botonParI.place(x=17, y=360)
  84. botonCero = tk.Button(text="0", width=ancho, height=alto, command=lambda:clic(0))
  85. botonCero.place(x=107, y=360)
  86. botonParD = tk.Button(text=")", width=ancho, height=alto, command=lambda:clic(')'))
  87. botonParD.place(x=197, y=360)
  88. botonDiv = tk.Button(text="/", bg="white",width=ancho, height=alto, command=lambda:clic('/'))
  89. botonDiv.place(x=287, y=360)
  90.  
  91. # quinta fila : Raiz punto decimal potencia resto
  92. botonRaiz = tk.Button(text="RAIZ", width=ancho, height=alto, command=lambda:clic('sqrt('))
  93. botonRaiz.place(x=17, y=420)
  94. botonPunto = tk.Button(text=".", width=ancho, height=alto, command=lambda:clic('.'))
  95. botonPunto.place(x=107, y=420)
  96. botonPot = tk.Button(text="POWER", width=ancho, height=alto, command=lambda:clic('**'))
  97. botonPot.place(x=197, y=420)
  98. botonResto = tk.Button(text="%", bg="gainsboro",width=ancho, height=alto, command=lambda:clic('%'))
  99. botonResto.place(x=287, y=420)
  100.  
  101. # sexta fila : clear factorial pi =
  102. boton1 = tk.Button(text="CLEAR", width=ancho, height=alto, command=limpieza)
  103. boton1.place(x=17, y=480)
  104. boton2 = tk.Button(text="!", width=ancho, height=alto, command=lambda:clic('factorial('))
  105. boton2.place(x=107, y=480)
  106. boton3 = tk.Button(text="PI", width=ancho, height=alto, command=lambda:clic(pi))
  107. boton3.place(x=197, y=480)
  108. botonSuma = tk.Button(text="=", bg="light blue",width=ancho, height=alto, command=hacer_cuenta)
  109. botonSuma.place(x=287, y=480)
  110.  
  111. pantalla = tk.Entry(
  112.                 font=['arial',20, 'bold'],
  113.                 width=22,
  114.                 textvariable = resultado,
  115.                 justify ="right",
  116.                 bd=20,
  117.                 bg="powder blue",
  118.                 state=tk.DISABLED
  119.                 )
  120. pantalla.place(x=10, y=60)
  121.  
  122.  
  123. ventana.mainloop()
  124.  
Add Comment
Please, Sign In to add comment