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, factorial, pi
- ########## funciones ############################
- def clic(tecla):
- global operacion
- operacion = operacion + tecla
- calculo.set(operacion)
- def limpieza():
- global operacion
- calculo.set("0") # borro la pantalla
- operacion = "" # borro del lado consola
- def hacer_cuenta():
- global operacion
- try:
- resultado = str(eval(operacion))
- except Exception:
- limpieza()
- resultado = "ERROR"
- calculo.set(resultado)
- def borrar_caracter():
- global operacion
- operacion = operacion[:-1]
- if not operacion:
- operacion="0"
- calculo.set(operacion)
- # Ventana para contener la calculadora
- ventana = tk.Tk()
- ventana.title("Calculadora ACME")
- ventana.config(width=390, height=600, bg="Light Steel Blue")
- ventana.resizable(0,0)
- ########### variables #################
- # creo una variable para mostrar el cálculo en pantalla
- calculo = tk.StringVar()
- # creo la misma variable pero del lado consola
- operacion = ""
- # llamo a limpieza antes de empezar
- limpieza()
- # creo la pantalla
- pantalla = tk.Entry(
- font = ("arial",18,"bold"),
- width = 20,
- bd = 20, # relieve
- bg = "powder blue",
- justify = "right",
- state = tk.DISABLED,
- textvariable = calculo
- )
- pantalla.place(x=20, y=50)
- # defino las dimensiones de las teclas
- ancho = 10
- alto = 2
- ##################### TECLAS ########################################
- # tecla para borrar un caracter
- b = tk.Button(text="DEL", width=ancho, height=alto, bg="medium aquamarine", command=borrar_caracter)
- b.place(x=287, y=140)
- ##### primera fila: 1 2 3 +
- b = tk.Button(text="1", width=ancho, height=alto, command=lambda:clic("1"))
- b.place(x=17, y=200)
- b = tk.Button(text="2", width=ancho, height=alto, command=lambda:clic("2"))
- b.place(x=107, y=200)
- b = tk.Button(text="3", width=ancho, height=alto, command=lambda:clic("3"))
- b.place(x=197, y=200)
- b = tk.Button(text="+", width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic("+"))
- b.place(x=287, y=200)
- #### segunda fila: 4 5 6 -
- b = tk.Button(text="4", width=ancho, height=alto, command=lambda:clic("4"))
- b.place(x=17, y=260)
- b = tk.Button(text="5", width=ancho, height=alto, command=lambda:clic("5"))
- b.place(x=107, y=260)
- b = tk.Button(text="6", width=ancho, height=alto, command=lambda:clic("6"))
- b.place(x=197, y=260)
- b = tk.Button(text="-", width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic("-"))
- b.place(x=287, y=260)
- ##### tercera fila: 7 8 9 x
- b = tk.Button(text="7", width=ancho, height=alto, command=lambda:clic("7"))
- b.place(x=17, y=320)
- b = tk.Button(text="8", width=ancho, height=alto, command=lambda:clic("8"))
- b.place(x=107, y=320)
- b = tk.Button(text="9", width=ancho, height=alto, command=lambda:clic("9"))
- b.place(x=197, y=320)
- b = tk.Button(text="x", width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic("*"))
- b.place(x=287, y=320)
- #### cuarta fila: ( 0 ) /
- b = tk.Button(text="(", width=ancho, height=alto, bg="sky blue", command=lambda:clic("("))
- b.place(x=17, y=380)
- b = tk.Button(text="0", width=ancho, height=alto, command=lambda:clic("0"))
- b.place(x=107, y=380)
- b = tk.Button(text=")", width=ancho, height=alto, bg="sky blue", command=lambda:clic(")"))
- b.place(x=197, y=380)
- b = tk.Button(text="/", width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic("/"))
- b.place(x=287, y=380)
- ##### quinta fila: raiz coma decimal potencia resto
- b = tk.Button(text="\u221A", width=ancho, height=alto, bg="sky blue", command=lambda:clic("sqrt("))
- b.place(x=17, y=440)
- b = tk.Button(text=".", width=ancho, height=alto, command=lambda:clic("."))
- b.place(x=107, y=440)
- b = tk.Button(text="Pot", width=ancho, height=alto, bg="sky blue", command=lambda:clic("**"))
- b.place(x=197, y=440)
- b = tk.Button(text="%", width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic("%"))
- b.place(x=287, y=440)
- #### sexta fila: Clear Factorial pi =
- b = tk.Button(text="CL", width=ancho, height=alto, bg="medium aquamarine", command=limpieza)
- b.place(x=17, y=500)
- b = tk.Button(text="!", width=ancho, height=alto, command=lambda:clic("factorial("))
- b.place(x=107, y=500)
- b = tk.Button(text="\u03C0", width=ancho, height=alto, bg="sky blue", command=lambda:clic(str(pi)))
- b.place(x=197, y=500)
- b = tk.Button(text="=", width=ancho, height=alto, bg="medium aquamarine", command=hacer_cuenta)
- b.place(x=287, y=500)
- ventana.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment