Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- #
- # calculadora
- #
- import tkinter as tk
- from math import sqrt, pi, factorial
- def clic(tecla):
- global operador
- operador = operador + str(tecla)
- ingresa_texto.set(operador)
- def limpieza():
- global operador
- ingresa_texto.set("0")
- operador = ""
- def hacer_cuenta():
- global operador
- try:
- total = str(eval(operador))
- except:
- limpieza()
- total = "ERROR"
- ingresa_texto.set(total)
- ######## programa principal ################
- ventana = tk.Tk()
- ventana.title("CALCULADORA")
- ventana.config(width=390, height=600, bg="Light Steel Blue")
- ventana.resizable(0,0)
- # tamaño de las teclas
- ancho = 9
- alto = 3
- # variable que almacena la cuenta a realizar
- operador = ""
- # variable para mostrar en la pantalla
- ingresa_texto = tk.StringVar()
- limpieza()
- # pantalla
- pantalla = tk.Entry(
- font = ["arial", 20, "bold"],
- width = 22, # ancho borde
- bd = 15, # grosor borde
- textvariable = ingresa_texto,
- justify = "right",
- bg = "powder blue",
- state = tk.DISABLED
- )
- pantalla.place(x=10, y=60)
- # primera fila de teclas: 1 2 3 +
- boton = tk.Button(text="1", width=ancho, height=alto, command=lambda:clic(1))
- boton.place(x=17, y=180)
- boton = tk.Button(text="2", width=ancho, height=alto, command=lambda:clic(2))
- boton.place(x=107, y=180)
- boton = tk.Button(text="3", width=ancho, height=alto, command=lambda:clic(3))
- boton.place(x=197, y=180)
- boton = tk.Button(text="+", width=ancho, height=alto, bg="sky blue", command=lambda:clic("+"))
- boton.place(x=287, y=180)
- # segunda fila de teclas: 4 5 6 -
- boton = tk.Button(text="4", width=ancho, height=alto, command=lambda:clic(4))
- boton.place(x=17, y=240)
- boton = tk.Button(text="5", width=ancho, height=alto, command=lambda:clic(5))
- boton.place(x=107, y=240)
- boton = tk.Button(text="6", width=ancho, height=alto, command=lambda:clic(6))
- boton.place(x=197, y=240)
- boton = tk.Button(text="-", width=ancho, height=alto, bg="sky blue", command=lambda:clic("-"))
- boton.place(x=287, y=240)
- # tercera fila de teclas: 7 8 9 x
- boton = tk.Button(text="7", width=ancho, height=alto, command=lambda:clic(7))
- boton.place(x=17, y=300)
- boton = tk.Button(text="8", width=ancho, height=alto, command=lambda:clic(8))
- boton.place(x=107, y=300)
- boton = tk.Button(text="9", width=ancho, height=alto, command=lambda:clic(9))
- boton.place(x=197, y=300)
- boton = tk.Button(text="x", width=ancho, height=alto, bg="sky blue", command=lambda:clic("*"))
- boton.place(x=287, y=300)
- # cuarta fila de teclas: ( 0 ) /
- boton = tk.Button(text="(", width=ancho, height=alto, bg="aquamarine", command=lambda:clic("("))
- boton.place(x=17, y=360)
- boton = tk.Button(text="0", width=ancho, height=alto, command=lambda:clic(0))
- boton.place(x=107, y=360)
- boton = tk.Button(text=")", width=ancho, height=alto, bg="aquamarine", command=lambda:clic(")"))
- boton.place(x=197, y=360)
- boton = tk.Button(text="/", width=ancho, height=alto, bg="sky blue", command=lambda:clic("/"))
- boton.place(x=287, y=360)
- # quinta fila de teclas: RAIZ Punto decimal Potencia %
- boton = tk.Button(text="RAIZ", width=ancho, height=alto, bg="aquamarine", command=lambda:clic("sqrt("))
- boton.place(x=17, y=420)
- boton = tk.Button(text=".", width=ancho, height=alto, command=lambda:clic("."))
- boton.place(x=107, y=420)
- boton = tk.Button(text="POT", width=ancho, height=alto, bg="aquamarine", command=lambda:clic("**"))
- boton.place(x=197, y=420)
- boton = tk.Button(text="%", width=ancho, height=alto, bg="sky blue", command=lambda:clic("%"))
- boton.place(x=287, y=420)
- # sexta fila de teclas: Clear Factorial PI =
- boton = tk.Button(text="CL", width=ancho, height=alto, bg="aquamarine", command=limpieza)
- boton.place(x=17, y=480)
- boton = tk.Button(text="!", width=ancho, height=alto, bg="aquamarine", command=lambda:clic("factorial("))
- boton.place(x=107, y=480)
- boton = tk.Button(text="PI", width=ancho, height=alto, bg="aquamarine", command=lambda:clic(pi))
- boton.place(x=197, y=480)
- boton = tk.Button(text="=", width=ancho, height=alto, bg="sky blue", command= hacer_cuenta)
- boton.place(x=287, y=480)
- ventana.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment