Advertisement
teslariu

calcu ACME

May 15th, 2023
1,073
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.60 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. """
  5.  
  6. Instalación de pyinstaller:
  7.  
  8.    python -m pip install pyinstaller
  9.  
  10. # Dentro de la carpeta donde tengo el archivo desde CMD, ejecutar
  11.  
  12.    pyinstaller --onefile --noconsole calcu.py
  13.    
  14. """
  15.  
  16.  
  17.  
  18. import tkinter as tk
  19. from math import sqrt, factorial , pi
  20.  
  21. def clic(tecla):
  22.     global cuenta_consola
  23.     cuenta_consola = cuenta_consola + tecla
  24.     cuenta.set(cuenta_consola)
  25.  
  26.  
  27. def borrar_pantalla():
  28.     global cuenta_consola
  29.     cuenta_consola = ""
  30.     cuenta.set("0")
  31.    
  32.    
  33.    
  34. def hacer_cuenta():
  35.     global cuenta_consola
  36.     try:
  37.         total = str(eval(cuenta_consola))
  38.         cuenta.set(total)
  39.     except Exception:
  40.         borrar_pantalla()
  41.         cuenta.set("ERROR")
  42.        
  43. def borrar_caracter():
  44.     global cuenta_consola
  45.     cuenta_consola = cuenta_consola[:-1]
  46.     cuenta.set(cuenta_consola)
  47.  
  48.  
  49. calculadora = tk.Tk()
  50. calculadora.title("Calculadora ACME")
  51. calculadora.config(width=390, height=600, bg="Light Steel Blue")
  52. calculadora.resizable(0,0)
  53. # calculadora.iconbitmap("icono.ico")
  54.  
  55.  
  56. # variable para mostrar la cuenta en la pantalla
  57. cuenta = tk.StringVar()
  58.  
  59. # variable para hacer la cuenta en la consola
  60. cuenta_consola = ""
  61.  
  62. borrar_pantalla()
  63.  
  64. # Creo la pantalla
  65. pantalla = tk.Entry(
  66.             font = ('arial', 20, 'bold'),
  67.             width = 20,
  68.             bd = 15,
  69.             bg = 'powder blue',
  70.             justify = 'right',
  71.             state = tk.DISABLED,
  72.             textvariable = cuenta
  73.         )
  74. pantalla.place(x=25, y=50)
  75.  
  76. # defino las dimensiones de las teclas
  77. ancho = 10
  78. alto = 2
  79.  
  80. ######### Teclas  ########################
  81.  
  82. b = tk.Button(text="DEL", width=ancho, height=alto, bg="medium aquamarine", command=borrar_caracter)
  83. b.place(x=287, y=140)
  84.  
  85. ### Primera fila: 1 2 3 +
  86. b = tk.Button(text="1", width=ancho, height=alto, command=lambda:clic("1"))
  87. b.place(x=17, y=200)
  88. b = tk.Button(text="2", width=ancho, height=alto, command=lambda:clic("2"))
  89. b.place(x=107, y=200)
  90. b = tk.Button(text="3", width=ancho, height=alto, command=lambda:clic("3"))
  91. b.place(x=197, y=200)
  92. b = tk.Button(text="+", width=ancho, height=alto, bg="SteelBlue", command=lambda:clic("+"))
  93. b.place(x=287, y=200)
  94.  
  95. ### Segunda fila 4 5 6 -
  96. b = tk.Button(text="4", width=ancho, height=alto, command=lambda:clic("4"))
  97. b.place(x=17, y=260)
  98. b = tk.Button(text="5", width=ancho, height=alto, command=lambda:clic("5"))
  99. b.place(x=107, y=260)
  100. b = tk.Button(text="6", width=ancho, height=alto, command=lambda:clic("6"))
  101. b.place(x=197, y=260)
  102. b = tk.Button(text="-", width=ancho, height=alto, bg="SteelBlue", command=lambda:clic("-"))
  103. b.place(x=287, y=260)
  104.  
  105. ### Tercera fila: 7 8 9 x
  106. b = tk.Button(text="7", width=ancho, height=alto, command=lambda:clic("7"))
  107. b.place(x=17, y=320)
  108. b = tk.Button(text="8", width=ancho, height=alto, command=lambda:clic("8"))
  109. b.place(x=107, y=320)
  110. b = tk.Button(text="9", width=ancho, height=alto, command=lambda:clic("9"))
  111. b.place(x=197, y=320)
  112. b = tk.Button(text="x", width=ancho, height=alto, bg="SteelBlue", command=lambda:clic("*"))
  113. b.place(x=287, y=320)
  114.  
  115. ### Cuarta fila: ( 0 ) /
  116. b = tk.Button(text="(", width=ancho, height=alto, bg="Sky Blue", command=lambda:clic("("))
  117. b.place(x=17, y=380)
  118. b = tk.Button(text="0", width=ancho, height=alto, command=lambda:clic("0"))
  119. b.place(x=107, y=380)
  120. b = tk.Button(text=")", width=ancho, height=alto, bg="Sky Blue", command=lambda:clic(")"))
  121. b.place(x=197, y=380)
  122. b = tk.Button(text="/", width=ancho, height=alto, bg="SteelBlue", command=lambda:clic("/"))
  123. b.place(x=287, y=380)
  124.  
  125.  
  126. ### Quinta fila: Raiz Coma decimal potencia  resto
  127. b = tk.Button(text="\u221A", width=ancho, height=alto, bg="Sky Blue", command=lambda:clic("sqrt("))
  128. b.place(x=17, y=440)
  129. b = tk.Button(text=".", width=ancho, height=alto, bg="SteelBlue", command=lambda:clic("."))
  130. b.place(x=107, y=440)
  131. b = tk.Button(text="pow", width=ancho, height=alto, bg="Sky Blue", command=lambda:clic("**"))
  132. b.place(x=197, y=440)
  133. b = tk.Button(text="%", width=ancho, height=alto, bg="Sky Blue", command=lambda:clic("%"))
  134. b.place(x=287, y=440)
  135.  
  136. ### Sexta fila: Clear Factorial PI =
  137. b = tk.Button(text="CL", width=ancho, height=alto, bg="medium aquamarine", command=borrar_pantalla)
  138. b.place(x=17, y=500)
  139. b = tk.Button(text="!", width=ancho, height=alto, bg="Sky Blue", command=lambda:clic("factorial("))
  140. b.place(x=107, y=500)
  141. b = tk.Button(text="\u03C0", width=ancho, height=alto, bg="Sky Blue", command=lambda:clic(str(pi)))
  142. b.place(x=197, y=500)
  143. b = tk.Button(text="=", width=ancho, height=alto, bg="medium aquamarine", command=hacer_cuenta)
  144. b.place(x=287, y=500)
  145.  
  146.  
  147. calculadora.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement