Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- #
- import tkinter as tk
- from math import sqrt, pi, factorial
- ##### lado "consola" ###################
- def clic(tecla):
- global calculo_consola
- calculo_consola = calculo_consola + tecla
- calculo.set(calculo_consola)
- def limpieza():
- global calculo_consola
- calculo_consola = ""
- calculo.set("0")
- def hacer_calculo():
- global calculo_consola
- try:
- calculo_consola = str(eval(calculo_consola))
- calculo.set(calculo_consola)
- except Exception:
- calculo_consola = ""
- calculo.set("ERROR")
- def borrar_caracter():
- global calculo_consola
- calculo_consola = calculo_consola[:-1]
- if calculo_consola:
- calculo.set(calculo_consola)
- else:
- calculo.set("0")
- # creo una variable del tipo string para almacenar el contenido a mostrar
- # en la pantalla
- calculo_consola = ""
- #### lado "grafico" ######################
- calculadora = tk.Tk()
- calculadora.title("Calculadora ACME")
- calculadora.config(width=370, height=600, bg="Light Steel Blue")
- calculadora.resizable(0,0)
- # creo una variable del tipo StringVar para almacenar el contenido a mostrar
- # en la pantalla
- calculo = tk.StringVar()
- limpieza()
- # defino el ancho y alto de las teclas:
- ancho = 10
- alto = 2
- # defino la pantalla
- pantalla = tk.Entry(
- font = ["arial",20,"bold"],
- width = 21,
- bd = 15, # grosor del borde
- bg = "powder blue",
- justify = "right",
- state = tk.DISABLED, # solo lectura
- textvariable = calculo # variable cuyo contenido se muestra en pantalla
- )
- pantalla.place(x=10,y=50)
- ######## teclas
- # tecla para borrar el ultimo caracter
- tecla = tk.Button(text="\u22B3", width=ancho, height=alto, command=borrar_caracter)
- tecla.place(x=280, y=140)
- # Primera fila 1 2 3 +
- tecla = tk.Button(text="1", width=ancho, height=alto, command=lambda:clic("1"))
- tecla.place(x=10, y=200)
- tecla = tk.Button(text="2", width=ancho, height=alto, command=lambda:clic("2"))
- tecla.place(x=100, y=200)
- tecla = tk.Button(text="3", width=ancho, height=alto, command=lambda:clic("3"))
- tecla.place(x=190, y=200)
- tecla = tk.Button(text="+", width=ancho, height=alto, bg="dodger blue", command=lambda:clic("+"))
- tecla.place(x=280, y=200)
- # Segunda fila 4 5 6 -
- tecla = tk.Button(text="4", width=ancho, height=alto, command=lambda:clic("4"))
- tecla.place(x=10, y=260)
- tecla = tk.Button(text="5", width=ancho, height=alto, command=lambda:clic("5"))
- tecla.place(x=100, y=260)
- tecla = tk.Button(text="6", width=ancho, height=alto, command=lambda:clic("6"))
- tecla.place(x=190, y=260)
- tecla = tk.Button(text="-", width=ancho, height=alto, bg="dodger blue", command=lambda:clic("-"))
- tecla.place(x=280, y=260)
- # Tercera fila 7 8 9 x
- tecla = tk.Button(text="7", width=ancho, height=alto, command=lambda:clic("7"))
- tecla.place(x=10, y=320)
- tecla = tk.Button(text="8", width=ancho, height=alto, command=lambda:clic("8"))
- tecla.place(x=100, y=320)
- tecla = tk.Button(text="9", width=ancho, height=alto, command=lambda:clic("9"))
- tecla.place(x=190, y=320)
- tecla = tk.Button(text="x", width=ancho, height=alto, bg="dodger blue", command=lambda:clic("*"))
- tecla.place(x=280, y=320)
- # Cuarta fila ( 0 ) /
- tecla = tk.Button(text="(", width=ancho, height=alto, bg="Light blue", command=lambda:clic("("))
- tecla.place(x=10, y=380)
- tecla = tk.Button(text="0", width=ancho, height=alto, command=lambda:clic("0"))
- tecla.place(x=100, y=380)
- tecla = tk.Button(text=")", width=ancho, height=alto, bg="Light blue", command=lambda:clic(")"))
- tecla.place(x=190, y=380)
- tecla = tk.Button(text="/", width=ancho, height=alto, bg="dodger blue", command=lambda:clic("/"))
- tecla.place(x=280, y=380)
- # Quinta fila Raiz Coma decimal Potencia Modulo
- tecla = tk.Button(text="\u221A", width=ancho, height=alto, bg="Skyblue3", command=lambda:clic("sqrt("))
- tecla.place(x=10, y=440)
- tecla = tk.Button(text=".", width=ancho, height=alto, command=lambda:clic("."))
- tecla.place(x=100, y=440)
- tecla = tk.Button(text="POW", width=ancho, height=alto, bg="Skyblue3",command=lambda:clic("**"))
- tecla.place(x=190, y=440)
- tecla = tk.Button(text="%", width=ancho, height=alto, bg="dodger blue", command=lambda:clic("%"))
- tecla.place(x=280, y=440)
- # Sexta fila Clear Factorial PI =
- tecla = tk.Button(text="CL", width=ancho, height=alto, bg="Light blue", command=limpieza)
- tecla.place(x=10, y=500)
- tecla = tk.Button(text="!", width=ancho, height=alto, bg="Skyblue3", command=lambda:clic("factorial("))
- tecla.place(x=100, y=500)
- tecla = tk.Button(text="\u03C0", width=ancho, height=alto, bg="Light blue", command=lambda:clic(str(pi)))
- tecla.place(x=190, y=500)
- tecla = tk.Button(text="=", width=ancho, height=alto, bg="dodger blue", command=hacer_calculo)
- tecla.place(x=280, y=500)
- calculadora.mainloop()
- """
- Existen bibliotecas de terceros en el sitio web pypi.org. Se pueden descargar
- con la utilidad pip del intèrprete de python. Para crear un ejecutable
- usaremos la biblioteca de terceros pyinstaller
- 1) Desde el prompt de Windows/ shel de Linux, etc, ejecutar
- python -m pip install pyinstaller (o simplemente pip install pyinstaller)
- 2) Navegar hasta la carpeta del script y ejecutar
- pyinstaller --noconsole --onefile calcu.py
- (Esto genera dos carpetas, build y dist, el ejecutable está en la carpeta dist)
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement