Advertisement
teslariu

calcu

Nov 9th, 2021
951
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.16 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. import tkinter as tk
  5. from math import sqrt, factorial, pi
  6.  
  7. def clic(tecla):
  8.     global operador
  9.     operador = operador + str(tecla)  # "2+3"
  10.     calculo.set(operador)
  11.    
  12.    
  13. def limpieza():
  14.     global operador
  15.     # mando un cero a la pantalla
  16.     calculo.set("0")
  17.     # borro la variable operador
  18.     operador = ""
  19.    
  20.    
  21. def calcular():
  22.     global operador
  23.     try:
  24.         total = str(eval(operador))
  25.     except Exception:
  26.         operador = ""
  27.         total = "Syntax Error"
  28.     calculo.set(total)
  29.  
  30.  
  31.  
  32. # ventana que contiene la calculadora
  33. ventana = tk.Tk()
  34. ventana.title("CALCULADORA ACME")
  35. ventana.config(
  36.         width = 390,
  37.         height = 600,
  38.         bg = "Steel Blue",
  39.         )
  40. ventana.resizable(0,0)
  41.  
  42. # creo la variable para mostrar en la pantalla de la calculadora
  43. calculo = tk.StringVar()
  44.  
  45. # creo la variable de consola para almacenar el cálculo a realizar
  46. operador = ""     # "12.58+87/sqrt(22.5)"
  47.  
  48. # ancho y alto de cada tecla:
  49. ancho = 7
  50. alto = 2
  51.  
  52. # creamos la pantalla de
  53. pantalla = tk.Entry(
  54.                 font = ('arial',20,'bold','italic'),
  55.                 width = 20,
  56.                 bd = 20,               # tamaño del borde
  57.                 bg = 'powder blue',    # color de fondo
  58.                 justify = 'right',
  59.                 state = tk.DISABLED,   # impido escribir en la pantalla
  60.                 textvariable = calculo
  61.         )
  62. pantalla.place(x=20, y=50)
  63.  
  64. ##############  filas de botones  ####################################
  65.  
  66. ##### fila 1: 1 2 3 +
  67. boton = tk.Button(text='1', width=ancho, height=alto, font=('bold'), command=lambda:clic(1))
  68. boton.place(x=20, y=170)
  69. boton = tk.Button(text='2', width=ancho, height=alto, font=('bold'), command=lambda:clic(2))
  70. boton.place(x=110, y=170)
  71. boton = tk.Button(text='3', width=ancho, height=alto, font=('bold'), command=lambda:clic(3))
  72. boton.place(x=200, y=170)
  73. boton = tk.Button(text='+', width=ancho, height=alto, font=('bold'), bg="snow3", command=lambda:clic('+'))
  74. boton.place(x=290, y=170)
  75.  
  76. ##### fila 2: 4 5 6 -
  77. boton = tk.Button(text='4', width=ancho, height=alto, font=('bold'), command=lambda:clic(4))
  78. boton.place(x=20, y=230)
  79. boton = tk.Button(text='5', width=ancho, height=alto, font=('bold'), command=lambda:clic(5))
  80. boton.place(x=110, y=230)
  81. boton = tk.Button(text='6', width=ancho, height=alto, font=('bold'), command=lambda:clic(6))
  82. boton.place(x=200, y=230)
  83. boton = tk.Button(text='-', width=ancho, height=alto, font=('bold'), bg="snow3", command=lambda:clic('-'))
  84. boton.place(x=290, y=230)
  85.  
  86. ##### fila 3: 7 8 9 x
  87. boton = tk.Button(text='7', width=ancho, height=alto, font=('bold'), command=lambda:clic(7))
  88. boton.place(x=20, y=290)
  89. boton = tk.Button(text='8', width=ancho, height=alto, font=('bold'), command=lambda:clic(8))
  90. boton.place(x=110, y=290)
  91. boton = tk.Button(text='9', width=ancho, height=alto, font=('bold'), command=lambda:clic(9))
  92. boton.place(x=200, y=290)
  93. boton = tk.Button(text='x', width=ancho, height=alto, font=('bold'), bg="snow3", command=lambda:clic('*'))
  94. boton.place(x=290, y=290)
  95.  
  96. ##### fila 4: ( 0 ) /
  97. boton = tk.Button(text='(', width=ancho, height=alto, font=('bold'), bg="snow3", command=lambda:clic('('))
  98. boton.place(x=20, y=350)
  99. boton = tk.Button(text='0', width=ancho, height=alto, font=('bold'), command=lambda:clic(0))
  100. boton.place(x=110, y=350)
  101. boton = tk.Button(text=')', width=ancho, height=alto, font=('bold'), bg="snow3", command=lambda:clic(')'))
  102. boton.place(x=200, y=350)
  103. boton = tk.Button(text='/', width=ancho, height=alto, font=('bold'), bg="snow3", command=lambda:clic('/'))
  104. boton.place(x=290, y=350)
  105.  
  106. ##### fila 5: Raiz PuntoDecimal Potencia Resto
  107. boton = tk.Button(text='RAIZ', width=ancho, height=alto, font=('bold'), bg="snow3", command=lambda:clic('sqrt('))
  108. boton.place(x=20, y=410)
  109. boton = tk.Button(text='.', width=ancho, height=alto, font=('bold'), bg="snow3", command=lambda:clic('.'))
  110. boton.place(x=110, y=410)
  111. boton = tk.Button(text='POW', width=ancho, height=alto, font=('bold'), bg="snow3", command=lambda:clic('**'))
  112. boton.place(x=200, y=410)
  113. boton = tk.Button(text='%', width=ancho, height=alto, font=('bold'), bg="snow3", command=lambda:clic('%'))
  114. boton.place(x=290, y=410)
  115.  
  116. ##### fila 6: Clear, factorial pi =
  117. boton = tk.Button(text='CLS', width=ancho, height=alto, font=('bold'), bg="snow3", command=limpieza)
  118. boton.place(x=20, y=470)
  119. boton = tk.Button(text='!', width=ancho, height=alto, font=('bold'), bg="snow3", command=lambda:clic('factorial('))
  120. boton.place(x=110, y=470)
  121. boton = tk.Button(text='PI', width=ancho, height=alto, font=('bold'), bg="snow3", command=lambda:clic('pi'))
  122. boton.place(x=200, y=470)
  123. boton = tk.Button(text='=', width=ancho, height=alto, font=('bold'), bg="tan", command=calcular)
  124. boton.place(x=290, y=470)
  125.  
  126. ##### programa principal #########
  127.  
  128. # limpio la pantalla
  129. limpieza()
  130.  
  131.  
  132. """
  133. INSTRUCCIONES PARA HACER UN EJECUTABLE
  134. 1) descargar la biblioteca pyinstaller
  135.  python -m pip install pyinstaller
  136.  
  137. 2) Crear el ejecutable
  138.     pyinstaller --noconsole --onefile calc.py
  139.    
  140. 3) Se crean dos carpetas: build (construcción) y dist(para distribuir el software)
  141.   e dist se guardan los proyectos (por ejemplo, calc) y adentro, los ejecutables
  142.   PORTABLES .No se necesita int+erprete de python, funciona en cualquier
  143.   máquina de similar sistema operativo
  144.  
  145. """
  146.  
  147. ventana.mainloop()
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement