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"""
- import tkinter as tk
- from math import factorial, sqrt, pi
- # creo la ventana
- ventana = tk.Tk()
- ventana.title("Calculadora")
- ventana.config(
- width = 390,
- height = 600,
- bg = "Light Steel Blue"
- )
- ventana.resizable(0, 0)
- # funciones
- def clic(tecla):
- '''Función que almacena los clics de cada tecla'''
- global operacion
- operacion = operacion + str(tecla)
- calculo.set(operacion)
- def limpieza():
- '''Función que limpia la pantalla'''
- global operacion # uso la variable operacion del prog. principal
- operacion = "" # borro lo escrito en la pantalla
- calculo.set("0")
- def calcular():
- '''Efectua la operacion mostrada en la pantalla
- a traves de la función eval cuando es llamada por clic("=")'''
- global operacion
- try:
- total = str(eval(operacion))
- except:
- total = "ERROR"
- calculo.set(total)
- # definimos el tamaño de las teclas
- ancho_tecla = 11
- alto_tecla = 3
- # creacion de la variable que muestra el cálculo a
- # realizar en la pantalla de la calculadora
- calculo = tk.StringVar()
- # variable que almacena la operación matemàtica a realizar
- operacion = ""
- # Inicio la calculadora poniendo un cero en pantalla
- limpieza()
- ################# Layout de la calculadora #######################
- # pantalla de la calculadora
- pantalla = tk.Entry(
- font = ('arial', 20, 'bold'),
- width = 22,
- bd = 20, # tamaño del borde
- bg = "powder blue",
- justify = "right",
- state = tk.DISABLED,
- textvariable = calculo
- )
- pantalla.place(x=10, y=60)
- # primera fila de teclas: 1 2 3 +
- boton1 = tk.Button(text='1', width=ancho_tecla, height=alto_tecla, command=lambda:clic(1))
- boton1.place(x=17, y=180)
- boton2 = tk.Button(text='2', width=ancho_tecla, height=alto_tecla, command=lambda:clic(2))
- boton2.place(x=107, y=180)
- boton3 = tk.Button(text='3', width=ancho_tecla, height=alto_tecla, command=lambda:clic(3))
- boton3.place(x=197, y=180)
- botonSuma = tk.Button(text='+', width=ancho_tecla, height=alto_tecla, bg="gainsboro", command=lambda:clic('+'))
- botonSuma.place(x=287, y=180)
- # segunda fila de teclas: 4 5 6 -
- boton4 = tk.Button(text='4', width=ancho_tecla, height=alto_tecla, command=lambda:clic(4))
- boton4.place(x=17, y=240)
- boton5 = tk.Button(text='5', width=ancho_tecla, height=alto_tecla, command=lambda:clic(5))
- boton5.place(x=107, y=240)
- boton6 = tk.Button(text='6', width=ancho_tecla, height=alto_tecla, command=lambda:clic(6))
- boton6.place(x=197, y=240)
- botonResta = tk.Button(text='-', width=ancho_tecla, height=alto_tecla, bg="gainsboro", command=lambda:clic('-'))
- botonResta.place(x=287, y=240)
- # tercera fila de teclas: 7 8 9 x
- boton7 = tk.Button(text='7', width=ancho_tecla, height=alto_tecla, command=lambda:clic(7))
- boton7.place(x=17, y=300)
- boton8 = tk.Button(text='8', width=ancho_tecla, height=alto_tecla, command=lambda:clic(8))
- boton8.place(x=107, y=300)
- boton9 = tk.Button(text='9', width=ancho_tecla, height=alto_tecla, command=lambda:clic(9))
- boton9.place(x=197, y=300)
- botonMult = tk.Button(text='x', width=ancho_tecla, height=alto_tecla, bg="gainsboro", command=lambda:clic('*'))
- botonMult.place(x=287, y=300)
- # cuarta fila de teclas: ( 0 ) /
- botonParenDer = tk.Button(text='(', width=ancho_tecla, height=alto_tecla, bg="gainsboro", command=lambda:clic('('))
- botonParenDer.place(x=17, y=360)
- boton0 = tk.Button(text='0', width=ancho_tecla, height=alto_tecla, command=lambda:clic(0))
- boton0.place(x=107, y=360)
- botonParenIzq = tk.Button(text=')', width=ancho_tecla, height=alto_tecla, bg="gainsboro", command=lambda:clic(')'))
- botonParenIzq.place(x=197, y=360)
- botonDiv = tk.Button(text='/', width=ancho_tecla, height=alto_tecla, bg="gainsboro", command=lambda:clic('/'))
- botonDiv.place(x=287, y=360)
- # quinta fila de teclas: Raiz coma decimal potencia %
- botonRaiz = tk.Button(text='RAIZ', width=ancho_tecla, height=alto_tecla, bg="gainsboro", command=lambda:clic('sqrt('))
- botonRaiz.place(x=17, y=420)
- botonComa = tk.Button(text='.', width=ancho_tecla, height=alto_tecla, bg="gainsboro", command=lambda:clic('.'))
- botonComa.place(x=107, y=420)
- botonPower = tk.Button(text='POWER', width=ancho_tecla, height=alto_tecla, bg="gainsboro", command=lambda:clic('**'))
- botonPower.place(x=197, y=420)
- botonResto = tk.Button(text='%', width=ancho_tecla, height=alto_tecla, bg="gainsboro", command=lambda:clic('%'))
- botonResto.place(x=287, y=420)
- # sexta fila de teclas: Clear Factorial PI =
- botonClear = tk.Button(text='CLEAR', width=ancho_tecla, height=alto_tecla, bg="cadet blue", command=limpieza)
- botonClear.place(x=17, y=480)
- botonFact = tk.Button(text='!', width=ancho_tecla, height=alto_tecla, bg="gainsboro", command=lambda:clic('factorial('))
- botonFact.place(x=107, y=480)
- botonPi = tk.Button(text='Pi', width=ancho_tecla, height=alto_tecla, bg="gainsboro", command=lambda:clic('pi'))
- botonPi.place(x=197, y=480)
- botonIgual = tk.Button(text='=', width=ancho_tecla, height=alto_tecla, bg="cadet blue", command=calcular)
- botonIgual.place(x=287, y=480)
- ventana.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment