Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- #
- """
- Programa que implementa una calculadora científica básica
- """
- import tkinter as tk
- from math import factorial, sqrt, pi
- def clic(tecla):
- '''Funcion que almacena los clics de cada tecla'''
- global cuenta
- cuenta = cuenta + str(tecla) #cuenta = "1+2" eval(1+2) --> 3
- resultado.set(cuenta)
- def limpieza():
- global cuenta
- cuenta = ""
- resultado.set("0")
- def hacer_cuenta():
- global cuenta
- try:
- total = str(eval(cuenta))
- except:
- limpieza()
- total = "ERROR"
- resultado.set(total)
- ###### programa principal #############################
- ventana = tk.Tk()
- ventana.config(width=392, height=600, bg="light steel blue")
- ventana.title("Calculadora")
- ventana.resizable(0,0)
- # variable que almacena una cadena con los clics de cada tecla
- cuenta = ""
- # variable que muestra el resultado por pantalla
- resultado = tk.StringVar()
- resultado.set("0")
- # ancho y alto de los teclas
- ancho = 10
- alto = 3
- # 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)
- botonSuma = tk.Button(text="+", bg="white", width=ancho, height=alto, command=lambda:clic('+'))
- botonSuma.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)
- botonResta = tk.Button(text="-", bg="white", width=ancho, height=alto, command=lambda:clic('-'))
- botonResta.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, command=lambda:clic(9))
- boton9.place(x=197, y=300)
- botonPor = tk.Button(text="x", width=ancho, bg="white", height=alto, command=lambda:clic('*'))
- botonPor.place(x=287, y=300)
- # cuarta fila : ( 0 ) /
- botonParI = tk.Button(text="(", width=ancho, height=alto, command=lambda:clic('('))
- botonParI.place(x=17, y=360)
- botonCero = tk.Button(text="0", width=ancho, height=alto, command=lambda:clic(0))
- botonCero.place(x=107, y=360)
- botonParD = tk.Button(text=")", width=ancho, height=alto, command=lambda:clic(')'))
- botonParD.place(x=197, y=360)
- botonDiv = tk.Button(text="/", bg="white",width=ancho, height=alto, command=lambda:clic('/'))
- botonDiv.place(x=287, y=360)
- # quinta fila : Raiz punto decimal potencia resto
- botonRaiz = tk.Button(text="RAIZ", width=ancho, height=alto, command=lambda:clic('sqrt('))
- botonRaiz.place(x=17, y=420)
- botonPunto = tk.Button(text=".", width=ancho, height=alto, command=lambda:clic('.'))
- botonPunto.place(x=107, y=420)
- botonPot = tk.Button(text="POWER", width=ancho, height=alto, command=lambda:clic('**'))
- botonPot.place(x=197, y=420)
- botonResto = tk.Button(text="%", bg="gainsboro",width=ancho, height=alto, command=lambda:clic('%'))
- botonResto.place(x=287, y=420)
- # sexta fila : clear factorial pi =
- boton1 = tk.Button(text="CLEAR", width=ancho, height=alto, command=limpieza)
- boton1.place(x=17, y=480)
- boton2 = tk.Button(text="!", width=ancho, height=alto, command=lambda:clic('factorial('))
- boton2.place(x=107, y=480)
- boton3 = tk.Button(text="PI", width=ancho, height=alto, command=lambda:clic(pi))
- boton3.place(x=197, y=480)
- botonSuma = tk.Button(text="=", bg="light blue",width=ancho, height=alto, command=hacer_cuenta)
- botonSuma.place(x=287, y=480)
- pantalla = tk.Entry(
- font=['arial',20, 'bold'],
- width=22,
- textvariable = resultado,
- justify ="right",
- bd=20,
- bg="powder blue",
- state=tk.DISABLED
- )
- pantalla.place(x=10, y=60)
- ventana.mainloop()
Add Comment
Please, Sign In to add comment