teslariu

calculadora

Jul 26th, 2022
1,422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.99 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4.  
  5. import tkinter as tk
  6. from math import sqrt, factorial, pi
  7.  
  8. cuenta = "" # similar a calculo = tk.StringVar() del lado gráfico
  9.  
  10. ####### funciones  #######################
  11. def clic(tecla):
  12.     global cuenta
  13.     cuenta = cuenta + tecla
  14.     calculo.set(cuenta)
  15.    
  16. def borrar_caracter():
  17.     global cuenta
  18.     cuenta = cuenta[:-1]
  19.     if cuenta:
  20.         calculo.set(cuenta)
  21.     else:
  22.         calculo.set("0")
  23.        
  24. def limpieza():
  25.     global cuenta
  26.     cuenta = ""
  27.     calculo.set("0")
  28.  
  29.    
  30. def hacer_cuenta():
  31.     global cuenta
  32.     try:
  33.         total = str(eval(cuenta))
  34.     except Exception:
  35.         limpieza()
  36.         total = "ERROR"
  37.     calculo.set(total)
  38.    
  39.  
  40.  
  41. # Ventana para contener la calculadora
  42. ventana = tk.Tk()
  43. ventana.title("Calculadora ACME")
  44. ventana.config(width=390, height=600, bg="Light Steel Blue")
  45. ventana.resizable(0,0)
  46.  
  47. # Creo una variable para almacenar el cálculo a mostrar en la pantalla
  48. calculo = tk.StringVar() # similar a cuenta = "" del lado consola
  49.  
  50. limpieza()
  51.  
  52. # creamos la pantalla
  53. pantalla = tk.Entry(
  54.                 font=["arial",20,"bold"],
  55.                 width=21,         # ancho
  56.                 bd=20,            # grosor del borde
  57.                 bg="powder blue",  # color de fondo
  58.                 justify="right",      # alineación del texto
  59.                 state=tk.DISABLED,    # la hago solo lectura
  60.                 textvariable=calculo  # variable a mostrar
  61.             )
  62. pantalla.place(x=16, y=50)
  63.  
  64. # defino las dimensiones de las teclas
  65. ancho = 9
  66. alto = 2
  67.  
  68. #################   TECLAS  ################################
  69. # tecla para borrar
  70. boton = tk.Button(text="\u2190", width=ancho, height=alto, bg="SkyBlue", command=borrar_caracter)
  71. boton.place(x=288, y=140)
  72.  
  73.  
  74. ## Primera fila 1 2 3 +
  75. boton = tk.Button(text="\uFF11", width=ancho, height=alto, command=lambda:clic("1"))
  76. boton.place(x=18, y=200)
  77. boton = tk.Button(text="2", width=ancho, height=alto, command=lambda:clic("2"))
  78. boton.place(x=108, y=200)
  79. boton = tk.Button(text="3", width=ancho, height=alto, command=lambda:clic("3"))
  80. boton.place(x=198, y=200)
  81. boton = tk.Button(text="+", width=ancho, height=alto, bg="SteelBlue", command=lambda:clic("+"))
  82. boton.place(x=288, y=200)
  83.  
  84. ## Segunda fila 4 5 6 -
  85. boton = tk.Button(text="4", width=ancho, height=alto, command=lambda:clic("4"))
  86. boton.place(x=18, y=260)
  87. boton = tk.Button(text="5", width=ancho, height=alto, command=lambda:clic("5"))
  88. boton.place(x=108, y=260)
  89. boton = tk.Button(text="6", width=ancho, height=alto, command=lambda:clic("6"))
  90. boton.place(x=198, y=260)
  91. boton = tk.Button(text="-", width=ancho, height=alto, bg="SteelBlue", command=lambda:clic("-"))
  92. boton.place(x=288, y=260)
  93.  
  94. ## Tercera fila 7 8 9 x
  95. boton = tk.Button(text="7", width=ancho, height=alto, command=lambda:clic("7"))
  96. boton.place(x=18, y=320)
  97. boton = tk.Button(text="8", width=ancho, height=alto, command=lambda:clic("8"))
  98. boton.place(x=108, y=320)
  99. boton = tk.Button(text="9", width=ancho, height=alto, command=lambda:clic("9"))
  100. boton.place(x=198, y=320)
  101. boton = tk.Button(text="x", width=ancho, height=alto, bg="SteelBlue", command=lambda:clic("*"))
  102. boton.place(x=288, y=320)
  103.  
  104. ## Cuarta fila ( 0 ) /
  105. boton = tk.Button(text="(", width=ancho, height=alto, bg="SkyBlue", command=lambda:clic("("))
  106. boton.place(x=18, y=380)
  107. boton = tk.Button(text="0", width=ancho, height=alto, command=lambda:clic("0"))
  108. boton.place(x=108, y=380)
  109. boton = tk.Button(text=")", width=ancho, height=alto, bg="SkyBlue", command=lambda:clic(")"))
  110. boton.place(x=198, y=380)
  111. boton = tk.Button(text="/", width=ancho, height=alto, bg="SteelBlue", command=lambda:clic("/"))
  112. boton.place(x=288, y=380)
  113.  
  114. ## Quinta fila Raiz Coma Potencia Modulo
  115. boton = tk.Button(text="\u221A", width=ancho, height=alto, bg="SkyBlue", command=lambda:clic("sqrt("))
  116. boton.place(x=18, y=440)
  117. boton = tk.Button(text=".", width=ancho, height=alto, bg="SkyBlue", command=lambda:clic("."))
  118. boton.place(x=108, y=440)
  119. boton = tk.Button(text="POW", width=ancho, height=alto, bg="SkyBlue", command=lambda:clic("**"))
  120. boton.place(x=198, y=440)
  121. boton = tk.Button(text="%", width=ancho, height=alto, bg="SkyBlue", command=lambda:clic("%"))
  122. boton.place(x=288, y=440)
  123.  
  124. ## Sexta fila Clear factorial pi =
  125. boton = tk.Button(text="CL", width=ancho, height=alto, bg="SkyBlue", command=limpieza)
  126. boton.place(x=18, y=500)
  127. boton = tk.Button(text="!", width=ancho, height=alto, bg="SkyBlue", command=lambda:clic("factorial("))
  128. boton.place(x=108, y=500)
  129. boton = tk.Button(text="\u03c0", width=ancho, height=alto, bg="SkyBlue", command=lambda:clic(str(pi)))
  130. boton.place(x=198, y=500)
  131. boton = tk.Button(text="=", width=ancho, height=alto, bg="medium aquamarine", command=hacer_cuenta)
  132. boton.place(x=288, y=500)
  133.  
  134. ventana.mainloop()
  135.  
  136. """
  137. BONUS TRACK
  138. Para hacer una carpeta dist con un ejecutable adentro (.exe)
  139. 1) Descargar pyinstaller
  140. python -m pip pyinstaller
  141.  
  142. 2) Crear el ejecutable sin consola y en un solo archivo
  143. pyinstaller --noconsole --onefile calc.py
  144.  
  145. """
Advertisement
Add Comment
Please, Sign In to add comment