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
- # defino una función limpieza para borrar el contenido del cálculo
- def limpieza():
- global operacion
- calculo.set("0") # pone un cero en la pantalla
- operacion = "" # borra la cuenta de la consola
- # defino la función que almacena los clics de cada tecla
- def clic(tecla):
- global operacion
- operacion = operacion + tecla
- calculo.set(operacion)
- def hacer_cuenta():
- global operacion
- try:
- total = str(eval(operacion))
- except Exception:
- limpieza()
- total = "ERROR"
- calculo.set(total)
- operacion = total
- def borrar_caracter():
- global operacion
- lista = []
- # creo una lista con los valores de operacion
- for i in range(len(operacion)):
- lista.append(operacion[i])
- # borro el último caracter
- lista = lista[:-1]
- # rearmo la cadena sin el último caracter a partir de la lista
- operacion = "".join(lista)
- # muestro la cadena en pantalla
- 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)
- # Creamos una variable para mostrar el cálculo en la pantalla
- calculo = tk.StringVar()
- # llamo a la funcion limpieza para inicializar la calculadora
- limpieza()
- # creamos una 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 = 20, y = 50)
- # defino las dimensiones de las teclas
- ancho = 10
- alto = 2
- # creo una variable para almacenar el cálculo del lado de consola
- operacion = ""
- ############ BOTONES ##################
- #### Primera fila: 1 2 3 +
- boton1 = tk.Button(text='1', width=ancho, height=alto, command=lambda:clic("1"))
- boton1.place(x=17, y=180)
- boton2 = tk.Button(text='2', width=ancho, height=alto, command=lambda:clic("2"))
- boton2.place(x=107, y=180)
- boton3 = tk.Button(text='3', width=ancho, height=alto, command=lambda:clic("3"))
- boton3.place(x=197, y=180)
- boton_Suma = tk.Button(text='+', width=ancho, height=alto, bg="steel blue2", command=lambda:clic("+"))
- boton_Suma.place(x=287, y=180)
- #### Segunda fila: 4 5 6 -
- boton4 = tk.Button(text='4', width=ancho, height=alto, command=lambda:clic("4"))
- boton4.place(x=17, y=240)
- boton5 = tk.Button(text='5', width=ancho, height=alto, command=lambda:clic("5"))
- boton5.place(x=107, y=240)
- boton6 = tk.Button(text='6', width=ancho, height=alto, command=lambda:clic("6"))
- boton6.place(x=197, y=240)
- boton_Resta = tk.Button(text='-', width=ancho, height=alto, bg="steel blue2", command=lambda:clic("-"))
- boton_Resta.place(x=287, y=240)
- #### Tercera fila: 7 8 9 x
- boton7 = tk.Button(text='7', width=ancho, height=alto, command=lambda:clic("7"))
- boton7.place(x=17, y=300)
- boton8 = tk.Button(text='8', width=ancho, height=alto, command=lambda:clic("8"))
- boton8.place(x=107, y=300)
- boton9 = tk.Button(text='9', width=ancho, height=alto)
- boton9.place(x=197, y=300)
- boton_Por = tk.Button(text='x', width=ancho, height=alto, bg="steel blue2", command=lambda:clic("*"))
- boton_Por.place(x=287, y=300)
- #### Cuarta fila: ( 0 ) /
- boton_par_izq = tk.Button(text='(', width=ancho, height=alto, bg="sky blue", command=lambda:clic("("))
- boton_par_izq.place(x=17, y=360)
- boton0 = tk.Button(text='0', width=ancho, height=alto, command=lambda:clic("0"))
- boton0.place(x=107, y=360)
- boton_par_der = tk.Button(text=')', width=ancho, height=alto, bg="sky blue", command=lambda:clic(")"))
- boton_par_der.place(x=197, y=360)
- boton_Division = tk.Button(text='/', width=ancho, height=alto, bg="steel blue2", command=lambda:clic("/"))
- boton_Division.place(x=287, y=360)
- #### Quinta fila: Raiz, coma decimal, potencia, resto
- boton_raiz = tk.Button(text='RAIZ', width=ancho, height=alto, bg="sky blue", command=lambda:clic("sqrt("))
- boton_raiz.place(x=17, y=420)
- boton_coma = tk.Button(text='.', width=ancho, height=alto, command=lambda:clic("."))
- boton_coma.place(x=107, y=420)
- boton_potencia = tk.Button(text='POWER', width=ancho, height=alto, bg="sky blue", command=lambda:clic("**"))
- boton_potencia.place(x=197, y=420)
- boton_resto = tk.Button(text='%', width=ancho, height=alto, bg="steel blue2", command=lambda:clic("%"))
- boton_resto.place(x=287, y=420)
- #### Sexta fila: Clear, Factorial PI =
- boton_clear = tk.Button(text='CL', width=ancho, height=alto, bg="medium aquamarine", command=limpieza)
- boton_clear.place(x=17, y=480)
- boton_factorial = tk.Button(text='!', width=ancho, height=alto, command=lambda:clic("factorial("))
- boton_factorial.place(x=107, y=480)
- boton_pi = tk.Button(text='PI', width=ancho, height=alto, bg="sky blue", command=lambda:clic(str(pi)))
- boton_pi.place(x=197, y=480)
- boton_igual = tk.Button(text='=', width=ancho, height=alto, bg="medium aquamarine", command=hacer_cuenta)
- boton_igual.place(x=287, y=480)
- # botón para borrar un caracter
- boton_del = tk.Button(text='DEL', width=ancho, height=alto, bg="medium aquamarine", command=borrar_caracter)
- boton_del.place(x=17, y=540)
- ventana.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement