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 escribe cada tecla en la pantalla"""
- global operacion
- operacion = operacion + tecla
- # ahora lo muestro en pantalla
- calculo.set(operacion)
- def limpieza():
- """Función que limpia la pantalla"""
- global operacion
- operacion = "" # borro la cuenta del lado de la consola
- calculo.set("0") # borro la pantalla y pongo un cero
- def hacer_cuenta():
- """Función que realiza el cálculo y lo imprime en la pantalla"""
- global operacion
- try:
- total = str(eval(operacion))
- except:
- limpieza()
- total = "ERROR"
- calculo.set(total)
- def borrar_caracter():
- """Borra el último caracter ingresado"""
- global operacion
- lista = []
- if operacion:
- # relleno la lista con los caracteres de la cuenta
- for caracter in operacion:
- lista.append(caracter)
- # borro el último caracter
- del lista[-1]
- # rearmo la cuenta a partir de la lista
- operacion = "".join(lista)
- # muestro la operación por pantalla
- calculo.set(operacion)
- ######### Lado consola ##############################
- # creo una variable para almacenar el cálculo del lado de la consola
- operacion = ""
- ############# Lado gráfico ###################################
- ventana = tk.Tk()
- ventana.title("Calculadora ACME")
- ventana.config(width=400, height=600, bg="Light Steel Blue")
- ventana.resizable(0,0)
- # creo una variable de cadena en entorno gráfico para almacenar el
- # cálculo que debo mostrar en pantalla
- calculo = tk.StringVar()
- # al iniciar la calculadora, debo mostrar un cero en pantalla
- limpieza()
- #### pantalla #####
- pantalla = tk.Entry(
- font = ('arial', 20, 'bold'),
- width = 20,
- bd = 20,
- bg = "powder blue",
- justify = "right",
- state = tk.DISABLED,
- textvariable = calculo
- )
- pantalla.place(x=25, y=50)
- #### teclado #####
- # defino las dimensiones de las teclas
- ancho = 9
- alto = 2
- grosor = 3
- # tecla para borrar un caracter
- tecla = tk.Button(text='<<', bd=grosor, width=ancho, height=alto, bg="medium aquamarine", command=borrar_caracter)
- tecla.place(x=295, y=140)
- # 1º fila de teclas: 1 2 3 +
- tecla = tk.Button(text='1', bd=grosor, width=ancho, height=alto, command=lambda:clic('1'))
- tecla.place(x=25, y=200)
- tecla = tk.Button(text='2', bd=grosor, width=ancho, height=alto, command=lambda:clic('2'))
- tecla.place(x=115, y=200)
- tecla = tk.Button(text='3', bd=grosor, width=ancho, height=alto, command=lambda:clic('3'))
- tecla.place(x=205, y=200)
- tecla = tk.Button(text='+', bd=grosor, width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic('+'))
- tecla.place(x=295, y=200)
- # 2º fila de teclas: 4 5 6 -
- tecla = tk.Button(text='4', bd=grosor, width=ancho, height=alto, command=lambda:clic('4'))
- tecla.place(x=25, y=260)
- tecla = tk.Button(text='5', bd=grosor, width=ancho, height=alto, command=lambda:clic('5'))
- tecla.place(x=115, y=260)
- tecla = tk.Button(text='6', bd=grosor, width=ancho, height=alto, command=lambda:clic('6'))
- tecla.place(x=205, y=260)
- tecla = tk.Button(text='-', bd=grosor, width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic('-'))
- tecla.place(x=295, y=260)
- # 3º fila de teclas: 7 8 9 x
- tecla = tk.Button(text='7', bd=grosor, width=ancho, height=alto, command=lambda:clic('7'))
- tecla.place(x=25, y=320)
- tecla = tk.Button(text='8', bd=grosor, width=ancho, height=alto, command=lambda:clic('8'))
- tecla.place(x=115, y=320)
- tecla = tk.Button(text='9', bd=grosor, width=ancho, height=alto, command=lambda:clic('9'))
- tecla.place(x=205, y=320)
- tecla = tk.Button(text='x', bd=grosor, width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic('x'))
- tecla.place(x=295, y=320)
- # 4º fila de teclas: ( 0 ) /
- tecla = tk.Button(text='(', bd=grosor, width=ancho, height=alto, bg="sky blue", command=lambda:clic('('))
- tecla.place(x=25, y=380)
- tecla = tk.Button(text='0', bd=grosor, width=ancho, height=alto, command=lambda:clic('0'))
- tecla.place(x=115, y=380)
- tecla = tk.Button(text=')', bd=grosor, width=ancho, height=alto, bg="sky blue", command=lambda:clic(')'))
- tecla.place(x=205, y=380)
- tecla = tk.Button(text='/', bd=grosor, width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic('/'))
- tecla.place(x=295, y=380)
- # 5º fila de teclas: Raiz coma potencia resto
- tecla = tk.Button(text='\u221A', bd=grosor, width=ancho, height=alto, bg="sky blue", command=lambda:clic('sqrt('))
- tecla.place(x=25, y=440)
- tecla = tk.Button(text='.', bd=grosor, width=ancho, height=alto, command=lambda:clic('.'))
- tecla.place(x=115, y=440)
- tecla = tk.Button(text='^', bd=grosor, width=ancho, height=alto, bg="sky blue", command=lambda:clic('**'))
- tecla.place(x=205, y=440)
- tecla = tk.Button(text='%', bd=grosor, width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic('%'))
- tecla.place(x=295, y=440)
- # 6º fila de teclas: Clear Factorial PI =
- tecla = tk.Button(text='CL', bd=grosor, width=ancho, height=alto, bg="sky blue", command=limpieza)
- tecla.place(x=25, y=500)
- tecla = tk.Button(text='!', bd=grosor, width=ancho, height=alto, bg="sky blue", command=lambda:clic('factorial('))
- tecla.place(x=115, y=500)
- tecla = tk.Button(text='\u03C0', bd=grosor, width=ancho, height=alto, bg="sky blue", command=lambda:clic(str(pi)))
- tecla.place(x=205, y=500)
- tecla = tk.Button(text='=', bd=grosor, width=ancho, height=alto, bg="medium aquamarine", command=hacer_cuenta)
- tecla.place(x=295, y=500)
- ventana.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement