Advertisement
teslariu

calc tkinter acme

Dec 3rd, 2022
529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.83 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # python -m pip install pyinstaller
  5. # pynstaller --onefile --noconsole acme.py
  6. #
  7. # calculadora marca ACME
  8.  
  9. import tkinter as tk
  10. from math import sqrt, factorial, pi
  11.  
  12. def limpieza():
  13.     global calculo_consola
  14.     calculo.set("0")        # limpio la pantalla
  15.     calculo_consola = ""    # limpio el cálculo en consola
  16.  
  17. def clic(tecla):
  18.     global calculo_consola
  19.     calculo_consola = calculo_consola + tecla
  20.     calculo.set(calculo_consola)
  21.    
  22.    
  23.    
  24. def hacer_cuenta():
  25.     global calculo_consola
  26.     try:
  27.         total = str(eval(calculo_consola))
  28.     except Exception:
  29.         limpieza()
  30.         total="ERROR"
  31.     calculo.set(total)
  32.     calculo_consola = total
  33.    
  34.    
  35. def borrar_caracter():
  36.     global calculo_consola
  37.     calculo_consola = calculo_consola[:-1]
  38.     if not calculo_consola:
  39.         calculo_consola = "0"
  40.     calculo.set(calculo_consola)
  41.     calculo_consola = ""
  42.  
  43.  
  44.  
  45. # ventana para la calculadora
  46. ventana = tk.Tk()
  47. ventana.title("Calculadora ACME")
  48. ventana.config(width=390, height=600, bg="Light Steel Blue")
  49. ventana.resizable(0,0)
  50.  
  51. # Creo la variable para mostrar el cálculo en pantalla
  52. calculo = tk.StringVar()
  53.  
  54. limpieza()
  55.  
  56. # creo la pantalla
  57. pantalla = tk.Entry(
  58.             font=("arial",20,"bold"),
  59.             width=21,
  60.             bd=15,
  61.             bg="powder blue",
  62.             justify="right",
  63.             state=tk.DISABLED,
  64.             textvariable=calculo
  65.     )
  66. pantalla.place(x=20, y=50)
  67.  
  68.  
  69. # creo las dimensiones de teclas
  70. ancho = 10
  71. alto = 2
  72.  
  73. # Creo la variable para mostrar el cálculo en consola
  74. calculo_consola = ""
  75.  
  76.  
  77. ################ TECLAS #######################################
  78.  
  79. tecla = tk.Button(text="<<", width=ancho, height=alto, bg="medium aquamarine", command=borrar_caracter)
  80. tecla.place(x=287, y=140)
  81.  
  82. ##### Fila 1: 1 2 3 +
  83. tecla = tk.Button(text="1", width=ancho, height=alto, command=lambda:clic("1"))
  84. tecla.place(x=17, y=200)
  85.  
  86. tecla = tk.Button(text="2", width=ancho, height=alto, command=lambda:clic("2"))
  87. tecla.place(x=107, y=200)
  88.  
  89. tecla = tk.Button(text="3", width=ancho, height=alto, command=lambda:clic("3"))
  90. tecla.place(x=197, y=200)
  91.  
  92. tecla = tk.Button(text="+", width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic("+"))
  93. tecla.place(x=287, y=200)
  94.  
  95.  
  96. ##### Fila 2: 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.  
  100. tecla = tk.Button(text="5", width=ancho, height=alto, command=lambda:clic("5"))
  101. tecla.place(x=107, y=260)
  102.  
  103. tecla = tk.Button(text="6", width=ancho, height=alto, command=lambda:clic("6"))
  104. tecla.place(x=197, y=260)
  105.  
  106. tecla = tk.Button(text="-", width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic("-"))
  107. tecla.place(x=287, y=260)
  108.  
  109.  
  110. ##### Fila 3: 7 8 9 x
  111. tecla = tk.Button(text="7", width=ancho, height=alto, command=lambda:clic("7"))
  112. tecla.place(x=17, y=320)
  113.  
  114. tecla = tk.Button(text="8", width=ancho, height=alto, command=lambda:clic("8"))
  115. tecla.place(x=107, y=320)
  116.  
  117. tecla = tk.Button(text="9", width=ancho, height=alto, command=lambda:clic("9"))
  118. tecla.place(x=197, y=320)
  119.  
  120. tecla = tk.Button(text="x", width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic("*"))
  121. tecla.place(x=287, y=320)
  122.  
  123.  
  124. ##### Fila 4: ( 0 ) /
  125. tecla = tk.Button(text="(", width=ancho, height=alto, bg="sky blue", command=lambda:clic("("))
  126. tecla.place(x=17, y=380)
  127.  
  128. tecla = tk.Button(text="0", width=ancho, height=alto, command=lambda:clic("0"))
  129. tecla.place(x=107, y=380)
  130.  
  131. tecla = tk.Button(text=")", width=ancho, height=alto, bg="sky blue", command=lambda:clic(")"))
  132. tecla.place(x=197, y=380)
  133.  
  134. tecla = tk.Button(text="/", width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic("/"))
  135. tecla.place(x=287, y=380)
  136.  
  137.  
  138. ##### Fila 5: Raiz Coma decimal potencia resto
  139. tecla = tk.Button(text="\u221A", width=ancho, height=alto, bg="sky blue", command=lambda:clic("sqrt("))
  140. tecla.place(x=17, y=440)
  141.  
  142. tecla = tk.Button(text=".", width=ancho, height=alto, command=lambda:clic("."))
  143. tecla.place(x=107, y=440)
  144.  
  145. tecla = tk.Button(text="POW", width=ancho, height=alto, bg="sky blue", command=lambda:clic("**"))
  146. tecla.place(x=197, y=440)
  147.  
  148. tecla = tk.Button(text="%", width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic("%"))
  149. tecla.place(x=287, y=440)
  150.  
  151.  
  152. ##### Fila 6: Clear, factorial, pi =
  153. tecla = tk.Button(text="CL", width=ancho, height=alto, bg="medium aquamarine", command=limpieza)
  154. tecla.place(x=17, y=500)
  155.  
  156. tecla = tk.Button(text="!", width=ancho, height=alto, command=lambda:clic("factorial("))
  157. tecla.place(x=107, y=500)
  158.  
  159. tecla = tk.Button(text="pi", width=ancho, height=alto, bg="sky blue", command=lambda:clic(str(pi)))
  160. tecla.place(x=197, y=500)
  161.  
  162. tecla = tk.Button(text="=", width=ancho, height=alto, bg="SteelBlue2", command=hacer_cuenta)
  163. tecla.place(x=287, y=500)
  164.  
  165.  
  166.  
  167. ventana.mainloop()
  168.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement