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, factorial, pi
- ################## Funciones ###############################
- def clic(tecla):
- """Función que almacena los clics de una tecla"""
- global operacion
- operacion = operacion + tecla # lado consola
- calculo.set(operacion) # lado gráfico
- def limpieza():
- """Función que limpia la pantalla borrando todo su contenido
- y mostrando un cero en su lugar"""
- global operacion
- operacion = "" # borro la cuenta de la consola
- calculo.set("0") # borro la cuenta de la pantalla
- def hacer_cuenta():
- """Función que realiza el cálculo ingresado en la pantalla de
- la calculadora y lo muestra en ella"""
- global operacion
- try:
- resultado = str(eval(operacion))
- except Exception:
- limpieza()
- resultado = "ERROR"
- calculo.set(resultado)
- def borrar_caracter():
- """Función que borra el último caracter ingresado en la pantalla"""
- global operacion
- # creo una lista para guardar la secuencia de teclas presionadas
- lista = []
- # guardo la secuencia tecla a tecla en la lista
- for caracter in operacion:
- lista.append(caracter)
- # borro el último caracter de la lista
- lista = lista[:-1]
- # rearmo la cadena de la operación sin el último caracter
- operacion = "".join(lista)
- # muestro en pantalla
- calculo.set(operacion)
- #######################################################################
- # defino las dimensiones de las teclas
- ancho = 10
- alto = 2
- # creo una variable de consola para almacenar la cuenta a realizar
- 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)
- # creo la variable gráfica 'calculo' para almacenar la cuenta a realizar
- calculo = tk.StringVar()
- # creamos una pantalla
- pantalla = tk.Entry(
- font = ['arial',20,'bold'],
- width = 20,
- bd = 20, # grosor del borde de la ventana
- bg = 'powder blue',
- justify = 'right',
- state = tk.DISABLED, # inhabilita el ingreso de datos
- textvariable = calculo # variable a mostrar (calculo)
- )
- pantalla.place(x=20, y=50)
- ##### Botones de la calculadora
- # boton para borrar el último caracter ingresado en pantalla
- 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="SteelBlue2", 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="SteelBlue2", 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="SteelBlue2", 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="SteelBlue2", command=lambda:clic("/"))
- boton.place(x=287, y=380)
- # quinta fila: raiz, coma decimal potencia resto
- boton = tk.Button(text="RAIZ", 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="POWER", 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=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="PI", 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)
- # Lo primero que hago es llamar a limpieza
- limpieza()
- ventana.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement