Advertisement
teslariu

cal

Oct 12th, 2021
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.38 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. # Script que implementa una calculadora científica básica
  5.  
  6. import tkinter as tk
  7. from math import sqrt, factorial, pi
  8.  
  9.  
  10. def clic(tecla):
  11.     global calculo
  12.     calculo = calculo + str(tecla)
  13.     texto_ingresado.set(calculo)
  14.    
  15.    
  16. def limpieza():
  17.     global calculo
  18.     calculo = ""
  19.     texto_ingresado.set("0")
  20.    
  21. def calcular():
  22.     global calculo
  23.     try:
  24.         total = str(eval(calculo))
  25.     except Exception:
  26.         total = "ERROR"
  27.     texto_ingresado.set(total)
  28.  
  29.  
  30.  
  31. # creo la ventana de la calculadora
  32. ventana = tk.Tk()
  33. ventana.title("CALCULADORA")
  34. ventana.config(width = 390, height = 600, bg = "Light Steel Blue")
  35. ventana.resizable(0,0)
  36.  
  37. # variable que almacena el contenido de la pantalla
  38. texto_ingresado = tk.StringVar()
  39.  
  40. # creo dos variables para almacenar el ancho y alto de los botones
  41. ancho = 10
  42. alto = 2
  43.  
  44. # creo una variable para almacenar el cálculo con la secuencia
  45. # de tecleo
  46. calculo = ""
  47.  
  48. limpieza()
  49.  
  50.  
  51.  
  52.  
  53. ########## creamos los botones de la calculadora  ####################
  54.  
  55. # fila 1: 1,2,3,+
  56. boton1 = tk.Button(text="1", width=ancho, height=alto, command=lambda:clic(1))
  57. boton1.place(x=17, y=180)
  58. boton2 = tk.Button(text="2", width=ancho, height=alto, command=lambda:clic(2))
  59. boton2.place(x=107, y=180)
  60. boton3 = tk.Button(text="3", width=ancho, height=alto, command=lambda:clic(3))
  61. boton3.place(x=197, y=180)
  62. boton_suma = tk.Button(text="+", width=ancho, height=alto, bg="pale green", command=lambda:clic('+'))
  63. boton_suma.place(x=287, y=180)
  64.  
  65. # fila 2: 1,2,3,+
  66. boton4 = tk.Button(text="4", width=ancho, height=alto, command=lambda:clic(4))
  67. boton4.place(x=17, y=240)
  68. boton5 = tk.Button(text="5", width=ancho, height=alto, command=lambda:clic(5))
  69. boton5.place(x=107, y=240)
  70. boton6 = tk.Button(text="6", width=ancho, height=alto, command=lambda:clic(6))
  71. boton6.place(x=197, y=240)
  72. boton_menos = tk.Button(text="-", width=ancho, height=alto, bg="pale green", command=lambda:clic('-'))
  73. boton_menos.place(x=287, y=240)
  74.  
  75. # fila 3: 7,8,9,x
  76. boton7 = tk.Button(text="7", width=ancho, height=alto, command=lambda:clic(7))
  77. boton7.place(x=17, y=300)
  78. boton8 = tk.Button(text="8", width=ancho, height=alto, command=lambda:clic(8))
  79. boton8.place(x=107, y=300)
  80. boton9 = tk.Button(text="9", width=ancho, height=alto, command=lambda:clic(9))
  81. boton9.place(x=197, y=300)
  82. boton_por = tk.Button(text="x", width=ancho, height=alto, bg="pale green", command=lambda:clic('*'))
  83. boton_por.place(x=287, y=300)
  84.  
  85. # fila 4: (,0,),/
  86. boton_par_izq = tk.Button(text="(", width=ancho, height=alto, bg="pale green", command=lambda:clic('('))
  87. boton_par_izq.place(x=17, y=360)
  88. boton0 = tk.Button(text="0", width=ancho, height=alto, command=lambda:clic(0))
  89. boton0.place(x=107, y=360)
  90. boton_par_der = tk.Button(text=")", width=ancho, height=alto, bg="pale green", command=lambda:clic(')'))
  91. boton_par_der.place(x=197, y=360)
  92. boton_cociente = tk.Button(text="/", width=ancho, height=alto, bg="pale green", command=lambda:clic('/'))
  93. boton_cociente.place(x=287, y=360)
  94.  
  95. # fila 5: raiz,coma decimal,potencia,resto
  96. boton_raiz = tk.Button(text="RAIZ", width=ancho, height=alto, bg="pale green",command=lambda:clic('sqrt('))
  97. boton_raiz.place(x=17, y=420)
  98. boton_punto = tk.Button(text=".", width=ancho, height=alto, command=lambda:clic('.'))
  99. boton_punto.place(x=107, y=420)
  100. boton_power = tk.Button(text="POW", width=ancho, height=alto, bg="pale green",command=lambda:clic('**'))
  101. boton_power.place(x=197, y=420)
  102. boton_resto = tk.Button(text="%", width=ancho, height=alto, bg="pale green", command=lambda:clic('%'))
  103. boton_resto.place(x=287, y=420)
  104.  
  105. # fila 6: Clear, Factorial, pi, igual
  106. boton_clear = tk.Button(text="CLEAR", width=ancho, height=alto, bg="SkyBlue3", command=limpieza)
  107. boton_clear.place(x=17, y=480)
  108. boton_fact = tk.Button(text="!", width=ancho, height=alto, bg="pale green", command=lambda:clic('factorial('))
  109. boton_fact.place(x=107, y=480)
  110. boton_pi = tk.Button(text="PI", width=ancho, height=alto, bg="pale green", command=lambda:clic('pi'))
  111. boton_pi.place(x=197, y=480)
  112. boton_igual = tk.Button(text="=", width=ancho, height=alto, bg="SkyBlue3", command=calcular)
  113. boton_igual.place(x=287, y=480)
  114.  
  115.  
  116. # creamos la pantalla
  117. pantalla = tk.Entry(
  118.             width = 22,
  119.             font = ('arial',20,'bold'),
  120.             bd = 18,
  121.             bg = 'powder blue',
  122.             justify = 'right',
  123.             textvariable = texto_ingresado,
  124.             state = tk.DISABLED
  125.             )
  126. pantalla.place(x=10, y=60)
  127.  
  128.  
  129.  
  130.  
  131.  
  132. ventana.mainloop()
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement