teslariu

calcu ACME

Jun 30th, 2023
1,001
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.53 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. """
  5. Como hacer un portable:
  6. 1) Desde la línea de comandos de Windows o la shell de *nix instalan pyinstaller
  7. python -m pip install pyinstaller
  8. 2) Navegan hasta la carpeta donde tienen el script y ejecutan desde la línea de órdenes
  9. pyinstaller --onefile --noconsole <nombre_del_script>
  10. """
  11. # Calculadora gráfica
  12.  
  13. import tkinter as tk
  14. from math import pi, factorial, sqrt
  15.  
  16.  
  17. def clic(tecla):
  18.     global calculo_consola
  19.     calculo_consola += tecla
  20.     calculo.set(calculo_consola)
  21.    
  22. def limpiar_pantalla():
  23.     global calculo_consola
  24.     calculo.set("0")
  25.     calculo_consola = ""    
  26.  
  27.  
  28. def hacer_cuenta():
  29.     global calculo_consola
  30.     try:
  31.         resultado = eval(calculo_consola)
  32.     except Exception:
  33.         limpiar_pantalla()
  34.         resultado = "ERROR"
  35.     calculo.set(str(resultado))
  36.  
  37. def borrar_caracter():
  38.     global calculo_consola
  39.     calculo_consola = calculo_consola[:-1]
  40.     if calculo_consola == "":
  41.         limpiar_pantalla()
  42.     else:
  43.         calculo.set(calculo_consola)
  44.  
  45.  
  46. # Variable del lado consola que almacena la cuenta a realizar
  47. calculo_consola = ""
  48.  
  49. # ventana de la calculadora
  50. calc = tk.Tk()
  51. calc.config(width=390, height=600, bg="Light Steel Blue")
  52. calc.resizable(False,False)
  53. calc.title("Calculadora ACME")
  54.  
  55. # Creo la variable para mostrar en la pantalla
  56. calculo = tk.StringVar()
  57.  
  58. # inicializo la pantalla
  59. limpiar_pantalla()
  60.  
  61.  
  62. # pantalla de la calculadora
  63. pantalla = tk.Entry(
  64.                 font = ('arial',20,'bold'),
  65.                 width = 20,
  66.                 bd = 18,
  67.                 justify = 'right',
  68.                 state = tk.DISABLED,
  69.                 textvariable = calculo
  70.         )
  71. pantalla.place(x=20, y=50)
  72.  
  73. # teclas
  74. ancho = 7
  75. alto = 2
  76. relieve = 3
  77. letra = ('sans',11)
  78.  
  79.  
  80. # tecla borrado hacia atras
  81. tecla = tk.Button(text="DEL", width=ancho, height=alto, bd=relieve, font=letra, bg="medium aquamarine", command=borrar_caracter)
  82. tecla.place(x=287, y=140)
  83.  
  84.  
  85. # primera fila: 1 2 3 +
  86. tecla = tk.Button(text="1", width=ancho, height=alto, bd=relieve, font=letra, command=lambda:clic("1"))
  87. tecla.place(x=17, y=200)
  88. tecla = tk.Button(text="2", width=ancho, height=alto, bd=relieve, font=letra, command=lambda:clic("2"))
  89. tecla.place(x=107, y=200)
  90. tecla = tk.Button(text="3", width=ancho, height=alto, bd=relieve, font=letra, command=lambda:clic("3"))
  91. tecla.place(x=197, y=200)
  92. tecla = tk.Button(text="+", width=ancho, height=alto, bd=relieve, font=letra, bg='steel blue', command=lambda:clic("+"))
  93. tecla.place(x=287, y=200)
  94.  
  95. # segunda fila: 4 5 6 -
  96. tecla = tk.Button(text="4", width=ancho, height=alto, bd=relieve, font=letra, command=lambda:clic("4"))
  97. tecla.place(x=17, y=260)
  98. tecla = tk.Button(text="5", width=ancho, height=alto, bd=relieve, font=letra, command=lambda:clic("5"))
  99. tecla.place(x=107, y=260)
  100. tecla = tk.Button(text="6", width=ancho, height=alto, bd=relieve, font=letra, command=lambda:clic("6"))
  101. tecla.place(x=197, y=260)
  102. tecla = tk.Button(text="-", width=ancho, height=alto, bd=relieve, font=letra, bg='steel blue', command=lambda:clic("-"))
  103. tecla.place(x=287, y=260)
  104.  
  105. # tercera fila: 7 8 9 x
  106. tecla = tk.Button(text="7", width=ancho, height=alto, bd=relieve, font=letra, command=lambda:clic("7"))
  107. tecla.place(x=17, y=320)
  108. tecla = tk.Button(text="8", width=ancho, height=alto, bd=relieve, font=letra, command=lambda:clic("8"))
  109. tecla.place(x=107, y=320)
  110. tecla = tk.Button(text="9", width=ancho, height=alto, bd=relieve, font=letra, command=lambda:clic("9"))
  111. tecla.place(x=197, y=320)
  112. tecla = tk.Button(text="x", width=ancho, height=alto, bd=relieve, font=letra, bg='steel blue', command=lambda:clic("*"))
  113. tecla.place(x=287, y=320)
  114.  
  115. # cuarta fila: ( 0 ) /
  116. tecla = tk.Button(text="(", width=ancho, height=alto, bd=relieve, font=letra, bg="sky blue", command=lambda:clic("("))
  117. tecla.place(x=17, y=380)
  118. tecla = tk.Button(text="0", width=ancho, height=alto, bd=relieve, font=letra, command=lambda:clic("0"))
  119. tecla.place(x=107, y=380)
  120. tecla = tk.Button(text=")", width=ancho, height=alto, bd=relieve, font=letra, bg="sky blue", command=lambda:clic(")"))
  121. tecla.place(x=197, y=380)
  122. tecla = tk.Button(text="/", width=ancho, height=alto, bd=relieve, font=letra, bg='steel blue', command=lambda:clic("/"))
  123. tecla.place(x=287, y=380)
  124.  
  125. # quinta fila: raiz, punto decimal, potencia, resto
  126. tecla = tk.Button(text="\u221A", width=ancho, height=alto, bd=relieve, font=letra, bg="sky blue", command=lambda:clic("sqrt("))
  127. tecla.place(x=17, y=440)
  128. tecla = tk.Button(text=".", width=ancho, height=alto, bd=relieve, font=letra, command=lambda:clic("."))
  129. tecla.place(x=107, y=440)
  130. tecla = tk.Button(text="POW", width=ancho, height=alto, bd=relieve, font=letra, bg="sky blue", command=lambda:clic("**"))
  131. tecla.place(x=197, y=440)
  132. tecla = tk.Button(text="%", width=ancho, height=alto, bd=relieve, font=letra, bg='sky blue', command=lambda:clic("%"))
  133. tecla.place(x=287, y=440)
  134.  
  135.  
  136. # sexta fila: CL, factorial pi =
  137. tecla = tk.Button(text="CL", width=ancho, height=alto, bd=relieve, font=letra, bg="medium aquamarine", command=limpiar_pantalla)
  138. tecla.place(x=17, y=500)
  139. tecla = tk.Button(text="!", width=ancho, height=alto, bd=relieve, font=letra, command=lambda:clic("factorial("))
  140. tecla.place(x=107, y=500)
  141. tecla = tk.Button(text="\u03C0", width=ancho, height=alto, bd=relieve, font=letra, bg="sky blue", command=lambda:clic(str(pi)))
  142. tecla.place(x=197, y=500)
  143. tecla = tk.Button(text="=", width=ancho, height=alto, bd=relieve, font=letra, bg='medium aquamarine', command=hacer_cuenta)
  144. tecla.place(x=287, y=500)
  145.  
  146. tk.mainloop()
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.    
  154.  
Advertisement
Add Comment
Please, Sign In to add comment