Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- """
- existen bibliotecas externas para aumentar la funcionalidad de python
- Se pueden instalar usando el comando pip
- Para instalar una biblio externa que cree un ejecutable (.exe) de la calculadora
- usaremos:
- python -m pip install pyinstaller
- luego, para crear un ejecutable lanzar desde cmd este comando
- pyinstaller --noconsole --onefile calculadoraACME.py
- """
- import tkinter as tk
- from math import sqrt, factorial, pi
- # creo una variable para hacer el calculo del lado de la consola
- calculo_consola = ""
- def clic(tecla):
- global calculo_consola
- calculo_consola = calculo_consola + tecla
- calculo.set(calculo_consola)
- def limpieza():
- global calculo_consola
- calculo.set("0") # pone un cero en la pantalla
- calculo_consola = ""
- def hacer_cuenta():
- global calculo_consola
- try:
- calculo_consola = str(eval(calculo_consola))
- except Exception:
- limpieza()
- calculo.set("ERROR")
- else:
- calculo.set(calculo_consola)
- def borrar_caracter():
- global calculo_consola
- calculo_consola = calculo_consola[:-1]
- calculo.set(calculo_consola)
- ventana = tk.Tk()
- ventana.config(width=390, height=600, bg="Light Steel Blue")
- ventana.title("Calculadora ACME")
- ventana.resizable(0,0)
- # Creo la variable calculo de tipo string gráfico para mostrar en pantalla
- calculo = tk.StringVar()
- # dimensiones de las teclas
- ancho = 10
- alto = 2
- # llamo a la funcion de limpieza para inicializar la calculadora
- limpieza()
- ##### Creamos la pantalla
- pantalla = tk.Entry(
- font = ('arial',20,'bold'),
- width = 20,
- bd = 15,
- bg = "powder blue",
- justify = 'right',
- state = tk.DISABLED,
- textvariable = calculo
- )
- pantalla.place(x=20, y=50)
- # boton para borrar un caracter
- boton = tk.Button(text="DEL", width=ancho, height=alto, bg="medium aquamarine", command=borrar_caracter)
- boton.place(x=287, y=140)
- ## 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="SteelBlue", command=lambda:clic("*"))
- boton.place(x=287, y=320)
- ## Cuarta: ( 0 ) /
- boton = tk.Button(text="(", width=ancho, height=alto, bg="SteelBlue2", 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="SteelBlue2",command=lambda:clic(")"))
- boton.place(x=197, y=380)
- boton = tk.Button(text="/", width=ancho, height=alto, bg="SteelBlue", command=lambda:clic("/"))
- boton.place(x=287, y=380)
- ## Quinta: 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, 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: Clear factorial pi =
- boton = tk.Button(text="CL", width=ancho, height=alto, bg="medium aquamarine", command=limpieza)
- boton.place(x=17, y=500)
- boton = tk.Button(text="!", width=ancho, height=alto, 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)
- ventana.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement