Advertisement
teslariu

calcu Final

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