Advertisement
teslariu

calculadora acme

Dec 2nd, 2022
618
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.23 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. ## python -m pip install pyinstaller
  7. ## pyinstaller --onefile --noconsole acme.py
  8.  
  9.  
  10.  
  11. import tkinter as tk
  12. from math import sqrt, factorial, pi
  13.  
  14. def limpieza():
  15.     global calculo_consola
  16.     calculo.set("0")        # se pone en cero la pantalla
  17.     calculo_consola = ""    # borro la cuenta de la consola
  18.    
  19.    
  20. def clic(tecla):
  21.     global calculo_consola
  22.     calculo_consola = calculo_consola + tecla
  23.     calculo.set(calculo_consola)
  24.    
  25.    
  26.  
  27. def hacer_cuenta():
  28.     global calculo_consola
  29.     try:
  30.         total = str(eval(calculo_consola))
  31.     except Exception:
  32.         limpieza()
  33.         total = "ERROR"
  34.     calculo.set(total)
  35.     calculo_consola = ""
  36.        
  37.  
  38.    
  39. def borrar_caracter():
  40.     global calculo_consola
  41.     if len(calculo_consola) > 1:
  42.         calculo_consola = calculo_consola[:-1]
  43.         calculo.set(calculo_consola)
  44.     else:
  45.         calculo_consola = ""
  46.         calculo.set("0")
  47.    
  48.  
  49.  
  50.  
  51. # ventana para contener la calculadora
  52. ventana = tk.Tk()
  53. ventana.title("Calculadora ACME")
  54. ventana.config(width=390, height=600, bg="Light Steel Blue")
  55. ventana.resizable(0,0)
  56.  
  57. # creo una variable string gràfica para mostrar el cálculo en la pantalla
  58. # de la calculadora
  59. calculo = tk.StringVar()
  60.  
  61. # llamo a una función que limpie la pantalla antes de empezar
  62. limpieza()
  63.  
  64. # creo la pantalla
  65. pantalla = tk.Entry(
  66.         font = ("arial",20,"bold"),
  67.         width = 20,
  68.         bd = 18,
  69.         bg = "powder blue",
  70.         justify = "right",
  71.         state = tk.DISABLED,
  72.         textvariable = calculo
  73.         )
  74. pantalla.place(x=20, y=50)
  75.  
  76. # defino las dimensiones de las teclas
  77. ancho = 10
  78. alto = 2
  79.  
  80. # creo una variable para almacenar el cálculo del lado de la consola
  81. calculo_consola = ""
  82.  
  83.  
  84. ######################   TECLAS  ##########################
  85.  
  86. # tecla para borrar el ultimo caracter ingresado
  87. tecla = tk.Button(text="DEL", width=ancho, height=alto, bg="medium aquamarine", command=borrar_caracter)
  88. tecla.place(x=287, y=140)
  89.  
  90.  
  91. ### Primera fila: 1 2 3 +  ######
  92. tecla = tk.Button(text="1", width=ancho, height=alto, command=lambda:clic("1"))
  93. tecla.place(x=17, y=200)
  94.  
  95. tecla = tk.Button(text="2", width=ancho, height=alto, command=lambda:clic("2"))
  96. tecla.place(x=107, y=200)
  97.  
  98. tecla = tk.Button(text="3", width=ancho, height=alto, command=lambda:clic("3"))
  99. tecla.place(x=197, y=200)
  100.  
  101. tecla = tk.Button(text="+", width=ancho, height=alto, bg="Steel Blue", command=lambda:clic("+"))
  102. tecla.place(x=287, y=200)
  103.  
  104.  
  105.  
  106. ### Segunda fila: 4 5 6 -  ######
  107. tecla = tk.Button(text="4", width=ancho, height=alto, command=lambda:clic("4"))
  108. tecla.place(x=17, y=260)
  109.  
  110. tecla = tk.Button(text="5", width=ancho, height=alto, command=lambda:clic("5"))
  111. tecla.place(x=107, y=260)
  112.  
  113. tecla = tk.Button(text="6", width=ancho, height=alto, command=lambda:clic("6"))
  114. tecla.place(x=197, y=260)
  115.  
  116. tecla = tk.Button(text="-", width=ancho, height=alto, bg="Steel Blue", command=lambda:clic("-"))
  117. tecla.place(x=287, y=260)
  118.  
  119.  
  120. ### Tercera fila: 7 8 9 x  ######
  121. tecla = tk.Button(text="7", width=ancho, height=alto, command=lambda:clic("7"))
  122. tecla.place(x=17, y=320)
  123.  
  124. tecla = tk.Button(text="8", width=ancho, height=alto, command=lambda:clic("8"))
  125. tecla.place(x=107, y=320)
  126.  
  127. tecla = tk.Button(text="9", width=ancho, height=alto, command=lambda:clic("9"))
  128. tecla.place(x=197, y=320)
  129.  
  130. tecla = tk.Button(text="x", width=ancho, height=alto, bg="Steel Blue", command=lambda:clic("*"))
  131. tecla.place(x=287, y=320)
  132.  
  133.  
  134.  
  135. ### Cuarta fila: ( 0 ) /  ######
  136. tecla = tk.Button(text="(", width=ancho, height=alto, bg="Steel Blue2", command=lambda:clic("("))
  137. tecla.place(x=17, y=380)
  138.  
  139. tecla = tk.Button(text="0", width=ancho, height=alto, command=lambda:clic("0"))
  140. tecla.place(x=107, y=380)
  141.  
  142. tecla = tk.Button(text=")", width=ancho, height=alto, bg="Steel Blue2", command=lambda:clic(")"))
  143. tecla.place(x=197, y=380)
  144.  
  145. tecla = tk.Button(text="/", width=ancho, height=alto, bg="Steel Blue", command=lambda:clic("/"))
  146. tecla.place(x=287, y=380)
  147.  
  148.  
  149. ### Quinta fila: Raiz, coma decimal, potencia, resto  ######
  150. tecla = tk.Button(text="\u221A", width=ancho, height=alto, bg="Steel Blue3", command=lambda:clic("sqrt("))
  151. tecla.place(x=17, y=440)
  152.  
  153. tecla = tk.Button(text=".", width=ancho, height=alto, bg="Steel Blue2", command=lambda:clic("."))
  154. tecla.place(x=107, y=440)
  155.  
  156. tecla = tk.Button(text="POW", width=ancho, height=alto, bg="Steel Blue3", command=lambda:clic("**"))
  157. tecla.place(x=197, y=440)
  158.  
  159. tecla = tk.Button(text="%", width=ancho, height=alto, bg="Steel Blue3", command=lambda:clic("%"))
  160. tecla.place(x=287, y=440)
  161.  
  162.  
  163.  
  164. ### Sexta fila: Clear Factorial PI =  ######
  165. tecla = tk.Button(text="CL", width=ancho, height=alto, bg="medium aquamarine", command=limpieza)
  166. tecla.place(x=17, y=500)
  167.  
  168. tecla = tk.Button(text="!", width=ancho, height=alto, bg="Steel Blue3", command=lambda:clic("factorial("))
  169. tecla.place(x=107, y=500)
  170.  
  171. tecla = tk.Button(text="\u03c0", width=ancho, height=alto, bg="Steel Blue3", command=lambda:clic(str(pi)))
  172. tecla.place(x=197, y=500)
  173.  
  174. tecla = tk.Button(text="=", width=ancho, height=alto, bg="medium aquamarine", command=hacer_cuenta)
  175. tecla.place(x=287, y=500)
  176.  
  177.  
  178.  
  179.  
  180.  
  181. ventana.mainloop()
  182.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement