Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- """
- como hacer un portable
- 1) Instalar una libreria externa (pyinstaller) (en cmd)
- python -m pip install pyinstaller
- 2) Desde la carpeta donde tienen el script, ejecutar (en cmd)
- pyinstaller --onefile --noconsole <nombre del script>
- Script que implementa una calculadora científica
- """
- import tkinter as tk
- from math import sqrt, factorial, pi
- ############# funciones #########################
- def clic(tecla):
- global calculo_consola
- calculo_consola = calculo_consola + tecla
- calculo.set(calculo_consola)
- def limpiar():
- global calculo_consola
- calculo_consola = ""
- calculo.set("0")
- def hacer_calculo():
- global calculo_consola
- try:
- cuenta = str(eval(calculo_consola))
- except Exception:
- limpiar()
- cuenta = "ERROR"
- calculo.set(cuenta)
- def borrar_caracter():
- global calculo_consola
- calculo_consola = calculo_consola[:-1]
- calculo.set(calculo_consola)
- ################# lado consola
- # variable para HACER el cálculo
- calculo_consola = ""
- ############### lado gráfico
- # diseño de la calculadora
- calc = tk.Tk()
- calc.config(width=390, height=600, bg="Light Steel Blue")
- calc.title("Calculadora ACME")
- calc.resizable(False, False)
- # creo la variable para MOSTRAR el cálculo EN LA PANTALLA
- calculo = tk.StringVar()
- # limpio la pantalla
- limpiar()
- # creo la pantalla
- pantalla = tk.Entry(
- font=('arial',21,'bold'),
- width=20,
- bd=16,
- justify='right',
- state=tk.DISABLED,
- textvariable=calculo
- )
- pantalla.place(x=20,y=50)
- # genero las dimensiones de las teclas
- ancho = 9
- alto = 2
- boton = tk.Button(text="DEL",width=ancho, height=alto, bg='SeaGreen3', command=borrar_caracter)
- boton.place(x=290, y=140)
- # primera fila: 1 2 3 +
- boton = tk.Button(text="1",width=ancho, height=alto, bg='Beige', command=lambda:clic("1"))
- boton.place(x=20, y=200)
- boton = tk.Button(text="2",width=ancho, height=alto, bg='Beige',command=lambda:clic("2"))
- boton.place(x=110, y=200)
- boton = tk.Button(text="3",width=ancho, height=alto, bg='Beige',command=lambda:clic("3"))
- boton.place(x=200, y=200)
- boton = tk.Button(text="+",width=ancho, height=alto, bg='Steel Blue', command=lambda:clic("+"))
- boton.place(x=290, y=200)
- # segunda fila: 4 5 6 -
- boton = tk.Button(text="4",width=ancho, height=alto, bg='Beige',command=lambda:clic("4"))
- boton.place(x=20, y=260)
- boton = tk.Button(text="5",width=ancho, height=alto, bg='Beige',command=lambda:clic("5"))
- boton.place(x=110, y=260)
- boton = tk.Button(text="6",width=ancho, height=alto, bg='Beige',command=lambda:clic("6"))
- boton.place(x=200, y=260)
- boton = tk.Button(text="-",width=ancho, height=alto, bg='Steel Blue', command=lambda:clic("-"))
- boton.place(x=290, y=260)
- # tercera fila: 7 8 9 x
- boton = tk.Button(text="7",width=ancho, height=alto, bg='Beige',command=lambda:clic("7"))
- boton.place(x=20, y=320)
- boton = tk.Button(text="8",width=ancho, height=alto, bg='Beige',command=lambda:clic("8"))
- boton.place(x=110, y=320)
- boton = tk.Button(text="9",width=ancho, height=alto, bg='Beige',command=lambda:clic("9"))
- boton.place(x=200, y=320)
- boton = tk.Button(text="*",width=ancho, height=alto, bg='Steel Blue', command=lambda:clic("*"))
- boton.place(x=290, y=320)
- # cuarta fila: ( 0 ) /
- boton = tk.Button(text="(",width=ancho, height=alto, bg='Sky Blue', command=lambda:clic("("))
- boton.place(x=20, y=380)
- boton = tk.Button(text="0",width=ancho, height=alto, bg='Beige',command=lambda:clic("0"))
- boton.place(x=110, y=380)
- boton = tk.Button(text=")",width=ancho, height=alto, bg='Sky Blue', command=lambda:clic(")"))
- boton.place(x=200, y=380)
- boton = tk.Button(text="/",width=ancho, height=alto, bg='Steel Blue', command=lambda:clic("/"))
- boton.place(x=290, y=380)
- # quinta fila: Raiz, coma decimal,potencia, modulo
- boton = tk.Button(text="\u221A",width=ancho, height=alto, bg='Steel Blue3', command=lambda:clic("sqrt("))
- boton.place(x=20, y=440)
- boton = tk.Button(text=".",width=ancho, height=alto, bg='Beige',command=lambda:clic("."))
- boton.place(x=110, y=440)
- boton = tk.Button(text="POW",width=ancho, height=alto, bg='Steel Blue3', command=lambda:clic("**"))
- boton.place(x=200, y=440)
- boton = tk.Button(text="%",width=ancho, height=alto, bg='Steel Blue3',command=lambda:clic("%"))
- boton.place(x=290, y=440)
- # sexta fila: Clear, factorial, pi ,=
- boton = tk.Button(text="CL",width=ancho, height=alto, bg='medium aquamarine', command=limpiar)
- boton.place(x=20, y=500)
- boton = tk.Button(text="!",width=ancho, height=alto, bg='Steel Blue3',command=lambda:clic("factorial("))
- boton.place(x=110, y=500)
- boton = tk.Button(text="\u03c0",width=ancho, height=alto, bg='Steel Blue2', command=lambda:clic(str(pi)))
- boton.place(x=200, y=500)
- boton = tk.Button(text="=",width=ancho, height=alto, bg='medium aquamarine',command=hacer_calculo)
- boton.place(x=290, y=500)
- calc.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement