Advertisement
teslariu

calcu

Sep 13th, 2022
869
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.42 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. import tkinter as tk
  5. from math import sqrt, pi, factorial
  6.  
  7. ##### lado "consola"  ###################
  8. def clic(tecla):
  9.     global calculo_consola
  10.     calculo_consola = calculo_consola + tecla
  11.     calculo.set(calculo_consola)
  12.  
  13.  
  14.  
  15. def limpieza():
  16.     global calculo_consola
  17.     calculo_consola = ""
  18.     calculo.set("0")
  19.  
  20.  
  21.    
  22.    
  23. def hacer_calculo():
  24.     global calculo_consola
  25.     try:
  26.         calculo_consola = str(eval(calculo_consola))
  27.         calculo.set(calculo_consola)
  28.     except Exception:
  29.         calculo_consola = ""
  30.         calculo.set("ERROR")
  31.  
  32.  
  33.    
  34. def borrar_caracter():
  35.     global calculo_consola
  36.     calculo_consola = calculo_consola[:-1]
  37.     if calculo_consola:
  38.         calculo.set(calculo_consola)
  39.     else:
  40.         calculo.set("0")
  41.        
  42.  
  43.  
  44.  
  45.    
  46.  
  47. # creo una variable del tipo string para almacenar el contenido a mostrar
  48. # en la pantalla
  49. calculo_consola = ""
  50.  
  51.  
  52. #### lado "grafico" ######################
  53. calculadora = tk.Tk()
  54. calculadora.title("Calculadora ACME")
  55. calculadora.config(width=370, height=600, bg="Light Steel Blue")
  56. calculadora.resizable(0,0)
  57.  
  58. # creo una variable del tipo StringVar para almacenar el contenido a mostrar
  59. # en la pantalla
  60. calculo = tk.StringVar()
  61.  
  62. limpieza()
  63.  
  64.  
  65. # defino el ancho y alto de las teclas:
  66. ancho = 10
  67. alto = 2
  68.  
  69.  
  70. # defino la pantalla
  71. pantalla = tk.Entry(
  72.             font = ["arial",20,"bold"],
  73.             width = 21,
  74.             bd = 15,    # grosor del borde
  75.             bg = "powder blue",
  76.             justify = "right",
  77.             state = tk.DISABLED,    # solo lectura
  78.             textvariable = calculo     # variable cuyo contenido se muestra en pantalla
  79.             )
  80. pantalla.place(x=10,y=50)
  81.  
  82.  
  83. ########  teclas
  84.  
  85. # tecla para borrar el ultimo caracter
  86. tecla = tk.Button(text="\u22B3", width=ancho, height=alto, command=borrar_caracter)
  87. tecla.place(x=280, y=140)
  88.  
  89.  
  90. # Primera fila 1 2 3 +
  91. tecla = tk.Button(text="1", width=ancho, height=alto, command=lambda:clic("1"))
  92. tecla.place(x=10, y=200)
  93. tecla = tk.Button(text="2", width=ancho, height=alto, command=lambda:clic("2"))
  94. tecla.place(x=100, y=200)
  95. tecla = tk.Button(text="3", width=ancho, height=alto, command=lambda:clic("3"))
  96. tecla.place(x=190, y=200)
  97. tecla = tk.Button(text="+", width=ancho, height=alto, bg="dodger blue", command=lambda:clic("+"))
  98. tecla.place(x=280, y=200)
  99.  
  100. # Segunda fila 4 5 6 -
  101. tecla = tk.Button(text="4", width=ancho, height=alto, command=lambda:clic("4"))
  102. tecla.place(x=10, y=260)
  103. tecla = tk.Button(text="5", width=ancho, height=alto, command=lambda:clic("5"))
  104. tecla.place(x=100, y=260)
  105. tecla = tk.Button(text="6", width=ancho, height=alto, command=lambda:clic("6"))
  106. tecla.place(x=190, y=260)
  107. tecla = tk.Button(text="-", width=ancho, height=alto, bg="dodger blue", command=lambda:clic("-"))
  108. tecla.place(x=280, y=260)
  109.  
  110. # Tercera fila 7 8 9 x
  111. tecla = tk.Button(text="7", width=ancho, height=alto, command=lambda:clic("7"))
  112. tecla.place(x=10, y=320)
  113. tecla = tk.Button(text="8", width=ancho, height=alto, command=lambda:clic("8"))
  114. tecla.place(x=100, y=320)
  115. tecla = tk.Button(text="9", width=ancho, height=alto, command=lambda:clic("9"))
  116. tecla.place(x=190, y=320)
  117. tecla = tk.Button(text="x", width=ancho, height=alto, bg="dodger blue", command=lambda:clic("*"))
  118. tecla.place(x=280, y=320)
  119.  
  120. # Cuarta fila ( 0 ) /
  121. tecla = tk.Button(text="(", width=ancho, height=alto, bg="Light blue", command=lambda:clic("("))
  122. tecla.place(x=10, y=380)
  123. tecla = tk.Button(text="0", width=ancho, height=alto, command=lambda:clic("0"))
  124. tecla.place(x=100, y=380)
  125. tecla = tk.Button(text=")", width=ancho, height=alto, bg="Light blue", command=lambda:clic(")"))
  126. tecla.place(x=190, y=380)
  127. tecla = tk.Button(text="/", width=ancho, height=alto, bg="dodger blue", command=lambda:clic("/"))
  128. tecla.place(x=280, y=380)
  129.  
  130.  
  131. # Quinta fila Raiz Coma decimal Potencia Modulo
  132. tecla = tk.Button(text="\u221A", width=ancho, height=alto, bg="Skyblue3", command=lambda:clic("sqrt("))
  133. tecla.place(x=10, y=440)
  134. tecla = tk.Button(text=".", width=ancho, height=alto, command=lambda:clic("."))
  135. tecla.place(x=100, y=440)
  136. tecla = tk.Button(text="POW", width=ancho, height=alto, bg="Skyblue3",command=lambda:clic("**"))
  137. tecla.place(x=190, y=440)
  138. tecla = tk.Button(text="%", width=ancho, height=alto, bg="dodger blue", command=lambda:clic("%"))
  139. tecla.place(x=280, y=440)
  140.  
  141. # Sexta fila Clear Factorial PI =
  142. tecla = tk.Button(text="CL", width=ancho, height=alto, bg="Light blue", command=limpieza)
  143. tecla.place(x=10, y=500)
  144. tecla = tk.Button(text="!", width=ancho, height=alto, bg="Skyblue3", command=lambda:clic("factorial("))
  145. tecla.place(x=100, y=500)
  146. tecla = tk.Button(text="\u03C0", width=ancho, height=alto, bg="Light blue", command=lambda:clic(str(pi)))
  147. tecla.place(x=190, y=500)
  148. tecla = tk.Button(text="=", width=ancho, height=alto, bg="dodger blue", command=hacer_calculo)
  149. tecla.place(x=280, y=500)
  150.  
  151.  
  152. calculadora.mainloop()
  153.  
  154. """
  155. Existen bibliotecas de terceros en el sitio web pypi.org. Se pueden descargar
  156. con la utilidad pip del intèrprete de python. Para crear un ejecutable
  157. usaremos la biblioteca de terceros pyinstaller
  158.  
  159. 1) Desde el prompt de Windows/ shel  de Linux, etc, ejecutar
  160. python -m pip install pyinstaller (o simplemente pip install pyinstaller)
  161.  
  162. 2) Navegar hasta la carpeta del script y ejecutar
  163. pyinstaller --noconsole --onefile calcu.py
  164.  
  165. (Esto genera dos carpetas, build y dist, el ejecutable está en la carpeta dist)
  166.  
  167. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement