Advertisement
teslariu

calculadoraACME

Feb 11th, 2023
750
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.04 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. """
  5. existen bibliotecas externas para aumentar la funcionalidad de python
  6. Se pueden instalar usando el comando pip
  7. Para instalar una biblio externa que cree un ejecutable (.exe) de la calculadora
  8. usaremos:
  9. python -m pip install pyinstaller
  10.  
  11. luego, para crear un ejecutable lanzar desde cmd este comando
  12. pyinstaller --noconsole --onefile calculadoraACME.py
  13.  
  14. """
  15.  
  16.  
  17. import tkinter as tk
  18. from math import sqrt, factorial, pi
  19.  
  20. # creo una variable para hacer el calculo del lado de la consola
  21. calculo_consola = ""
  22.  
  23. def clic(tecla):
  24.     global calculo_consola
  25.     calculo_consola = calculo_consola + tecla
  26.     calculo.set(calculo_consola)
  27.  
  28.  
  29. def limpieza():
  30.     global calculo_consola
  31.     calculo.set("0")  # pone un cero en la pantalla
  32.     calculo_consola = ""
  33.    
  34.  
  35. def hacer_cuenta():
  36.     global calculo_consola
  37.     try:
  38.         calculo_consola = str(eval(calculo_consola))
  39.     except Exception:
  40.         limpieza()
  41.         calculo.set("ERROR")
  42.     else:
  43.         calculo.set(calculo_consola)
  44.    
  45.  
  46. def borrar_caracter():
  47.     global calculo_consola
  48.     calculo_consola = calculo_consola[:-1]
  49.     calculo.set(calculo_consola)
  50.  
  51.  
  52.  
  53. ventana = tk.Tk()
  54. ventana.config(width=390, height=600, bg="Light Steel Blue")
  55. ventana.title("Calculadora ACME")
  56. ventana.resizable(0,0)
  57.  
  58. # Creo la variable calculo de tipo string gráfico para mostrar en pantalla
  59. calculo = tk.StringVar()
  60.  
  61. # dimensiones de las teclas
  62. ancho = 10
  63. alto = 2
  64.  
  65. # llamo a la funcion de limpieza para inicializar la calculadora
  66. limpieza()
  67.  
  68. ##### Creamos la pantalla
  69. pantalla = tk.Entry(
  70.                 font = ('arial',20,'bold'),
  71.                 width = 20,
  72.                 bd = 15,
  73.                 bg = "powder blue",
  74.                 justify = 'right',
  75.                 state = tk.DISABLED,
  76.                 textvariable = calculo
  77.             )
  78. pantalla.place(x=20, y=50)
  79.  
  80. # boton para borrar un caracter
  81. boton = tk.Button(text="DEL", width=ancho, height=alto, bg="medium aquamarine", command=borrar_caracter)
  82. boton.place(x=287, y=140)
  83.  
  84. ## Primera fila: 1 2 3 +
  85. boton = tk.Button(text="1", width=ancho, height=alto, command=lambda:clic("1"))
  86. boton.place(x=17, y=200)
  87. boton = tk.Button(text="2", width=ancho, height=alto, command=lambda:clic("2"))
  88. boton.place(x=107, y=200)
  89. boton = tk.Button(text="3", width=ancho, height=alto, command=lambda:clic("3"))
  90. boton.place(x=197, y=200)
  91. boton = tk.Button(text="+", width=ancho, height=alto, bg="Steel Blue", command=lambda:clic("+"))
  92. boton.place(x=287, y=200)
  93.  
  94.  
  95. ## Segunda fila: 4 5 6 -
  96. boton = tk.Button(text="4", width=ancho, height=alto, command=lambda:clic("4"))
  97. boton.place(x=17, y=260)
  98. boton = tk.Button(text="5", width=ancho, height=alto, command=lambda:clic("5"))
  99. boton.place(x=107, y=260)
  100. boton = tk.Button(text="6", width=ancho, height=alto, command=lambda:clic("6"))
  101. boton.place(x=197, y=260)
  102. boton = tk.Button(text="-", width=ancho, height=alto,  bg="Steel Blue", command=lambda:clic("-"))
  103. boton.place(x=287, y=260)
  104.  
  105.  
  106. ## Tercera fila: 7 8 9 x
  107. boton = tk.Button(text="7", width=ancho, height=alto, command=lambda:clic("7"))
  108. boton.place(x=17, y=320)
  109. boton = tk.Button(text="8", width=ancho, height=alto, command=lambda:clic("8"))
  110. boton.place(x=107, y=320)
  111. boton = tk.Button(text="9", width=ancho, height=alto, command=lambda:clic("9"))
  112. boton.place(x=197, y=320)
  113. boton = tk.Button(text="x", width=ancho, height=alto,  bg="SteelBlue", command=lambda:clic("*"))
  114. boton.place(x=287, y=320)
  115.  
  116.  
  117. ## Cuarta: ( 0 ) /
  118. boton = tk.Button(text="(", width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic("("))
  119. boton.place(x=17, y=380)
  120. boton = tk.Button(text="0", width=ancho, height=alto, command=lambda:clic("0"))
  121. boton.place(x=107, y=380)
  122. boton = tk.Button(text=")", width=ancho, height=alto, bg="SteelBlue2",command=lambda:clic(")"))
  123. boton.place(x=197, y=380)
  124. boton = tk.Button(text="/", width=ancho, height=alto,  bg="SteelBlue", command=lambda:clic("/"))
  125. boton.place(x=287, y=380)
  126.  
  127.  
  128. ## Quinta: Raiz Coma decimal potencia resto
  129. boton = tk.Button(text="\u221A", width=ancho, height=alto, bg="Sky blue", command=lambda:clic("sqrt("))
  130. boton.place(x=17, y=440)
  131. boton = tk.Button(text=".", width=ancho, height=alto, command=lambda:clic("."))
  132. boton.place(x=107, y=440)
  133. boton = tk.Button(text="POW", width=ancho, height=alto, bg="Sky Blue",command=lambda:clic("**"))
  134. boton.place(x=197, y=440)
  135. boton = tk.Button(text="%", width=ancho, height=alto,  bg="SteelBlue2", command=lambda:clic("%"))
  136. boton.place(x=287, y=440)
  137.  
  138.  
  139. ## Sexta: Clear factorial pi =
  140. boton = tk.Button(text="CL", width=ancho, height=alto, bg="medium aquamarine", command=limpieza)
  141. boton.place(x=17, y=500)
  142. boton = tk.Button(text="!", width=ancho, height=alto, command=lambda:clic("factorial("))
  143. boton.place(x=107, y=500)
  144. boton = tk.Button(text="\u03C0", width=ancho, height=alto, bg="sky blue",command=lambda:clic(str(pi)))
  145. boton.place(x=197, y=500)
  146. boton = tk.Button(text="=", width=ancho, height=alto,  bg="medium aquamarine", command=hacer_cuenta)
  147. boton.place(x=287, y=500)
  148.  
  149. ventana.mainloop()
  150.  
  151.  
  152.  
  153.    
  154.    
  155.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement