teslariu

calc Tkinter

May 27th, 2023
988
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.86 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. """
  5. como hacer un portable
  6. 1) instalar una libreria externa desde cmd
  7. python -m pip install pyinstaller
  8.  
  9. 2) navegan hasta la carpeta donde tienen el script y ejecutan DESDE CMD
  10. pyinstaller --onefile --noconsole <nombre del script>
  11. """
  12.  
  13.  
  14.  
  15. import tkinter as tk
  16. from math import sqrt, factorial, pi
  17.  
  18. def limpiar_pantalla():
  19.     global calculo_consola
  20.     calculo_consola = ""
  21.     calculo.set("0")
  22.  
  23.  
  24. def clic(tecla):
  25.     global calculo_consola
  26.     calculo_consola = calculo_consola + tecla
  27.     calculo.set(calculo_consola)
  28.    
  29.  
  30. def hacer_cuenta():
  31.     global calculo_consola
  32.     try:
  33.         resultado = str(eval(calculo_consola))
  34.     except Exception:
  35.         limpiar_pantalla()
  36.         resultado = "ERROR"
  37.     calculo.set(resultado)
  38.    
  39.  
  40. def borrar_caracter():
  41.     global calculo_consola
  42.     calculo_consola = calculo_consola[:-1]
  43.     calculo.set(calculo_consola)
  44.    
  45.  
  46. # creo la variable para MOSTRAR la cuenta EN LA CONSOLA
  47. calculo_consola = ""
  48.  
  49.  
  50.  
  51.  
  52. # ventana de la calculadora
  53. calc = tk.Tk()
  54. calc.config(width=390, height=600, bg="Light Steel Blue")
  55. calc.title("Calculadora ACME")
  56. calc.resizable(False,False)
  57.  
  58. # creo la variable para MOSTRAR la cuenta EN LA PANTALLA
  59. calculo = tk.StringVar()
  60.  
  61.  
  62. # llamo a la función de limpieza para inicializar la calculadora
  63. limpiar_pantalla()
  64.  
  65. # creo la pantalla
  66. pantalla = tk.Entry(
  67.     font = ('arial',20,'bold'),
  68.     width = 20,
  69.     bd = 18,  # grosor del relieve del borde
  70.     justify = "right",
  71.     state = tk.DISABLED,  # impido escribir adentro de la caja de texto
  72.     textvariable = calculo
  73. )
  74. pantalla.place(x=20, y=50)
  75.  
  76. # creo las dimensiones de las teclas
  77. ancho = 9
  78. alto = 2
  79.  
  80. boton = tk.Button(text="DEL", width=ancho, height=alto, bg="medium aquamarine", command=borrar_caracter)
  81. boton.place(x=287, y=140)
  82.  
  83.  
  84. ## Teclas
  85. # primera fila: 1 2 3 +
  86. boton = tk.Button(text="1", width=ancho, height=alto, command=lambda:clic("1"))
  87. boton.place(x=17, y=200)
  88. boton = tk.Button(text="2", width=ancho, height=alto, command=lambda:clic("2"))
  89. boton.place(x=107, y=200)
  90. boton = tk.Button(text="3", width=ancho, height=alto, command=lambda:clic("3"))
  91. boton.place(x=197, y=200)
  92. boton = tk.Button(text="+", width=ancho, height=alto, bg="Steel Blue", command=lambda:clic("+"))
  93. boton.place(x=287, y=200)
  94.  
  95. # segunda fila: 4 5 6 -
  96. boton = tk.Button(text="4", width=ancho, height=alto, command=lambda:clic("4"))
  97. boton.place(x=17, y=260)
  98. boton = tk.Button(text="5", width=ancho, height=alto, command=lambda:clic("5"))
  99. boton.place(x=107, y=260)
  100. boton = tk.Button(text="6", width=ancho, height=alto, command=lambda:clic("6"))
  101. boton.place(x=197, y=260)
  102. boton = tk.Button(text="-", width=ancho, height=alto, bg="Steel Blue", command=lambda:clic("-"))
  103. boton.place(x=287, y=260)
  104.  
  105. # tercera fila: 7 8 9 x
  106. boton = tk.Button(text="7", width=ancho, height=alto, command=lambda:clic("7"))
  107. boton.place(x=17, y=320)
  108. boton = tk.Button(text="8", width=ancho, height=alto, command=lambda:clic("8"))
  109. boton.place(x=107, y=320)
  110. boton = tk.Button(text="9", width=ancho, height=alto, command=lambda:clic("9"))
  111. boton.place(x=197, y=320)
  112. boton = tk.Button(text="x", width=ancho, height=alto, bg="Steel Blue", command=lambda:clic("*"))
  113. boton.place(x=287, y=320)
  114.  
  115. # cuarta fila: ( 0 ) /
  116. boton = tk.Button(text="(", width=ancho, height=alto, bg="Sky Blue", command=lambda:clic("("))
  117. boton.place(x=17, y=380)
  118. boton = tk.Button(text="0", width=ancho, height=alto, command=lambda:clic("0"))
  119. boton.place(x=107, y=380)
  120. boton = tk.Button(text=")", width=ancho, height=alto, bg="Sky Blue", command=lambda:clic(")"))
  121. boton.place(x=197, y=380)
  122. boton = tk.Button(text="/", width=ancho, height=alto, bg="Steel Blue", command=lambda:clic("/"))
  123. boton.place(x=287, y=380)
  124.  
  125. # quinta fila: raiz coma decimal  potencia resto
  126. boton = tk.Button(text="\u221A", width=ancho, height=alto, bg="sky blue", command=lambda:clic("sqrt("))
  127. boton.place(x=17, y=440)
  128. boton = tk.Button(text=".", width=ancho, height=alto, bg="sky blue", command=lambda:clic("."))
  129. boton.place(x=107, y=440)
  130. boton = tk.Button(text="POW", width=ancho, height=alto, bg="Sky Blue", command=lambda:clic("**"))
  131. boton.place(x=197, y=440)
  132. boton = tk.Button(text="%", width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic("/"))
  133. boton.place(x=287, y=440)
  134.  
  135. # sexta fila: clear factorial Pi =
  136. boton = tk.Button(text="CL", width=ancho, height=alto, bg="medium aquamarine", command=limpiar_pantalla)
  137. boton.place(x=17, y=500)
  138. boton = tk.Button(text="!", width=ancho, height=alto, bg="sky blue", command=lambda:clic("factorial("))
  139. boton.place(x=107, y=500)
  140. boton = tk.Button(text="\u03c0", width=ancho, height=alto, bg="Sky Blue", command=lambda:clic(str(pi)))
  141. boton.place(x=197, y=500)
  142. boton = tk.Button(text="=", width=ancho, height=alto, bg="medium aquamarine", command=hacer_cuenta)
  143. boton.place(x=287, y=500)
  144.  
  145.  
  146.  
  147.  
  148.  
  149. calc.mainloop()
  150.  
Advertisement
Add Comment
Please, Sign In to add comment