Advertisement
teslariu

calculadora

Sep 13th, 2022 (edited)
763
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.13 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4.  
  5. # Script que implementa una calculadora científica básica
  6.  
  7. import tkinter as tk
  8. from math import sqrt,factorial,pi
  9.  
  10. ##### funciones
  11. def clic(tecla):
  12.     global calculo_consola
  13.     calculo_consola = calculo_consola + tecla
  14.     calculo.set(calculo_consola)
  15.  
  16.  
  17. def limpieza():
  18.     global calculo_consola
  19.     calculo_consola = ""
  20.     calculo.set("0")
  21.    
  22.  
  23. def hacer_cuenta():
  24.     global calculo_consola
  25.     try:
  26.         calculo_consola = str(eval(calculo_consola))
  27.     except Exception:
  28.         limpieza()
  29.         calculo_consola = "ERROR"
  30.     calculo.set(calculo_consola)
  31.    
  32.  
  33. def borrar_caracter():
  34.     global calculo_consola
  35.     calculo_consola = calculo_consola[:-1]
  36.     if calculo_consola:
  37.         calculo.set(calculo_consola)
  38.     else:
  39.         calculo.set("0")
  40.  
  41.  
  42.    
  43. # variables 'del lado consola'
  44. calculo_consola = ""       # variable que se muestra por pantalla
  45.  
  46.  
  47. ######### Lado gráfico  ##################
  48. calculadora = tk.Tk()
  49. calculadora.title("Calculadora ACME")
  50. calculadora.config(width=390, height=600, bg="Light Steel Blue")
  51. calculadora.resizable(0,0)
  52.  
  53. # variables 'del lado gráfico'
  54. calculo = tk.StringVar()       # variable que se muestra por pantalla
  55.  
  56. # inicializo la calculadora
  57. limpieza()
  58.  
  59. # creamos una pantalla
  60. pantalla = tk.Entry(
  61.         font = ["arial",20,"bold"],
  62.         width = 22,
  63.         bd = 14,         # grosor del borde
  64.         bg = "powder blue",
  65.         justify = "right",
  66.         state = tk.DISABLED,   # la hago sólo lectura
  67.         textvariable =  calculo        # la pantalla muestra en todo momento
  68.         )                              # el contenido de la variable calculo
  69. pantalla.place(x=10, y=50)
  70.  
  71. # defino las dimensiones de las teclas
  72. ancho = 10
  73. alto = 2
  74.  
  75. #### Teclas
  76. # boton retroceso
  77. tecla = tk.Button(text="\u2190", width=ancho, height=alto, command=borrar_caracter)
  78. tecla.place(x=288, y=140)
  79.  
  80.  
  81. ## Primera fila: 1 2 3 +
  82. tecla = tk.Button(text="1", width=ancho, height=alto, command=lambda:clic("1"))
  83. tecla.place(x=18, y=200)
  84. tecla = tk.Button(text="2", width=ancho, height=alto, command=lambda:clic("2"))
  85. tecla.place(x=108, y=200)
  86. tecla = tk.Button(text="3", width=ancho, height=alto, command=lambda:clic("3"))
  87. tecla.place(x=198, y=200)
  88. tecla = tk.Button(text="+", width=ancho, height=alto, bg="Steel Blue", command=lambda:clic("+"))
  89. tecla.place(x=288, y=200)
  90.  
  91. ## Segunda fila: 4 5 6 -
  92. tecla = tk.Button(text="4", width=ancho, height=alto, command=lambda:clic("4"))
  93. tecla.place(x=18, y=260)
  94. tecla = tk.Button(text="5", width=ancho, height=alto, command=lambda:clic("5"))
  95. tecla.place(x=108, y=260)
  96. tecla = tk.Button(text="6", width=ancho, height=alto, command=lambda:clic("6"))
  97. tecla.place(x=198, y=260)
  98. tecla = tk.Button(text="-", width=ancho, height=alto, bg="Steel Blue", command=lambda:clic("-"))
  99. tecla.place(x=288, y=260)
  100.  
  101. ## Tercera fila: 7 8 9 x
  102. tecla = tk.Button(text="7", width=ancho, height=alto, command=lambda:clic("7"))
  103. tecla.place(x=18, y=320)
  104. tecla = tk.Button(text="8", width=ancho, height=alto, command=lambda:clic("8"))
  105. tecla.place(x=108, y=320)
  106. tecla = tk.Button(text="9", width=ancho, height=alto, command=lambda:clic("9"))
  107. tecla.place(x=198, y=320)
  108. tecla = tk.Button(text="x", width=ancho, height=alto, bg="Steel Blue", command=lambda:clic("*"))
  109. tecla.place(x=288, y=320)
  110.  
  111. ## Cuarta fila: ( 0 ) /
  112. tecla = tk.Button(text="(", width=ancho, height=alto, bg="Sky Blue", command=lambda:clic("("))
  113. tecla.place(x=18, y=380)
  114. tecla = tk.Button(text="0", width=ancho, height=alto, command=lambda:clic("0"))
  115. tecla.place(x=108, y=380)
  116. tecla = tk.Button(text=")", width=ancho, height=alto, bg="Sky Blue", command=lambda:clic(")"))
  117. tecla.place(x=198, y=380)
  118. tecla = tk.Button(text="/", width=ancho, height=alto, bg="Steel Blue", command=lambda:clic("/"))
  119. tecla.place(x=288, y=380)
  120.  
  121. ## Quinta fila: Raiz Coma decimal Potencia Modulo
  122. tecla = tk.Button(text="\u221A", width=ancho, height=alto, bg="Steel Blue", command=lambda:clic("sqrt("))
  123. tecla.place(x=18, y=440)
  124. tecla = tk.Button(text=".", width=ancho, height=alto, command=lambda:clic("."))
  125. tecla.place(x=108, y=440)
  126. tecla = tk.Button(text="\u005E", width=ancho, height=alto, bg="Steel Blue", command=lambda:clic("**"))
  127. tecla.place(x=198, y=440)
  128. tecla = tk.Button(text="%", width=ancho, height=alto, bg="Steel Blue", command=lambda:clic("%"))
  129. tecla.place(x=288, y=440)
  130.  
  131. ## Sexta fila: Clear Factorial pi =
  132. tecla = tk.Button(text="CL", width=ancho, height=alto, bg="Sky Blue", command=limpieza)
  133. tecla.place(x=18, y=500)
  134. tecla = tk.Button(text="!", width=ancho, height=alto, bg="Steel Blue", command=lambda:clic("factorial("))
  135. tecla.place(x=108, y=500)
  136. tecla = tk.Button(text="\u03C0", width=ancho, height=alto, bg="Sky Blue", command=lambda:clic(str(pi)))
  137. tecla.place(x=198, y=500)
  138. tecla = tk.Button(text="=", width=ancho, height=alto, bg="thistle", command=hacer_cuenta)
  139. tecla.place(x=288, y=500)
  140.  
  141. calculadora.mainloop()
  142.  
  143. """
  144. 1)instalar pyinstaller:
  145. python -m pip install pyinstaller
  146.  
  147. 2) Navegar hasta la carpeta con el script y ejecutar
  148. pyinstaller --noconsole --onefile calc.py
  149.  
  150. 3) Buscar el ejecutable dentro de la carpeta dist
  151. """
  152.  
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement