Advertisement
teslariu

calculadora

Sep 12th, 2023
802
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.95 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 (pyinstaller) (en cmd)
  7. python -m pip install pyinstaller
  8.  
  9. 2) Desde la carpeta donde tienen el script, ejecutar (en cmd)
  10. pyinstaller --onefile --noconsole <nombre del script>
  11.  
  12. Script que implementa una calculadora científica
  13.  
  14. """
  15.  
  16. import tkinter as tk
  17. from math import sqrt, factorial, pi
  18.  
  19.  
  20. #############  funciones #########################
  21. def clic(tecla):
  22.     global calculo_consola
  23.     calculo_consola = calculo_consola + tecla
  24.     calculo.set(calculo_consola)
  25.    
  26.    
  27.    
  28.    
  29. def limpiar():
  30.     global calculo_consola
  31.     calculo_consola = ""
  32.     calculo.set("0")
  33.    
  34.    
  35. def hacer_calculo():
  36.     global calculo_consola
  37.     try:
  38.         cuenta = str(eval(calculo_consola))
  39.     except Exception:
  40.         limpiar()
  41.         cuenta = "ERROR"
  42.    
  43.     calculo.set(cuenta)
  44.    
  45.    
  46. def borrar_caracter():
  47.     global calculo_consola
  48.     calculo_consola = calculo_consola[:-1]
  49.     calculo.set(calculo_consola)
  50.    
  51.                                      
  52.                                        
  53. ################# lado consola
  54.  
  55. # variable para HACER el cálculo
  56. calculo_consola = ""
  57.  
  58.  
  59.  
  60. ############### lado gráfico
  61.  
  62. # diseño de la calculadora
  63.  
  64. calc = tk.Tk()
  65. calc.config(width=390, height=600, bg="Light Steel Blue")
  66. calc.title("Calculadora ACME")
  67. calc.resizable(False, False)
  68.  
  69.  
  70.  
  71. # creo la variable para MOSTRAR el cálculo EN LA PANTALLA
  72. calculo = tk.StringVar()
  73.  
  74. # limpio la pantalla
  75. limpiar()
  76.  
  77.  
  78. # creo la pantalla
  79. pantalla = tk.Entry(
  80.         font=('arial',21,'bold'),
  81.         width=20,
  82.         bd=16,
  83.         justify='right',
  84.         state=tk.DISABLED,
  85.         textvariable=calculo
  86. )
  87. pantalla.place(x=20,y=50)
  88.  
  89. # genero las dimensiones de las teclas
  90. ancho = 9
  91. alto = 2
  92.  
  93.  
  94. boton = tk.Button(text="DEL",width=ancho, height=alto, bg='SeaGreen3', command=borrar_caracter)
  95. boton.place(x=290, y=140)
  96.  
  97. # primera fila: 1 2 3 +
  98. boton = tk.Button(text="1",width=ancho, height=alto, bg='Beige', command=lambda:clic("1"))
  99. boton.place(x=20, y=200)
  100. boton = tk.Button(text="2",width=ancho, height=alto, bg='Beige',command=lambda:clic("2"))
  101. boton.place(x=110, y=200)
  102. boton = tk.Button(text="3",width=ancho, height=alto, bg='Beige',command=lambda:clic("3"))
  103. boton.place(x=200, y=200)
  104. boton = tk.Button(text="+",width=ancho, height=alto, bg='Steel Blue', command=lambda:clic("+"))
  105. boton.place(x=290, y=200)
  106.  
  107. # segunda fila: 4 5 6 -
  108. boton = tk.Button(text="4",width=ancho, height=alto, bg='Beige',command=lambda:clic("4"))
  109. boton.place(x=20, y=260)
  110. boton = tk.Button(text="5",width=ancho, height=alto, bg='Beige',command=lambda:clic("5"))
  111. boton.place(x=110, y=260)
  112. boton = tk.Button(text="6",width=ancho, height=alto, bg='Beige',command=lambda:clic("6"))
  113. boton.place(x=200, y=260)
  114. boton = tk.Button(text="-",width=ancho, height=alto, bg='Steel Blue', command=lambda:clic("-"))
  115. boton.place(x=290, y=260)
  116.  
  117. # tercera fila: 7 8 9 x
  118. boton = tk.Button(text="7",width=ancho, height=alto, bg='Beige',command=lambda:clic("7"))
  119. boton.place(x=20, y=320)
  120. boton = tk.Button(text="8",width=ancho, height=alto, bg='Beige',command=lambda:clic("8"))
  121. boton.place(x=110, y=320)
  122. boton = tk.Button(text="9",width=ancho, height=alto, bg='Beige',command=lambda:clic("9"))
  123. boton.place(x=200, y=320)
  124. boton = tk.Button(text="*",width=ancho, height=alto, bg='Steel Blue', command=lambda:clic("*"))
  125. boton.place(x=290, y=320)
  126.  
  127. # cuarta fila: ( 0 ) /
  128. boton = tk.Button(text="(",width=ancho, height=alto, bg='Sky Blue', command=lambda:clic("("))
  129. boton.place(x=20, y=380)
  130. boton = tk.Button(text="0",width=ancho, height=alto, bg='Beige',command=lambda:clic("0"))
  131. boton.place(x=110, y=380)
  132. boton = tk.Button(text=")",width=ancho, height=alto, bg='Sky Blue', command=lambda:clic(")"))
  133. boton.place(x=200, y=380)
  134. boton = tk.Button(text="/",width=ancho, height=alto, bg='Steel Blue', command=lambda:clic("/"))
  135. boton.place(x=290, y=380)
  136.  
  137. # quinta fila: Raiz, coma decimal,potencia, modulo
  138. boton = tk.Button(text="\u221A",width=ancho, height=alto, bg='Steel Blue3', command=lambda:clic("sqrt("))
  139. boton.place(x=20, y=440)
  140. boton = tk.Button(text=".",width=ancho, height=alto, bg='Beige',command=lambda:clic("."))
  141. boton.place(x=110, y=440)
  142. boton = tk.Button(text="POW",width=ancho, height=alto, bg='Steel Blue3', command=lambda:clic("**"))
  143. boton.place(x=200, y=440)
  144. boton = tk.Button(text="%",width=ancho, height=alto, bg='Steel Blue3',command=lambda:clic("%"))
  145. boton.place(x=290, y=440)
  146.  
  147. # sexta fila: Clear, factorial, pi ,=
  148. boton = tk.Button(text="CL",width=ancho, height=alto, bg='medium aquamarine', command=limpiar)
  149. boton.place(x=20, y=500)
  150. boton = tk.Button(text="!",width=ancho, height=alto, bg='Steel Blue3',command=lambda:clic("factorial("))
  151. boton.place(x=110, y=500)
  152. boton = tk.Button(text="\u03c0",width=ancho, height=alto, bg='Steel Blue2', command=lambda:clic(str(pi)))
  153. boton.place(x=200, y=500)
  154. boton = tk.Button(text="=",width=ancho, height=alto, bg='medium aquamarine',command=hacer_calculo)
  155. boton.place(x=290, y=500)
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162. calc.mainloop()
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement