Advertisement
teslariu

cal ACME

Jul 13th, 2023
1,089
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.78 KB | None | 0 0
  1. #!/usr/b#in/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. """
  5. PARA HACER PORTABLE
  6. 1) desde cmd de windows:
  7. pip install pyinstaller
  8.  
  9. 2) pyinstaller --onefile --noconsole <nombre del script>
  10.  
  11. """
  12.  
  13.  
  14. import tkinter as tk
  15. from math import sqrt, pi, factorial
  16.  
  17.  
  18. def clic(tecla):
  19.     global calculo_consola
  20.     calculo_consola = calculo_consola + tecla
  21.     calculo.set(calculo_consola)
  22.    
  23. def limpieza():
  24.     global calculo_consola
  25.     calculo_consola = ""
  26.     calculo.set("0")
  27.    
  28. def hacer_cuenta():
  29.     global calculo_consola
  30.    
  31.     try:
  32.         resultado = str(eval(calculo_consola))
  33.     except Exception:
  34.         resultado = "ERROR"
  35.         limpieza()
  36.     calculo.set(resultado)
  37.    
  38. def borrar_caracter():
  39.     global calculo_consola
  40.     calculo_consola = calculo_consola[:-1]
  41.     calculo.set(calculo_consola)
  42.    
  43.    
  44.  
  45. # creo una variable (del lado consola) para calcular la cuenta en
  46. # la pantalla
  47. calculo_consola = ""
  48.  
  49.  
  50. # creo el diseño de la calculadora
  51. calculadora = tk.Tk()
  52. calculadora.config(width=390, height=600, bg="Light Steel Blue")
  53. calculadora.title("Calculadora ACME")
  54. calculadora.resizable(False,False)
  55. # icono = tk.PhotoImage(file='icono.png')
  56. # calculadora.iconphoto(True, icono)
  57.  
  58. # creo una variable (del lado gráfico) para mostrar la cuenta en
  59. # la pantalla
  60. calculo = tk.StringVar()
  61.  
  62. # llamo a limpieza antes de crear la pantalla
  63. limpieza()
  64.  
  65. # diseño de la pantalla
  66. pantalla = tk.Entry(
  67.     width = 20,
  68.     bd = 18,
  69.     font = ('arial',20, 'bold'),
  70.     justify = 'right',
  71.     state = tk.DISABLED,
  72.     textvariable = calculo
  73. )
  74. pantalla.place(x=20, y=50)
  75.  
  76. # diseño de las teclas
  77. ancho = 9
  78. alto = 2
  79.  
  80. # fila cero
  81. tecla = tk.Button(text="<<", width=ancho, height=alto, fg="white", bg="Dark Slate grey", 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, command=lambda:clic("1"))
  87. tecla.place(x=17, y=200)
  88. tecla = tk.Button(text="2", width=ancho, height=alto, command=lambda:clic("2"))
  89. tecla.place(x=107, y=200)
  90. tecla = tk.Button(text="3", width=ancho, height=alto, command=lambda:clic("3"))
  91. tecla.place(x=197, y=200)
  92. tecla = tk.Button(text="+", width=ancho, height=alto, bg="cadet Blue", command=lambda:clic("+"))
  93. tecla.place(x=287, y=200)
  94.  
  95.  
  96. # segunda fila: 4 5 6 -
  97. tecla = tk.Button(text="4", width=ancho, height=alto, command=lambda:clic("4"))
  98. tecla.place(x=17, y=260)
  99. tecla = tk.Button(text="5", width=ancho, height=alto, command=lambda:clic("5"))
  100. tecla.place(x=107, y=260)
  101. tecla = tk.Button(text="6", width=ancho, height=alto, command=lambda:clic("6"))
  102. tecla.place(x=197, y=260)
  103. tecla = tk.Button(text="-", width=ancho, height=alto, bg="cadet Blue", command=lambda:clic("-"))
  104. tecla.place(x=287, y=260)
  105.  
  106.  
  107. # tercera fila: 7 8 9 x
  108. tecla = tk.Button(text="7", width=ancho, height=alto, command=lambda:clic("7"))
  109. tecla.place(x=17, y=320)
  110. tecla = tk.Button(text="8", width=ancho, height=alto, command=lambda:clic("8"))
  111. tecla.place(x=107, y=320)
  112. tecla = tk.Button(text="9", width=ancho, height=alto, command=lambda:clic("9"))
  113. tecla.place(x=197, y=320)
  114. tecla = tk.Button(text="x", width=ancho, height=alto, bg="cadet Blue", command=lambda:clic("*"))
  115. tecla.place(x=287, y=320)
  116.  
  117.  
  118. # cuarta fila: ( 0 ) /
  119. tecla = tk.Button(text="(", width=ancho, height=alto, bg="dodger Blue", command=lambda:clic("("))
  120. tecla.place(x=17, y=380)
  121. tecla = tk.Button(text="0", width=ancho, height=alto, command=lambda:clic("0"))
  122. tecla.place(x=107, y=380)
  123. tecla = tk.Button(text=")", width=ancho, height=alto, bg="dodger Blue", command=lambda:clic(")"))
  124. tecla.place(x=197, y=380)
  125. tecla = tk.Button(text="/", width=ancho, height=alto, bg="cadet Blue", command=lambda:clic("/"))
  126. tecla.place(x=287, y=380)
  127.  
  128.  
  129. # quinta fila: raiz cuadrada, punto decimal, potencia, resto
  130. tecla = tk.Button(text="\u221A", width=ancho, height=alto, bg="dodger Blue", command=lambda:clic("sqrt("))
  131. tecla.place(x=17, y=440)
  132. tecla = tk.Button(text=".", width=ancho, height=alto, bg='dodger blue', command=lambda:clic("."))
  133. tecla.place(x=107, y=440)
  134. tecla = tk.Button(text="^", width=ancho, height=alto, bg="dodger Blue", command=lambda:clic("**"))
  135. tecla.place(x=197, y=440)
  136. tecla = tk.Button(text="%", width=ancho, height=alto, bg="dodger Blue", command=lambda:clic("%"))
  137. tecla.place(x=287, y=440)
  138.  
  139.  
  140. # sexta fila: clear, factorial, pi, =
  141. tecla = tk.Button(text="CL", width=ancho, height=alto, fg="white", bg="Dark Slate grey", command=limpieza)
  142. tecla.place(x=17, y=500)
  143. tecla = tk.Button(text="!", width=ancho, height=alto, bg='dodger blue', command=lambda:clic("factorial("))
  144. tecla.place(x=107, y=500)
  145. tecla = tk.Button(text="\u03C0", width=ancho, height=alto, bg="dodger Blue", command=lambda:clic(str(pi)))
  146. tecla.place(x=197, y=500)
  147. tecla = tk.Button(text="=", width=ancho, height=alto, fg="white", bg="Dark Slate grey", command=hacer_cuenta)
  148. tecla.place(x=287, y=500)
  149.  
  150.  
  151. calculadora.mainloop()
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement