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 desde cmd
- python -m pip install pyinstaller
- 2) navegan hasta la carpeta donde tienen el script y ejecutan DESDE CMD
- pyinstaller --onefile --noconsole <nombre del script>
- """
- import tkinter as tk
- from math import sqrt, factorial, pi
- def limpiar_pantalla():
- global calculo_consola
- calculo_consola = ""
- calculo.set("0")
- def clic(tecla):
- global calculo_consola
- calculo_consola = calculo_consola + tecla
- calculo.set(calculo_consola)
- def hacer_cuenta():
- global calculo_consola
- try:
- resultado = str(eval(calculo_consola))
- except Exception:
- limpiar_pantalla()
- resultado = "ERROR"
- calculo.set(resultado)
- def borrar_caracter():
- global calculo_consola
- calculo_consola = calculo_consola[:-1]
- calculo.set(calculo_consola)
- # creo la variable para MOSTRAR la cuenta EN LA CONSOLA
- calculo_consola = ""
- # ventana 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 la cuenta EN LA PANTALLA
- calculo = tk.StringVar()
- # llamo a la función de limpieza para inicializar la calculadora
- limpiar_pantalla()
- # creo la pantalla
- pantalla = tk.Entry(
- font = ('arial',20,'bold'),
- width = 20,
- bd = 18, # grosor del relieve del borde
- justify = "right",
- state = tk.DISABLED, # impido escribir adentro de la caja de texto
- textvariable = calculo
- )
- pantalla.place(x=20, y=50)
- # creo las dimensiones de las teclas
- ancho = 9
- alto = 2
- boton = tk.Button(text="DEL", width=ancho, height=alto, bg="medium aquamarine", command=borrar_caracter)
- boton.place(x=287, y=140)
- ## Teclas
- # primera fila: 1 2 3 +
- boton = tk.Button(text="1", width=ancho, height=alto, command=lambda:clic("1"))
- boton.place(x=17, y=200)
- boton = tk.Button(text="2", width=ancho, height=alto, command=lambda:clic("2"))
- boton.place(x=107, y=200)
- boton = tk.Button(text="3", width=ancho, height=alto, command=lambda:clic("3"))
- boton.place(x=197, y=200)
- boton = tk.Button(text="+", width=ancho, height=alto, bg="Steel Blue", command=lambda:clic("+"))
- boton.place(x=287, y=200)
- # segunda fila: 4 5 6 -
- boton = tk.Button(text="4", width=ancho, height=alto, command=lambda:clic("4"))
- boton.place(x=17, y=260)
- boton = tk.Button(text="5", width=ancho, height=alto, command=lambda:clic("5"))
- boton.place(x=107, y=260)
- boton = tk.Button(text="6", width=ancho, height=alto, command=lambda:clic("6"))
- boton.place(x=197, y=260)
- boton = tk.Button(text="-", width=ancho, height=alto, bg="Steel Blue", command=lambda:clic("-"))
- boton.place(x=287, y=260)
- # tercera fila: 7 8 9 x
- boton = tk.Button(text="7", width=ancho, height=alto, command=lambda:clic("7"))
- boton.place(x=17, y=320)
- boton = tk.Button(text="8", width=ancho, height=alto, command=lambda:clic("8"))
- boton.place(x=107, y=320)
- boton = tk.Button(text="9", width=ancho, height=alto, command=lambda:clic("9"))
- boton.place(x=197, y=320)
- boton = tk.Button(text="x", width=ancho, height=alto, bg="Steel Blue", command=lambda:clic("*"))
- boton.place(x=287, y=320)
- # cuarta fila: ( 0 ) /
- boton = tk.Button(text="(", width=ancho, height=alto, bg="Sky Blue", command=lambda:clic("("))
- boton.place(x=17, y=380)
- boton = tk.Button(text="0", width=ancho, height=alto, command=lambda:clic("0"))
- boton.place(x=107, y=380)
- boton = tk.Button(text=")", width=ancho, height=alto, bg="Sky Blue", command=lambda:clic(")"))
- boton.place(x=197, y=380)
- boton = tk.Button(text="/", width=ancho, height=alto, bg="Steel Blue", command=lambda:clic("/"))
- boton.place(x=287, y=380)
- # quinta fila: raiz coma decimal potencia resto
- boton = tk.Button(text="\u221A", width=ancho, height=alto, bg="sky blue", command=lambda:clic("sqrt("))
- boton.place(x=17, y=440)
- boton = tk.Button(text=".", width=ancho, height=alto, bg="sky blue", command=lambda:clic("."))
- boton.place(x=107, y=440)
- boton = tk.Button(text="POW", width=ancho, height=alto, bg="Sky Blue", command=lambda:clic("**"))
- boton.place(x=197, y=440)
- boton = tk.Button(text="%", width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic("/"))
- boton.place(x=287, y=440)
- # sexta fila: clear factorial Pi =
- boton = tk.Button(text="CL", width=ancho, height=alto, bg="medium aquamarine", command=limpiar_pantalla)
- boton.place(x=17, y=500)
- boton = tk.Button(text="!", width=ancho, height=alto, bg="sky blue", command=lambda:clic("factorial("))
- boton.place(x=107, y=500)
- boton = tk.Button(text="\u03c0", width=ancho, height=alto, bg="Sky Blue", command=lambda:clic(str(pi)))
- boton.place(x=197, y=500)
- boton = tk.Button(text="=", width=ancho, height=alto, bg="medium aquamarine", command=hacer_cuenta)
- boton.place(x=287, y=500)
- calc.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment