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
- def limpieza():
- global operador
- ingresa_texto.set("0")
- operador = ""
- def clic(tecla):
- global operador # el mismo operador de la función principal
- operador = operador + str(tecla)
- ingresa_texto.set(operador)
- def operacion():
- global operador
- try:
- resultado = str(eval(operador))
- except Exception:
- limpieza()
- resultado = "ERROR"
- finally:
- ingresa_texto.set(resultado)
- # Ventana de la calculadora
- ventana = tk.Tk()
- ventana.title("CALCULADORA")
- ventana.config(
- width = 390,
- height = 600,
- bg = "Light Steel Blue"
- )
- ventana.resizable(0, 0)
- # variable que se muestra en pantalla
- ingresa_texto = tk.StringVar()
- # tamaño de las teclas
- ancho = 10
- alto = 3
- # inicializo la calculadora
- limpieza()
- ##### pantalla ########
- pantalla = tk.Entry(
- font = ["arial", 20, "bold"],
- width = 22,
- textvariable = ingresa_texto,
- bd = 20, # dimension del borde de la pantalla
- bg = "powder blue",
- justify = "right",
- state = tk.DISABLED
- )
- pantalla.place(x=10, y=60)
- ### 1ª fila de teclas : 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="+",width=ancho,height=alto,bg="white",command=lambda:clic('+'))
- botonSuma.place(x=287,y=180)
- ### 2ª fila de teclas : 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="-",width=ancho,height=alto,bg="white",command=lambda:clic('-'))
- botonResta.place(x=287,y=240)
- ### 3ª fila de teclas : 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)
- botonMult = tk.Button(text="x",width=ancho,height=alto,bg="white",command=lambda:clic('*'))
- botonMult.place(x=287,y=300)
- ### 4ª fila de teclas : ( 0 ) / #############
- botonParenIzq = tk.Button(text="(",width=ancho,height=alto,bg="gainsboro",command=lambda:clic('('))
- botonParenIzq.place(x=17,y=360)
- botonCero = tk.Button(text="0",width=ancho,height=alto,command=lambda:clic(0))
- botonCero.place(x=107,y=360)
- botonParenDer = tk.Button(text=")",width=ancho,height=alto,bg="gainsboro",command=lambda:clic(')'))
- botonParenDer.place(x=197,y=360)
- botonDiv = tk.Button(text="/",width=ancho,height=alto,bg="white",command=lambda:clic('/'))
- botonDiv.place(x=287,y=360)
- ### 5ª fila de teclas : Raiz, coma, potencia, % #############
- botonRaiz = tk.Button(text="RAIZ",width=ancho,height=alto,bg="gainsboro",command=lambda:clic('sqrt('))
- botonRaiz.place(x=17,y=420)
- botonComa = tk.Button(text=".",width=ancho,height=alto,command=lambda:clic('.'))
- botonComa.place(x=107,y=420)
- botonPot = tk.Button(text="POWER",width=ancho,height=alto,bg="gainsboro",command=lambda:clic('**'))
- botonPot.place(x=197,y=420)
- botonResto = tk.Button(text="%",width=ancho,height=alto,bg="gainsboro",command=lambda:clic('%'))
- botonResto.place(x=287,y=420)
- ### 6ª fila de teclas : CLEAR, Factorial, PI, = #############
- botonClear = tk.Button(text="CL",width=ancho,height=alto,bg="gainsboro",command=limpieza)
- botonClear.place(x=17,y=480)
- botonFact = tk.Button(text="!",width=ancho,height=alto,bg="gainsboro",command=lambda:clic('factorial('))
- botonFact.place(x=107,y=480)
- botonPI = tk.Button(text="PI",width=ancho,height=alto,bg="gainsboro",command=lambda:clic(pi))
- botonPI.place(x=197,y=480)
- botonIgual = tk.Button(text="=",width=ancho,height=alto,bg="gainsboro",command=operacion)
- botonIgual.place(x=287,y=480)
- ventana.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment