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
- # Ventana que contiene a la calculadora
- ventana = tk.Tk()
- ventana.title("CALCULADORA")
- ventana.config(
- width = 392,
- height = 600,
- bg = "Light Steel Blue"
- )
- ventana.resizable(0, 0)
- # función que almacena los clics en cada tecla
- def clic(tecla):
- global operador
- operador = operador + str(tecla) # op = "1*2"
- ingresa_texto.set(operador) #
- # Función para limpieza
- def limpieza():
- global operador
- ingresa_texto.set("0")
- operador = ""
- # Función que efectúa la operación
- # eval evalúa una cadena como un sentencia de python
- # print(eval("2+2") arroja 4
- # También se manejan excepciones:
- # try realiza el cálculo si no hay errores
- # en caso contrario se ejecuta except
- def operacion():
- global operador
- try:
- valor_operacion = str(eval(operador))
- except Exception:
- limpieza()
- valor_operacion = "ERROR"
- finally:
- ingresa_texto.set(valor_operacion)
- ################### Programa principal ##########################
- # Definimos las dimensiones de las teclas
- ancho = 9
- alto = 3
- # variable que almacena la operación a realizar (por ejemplo, 1+2)
- operador = "" # "12.35+89/58"
- # Creación de una cadena en Tkinter
- ingresa_texto = tk.StringVar() # Hola"
- # Limpia todo antes de empezar un cálculo
- limpieza()
- # Creamos nuestros botones
- # La función lambda define funciones inline (en una línea)
- # primera fila: 1,2,3,+
- # Por ejemplo:
- # def suma(x,y):
- # return x+y
- # Se escribe en notación lambda como:
- # b = lambda x, y : x + y
- # primera fila
- boton1 = tk.Button(
- ventana,
- text = '1',
- width = ancho, height = alto,
- command = lambda:clic(1)
- )
- boton1.place(x = 17, y = 180)
- boton2 = tk.Button(
- ventana,
- text = '2',
- width = ancho, height = alto,
- command = lambda:clic(2)
- )
- boton2.place(x = 107, y = 180)
- boton3 = tk.Button(
- ventana,
- text = '3',
- width = ancho, height = alto,
- command = lambda:clic(3)
- )
- boton3.place(x = 197, y = 180)
- botonSuma = tk.Button(
- ventana,
- text = '+',
- width = ancho, height = alto,
- bg = "white",
- command = lambda:clic('+')
- )
- botonSuma.place(x = 287, y = 180)
- # segunda fila; 4,5,6,-
- boton4 = tk.Button(
- ventana,
- text = '4',
- width = ancho, height = alto,
- command = lambda:clic(4)
- )
- boton4.place(x = 17, y = 240)
- boton5 = tk.Button(
- ventana,
- text = '5',
- width = ancho, height = alto,
- command = lambda:clic(5)
- )
- boton5.place(x = 107, y = 240)
- boton6 = tk.Button(
- ventana,
- text = '6',
- width = ancho, height = alto,
- command=lambda:clic(6)
- )
- boton6.place(x = 197, y = 240)
- botonResta = tk.Button(
- ventana,
- text = '-',
- width = ancho, height = alto,
- bg = "white",
- command = lambda:clic('-')
- )
- botonResta.place(x = 287, y = 240)
- # tercera fila; 7,8,9,x
- boton7 = tk.Button(
- ventana,
- text = '7',
- width = ancho, height = alto,
- command = lambda:clic(7)
- )
- boton7.place(x = 17, y = 300)
- boton8 = tk.Button(
- ventana,
- text = '8',
- width = ancho, height = alto,
- command = lambda:clic(8)
- )
- boton8.place(x = 107, y = 300)
- boton9 = tk.Button(
- ventana,
- text = '9',
- width = ancho, height = alto,
- command = lambda:clic(9)
- )
- boton9.place(x = 197, y = 300)
- botonMult = tk.Button(
- ventana,
- text = 'x',
- width = ancho, height = alto,
- bg = "white",
- command = lambda:clic('*')
- )
- botonMult.place(x = 287, y = 300)
- # cuarta fila: (,0,),/
- botonParenIzq = tk.Button(
- ventana,
- text = '(',
- width = ancho, height = alto,
- bg = "gainsboro",
- command = lambda:clic('(')
- )
- botonParenIzq.place(x = 17, y = 360)
- boton0 = tk.Button(
- ventana,
- text = '0',
- width = ancho, height = alto,
- command = lambda:clic(0)
- )
- boton0.place(x = 107, y = 360)
- botonParenDer = tk.Button(
- ventana,
- text = ')',
- width = ancho, height = alto,
- bg = "gainsboro",
- command = lambda:clic(')')
- )
- botonParenDer.place(x = 197, y = 360)
- botonDiv = tk.Button(
- ventana,
- text = '/',
- width = ancho, height = alto,
- bg = "white",
- command = lambda:clic('/')
- )
- botonDiv.place(x = 287, y = 360)
- # quinta fila: Raiz,coma decimal,Potencia,%
- botonRaiz = tk.Button(
- ventana,
- text = 'RAIZ',
- width = ancho, height = alto,
- bg = "gainsboro",
- command = lambda:clic('sqrt(')
- )
- botonRaiz.place(x = 17, y = 420)
- botonComa = tk.Button(
- ventana,
- text = '.',
- width = ancho, height = alto,
- command = lambda:clic('.')
- )
- botonComa.place(x = 107, y = 420)
- botonPotencia = tk.Button(
- ventana,
- text = 'POWER',
- width = ancho, height = alto,
- bg = "gainsboro",
- command = lambda:clic('**')
- )
- botonPotencia.place(x = 197, y = 420)
- botonResto = tk.Button(
- ventana,
- text = '%',
- width = ancho, height = alto,
- bg = "gainsboro",
- command = lambda:clic('%')
- )
- botonResto.place(x = 287, y = 420)
- # sexta fila: Clear,Exp,PI,=
- botonClear = tk.Button(
- ventana,
- text = 'CLEAR',
- width = ancho, height = alto,
- bg = "cadet blue",
- command = limpieza
- )
- botonClear.place(x = 17, y = 480)
- botonFact = tk.Button(
- ventana,
- text = '!',
- width = ancho, height = alto,
- bg = "gainsboro",
- command = lambda:clic('factorial(')
- )
- botonFact.place(x = 107, y = 480)
- botonPi = tk.Button(
- ventana,
- text = 'PI',
- width = ancho, height = alto,
- bg = "gainsboro",
- command = lambda:clic(pi)
- )
- botonPi.place(x = 197, y = 480)
- botonIgual = tk.Button(
- ventana,
- text = '=',
- width = ancho, height = alto,
- bg = "cadet blue",
- command = operacion
- )
- botonIgual.place(x = 287, y = 480)
- # Creamos la pantalla de nuestra calculadora
- # font formatea el tipo y tamaño de los caracteres
- # width y bd el ancho y tamaño del borde de la pantalla
- # bg el color de fondo, justify la alineación
- # tk.DISABLED impide el ingreso de datos manualmente en la pantalla
- pantalla = tk.Entry(
- font = ('arial',20,'bold'),
- width = 22,
- textvariable = ingresa_texto,
- bd = 18,
- bg = "powder blue",
- justify = "right",
- state = tk.DISABLED
- )
- pantalla.place(x = 10, y = 60)
- ventana.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement