Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Script que implementa una calculadora científica básica
- import tkinter as tk
- from math import sqrt, factorial, pi
- def clic(tecla):
- global calculo
- calculo = calculo + str(tecla)
- texto_ingresado.set(calculo)
- def limpieza():
- global calculo
- calculo = ""
- texto_ingresado.set("0")
- def calcular():
- global calculo
- try:
- total = str(eval(calculo))
- except Exception:
- total = "ERROR"
- texto_ingresado.set(total)
- # creo la 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 almacena el contenido de la pantalla
- texto_ingresado = tk.StringVar()
- # creo dos variables para almacenar el ancho y alto de los botones
- ancho = 10
- alto = 2
- # creo una variable para almacenar el cálculo con la secuencia
- # de tecleo
- calculo = ""
- limpieza()
- ########## creamos los botones de la calculadora ####################
- # fila 1: 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)
- boton_suma = tk.Button(text="+", width=ancho, height=alto, bg="pale green", command=lambda:clic('+'))
- boton_suma.place(x=287, y=180)
- # fila 2: 1,2,3,+
- 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)
- boton_menos = tk.Button(text="-", width=ancho, height=alto, bg="pale green", command=lambda:clic('-'))
- boton_menos.place(x=287, y=240)
- # fila 3: 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)
- boton_por = tk.Button(text="x", width=ancho, height=alto, bg="pale green", command=lambda:clic('*'))
- boton_por.place(x=287, y=300)
- # fila 4: (,0,),/
- boton_par_izq = tk.Button(text="(", width=ancho, height=alto, bg="pale green", command=lambda:clic('('))
- boton_par_izq.place(x=17, y=360)
- boton0 = tk.Button(text="0", width=ancho, height=alto, command=lambda:clic(0))
- boton0.place(x=107, y=360)
- boton_par_der = tk.Button(text=")", width=ancho, height=alto, bg="pale green", command=lambda:clic(')'))
- boton_par_der.place(x=197, y=360)
- boton_cociente = tk.Button(text="/", width=ancho, height=alto, bg="pale green", command=lambda:clic('/'))
- boton_cociente.place(x=287, y=360)
- # fila 5: raiz,coma decimal,potencia,resto
- boton_raiz = tk.Button(text="RAIZ", width=ancho, height=alto, bg="pale green",command=lambda:clic('sqrt('))
- boton_raiz.place(x=17, y=420)
- boton_punto = tk.Button(text=".", width=ancho, height=alto, command=lambda:clic('.'))
- boton_punto.place(x=107, y=420)
- boton_power = tk.Button(text="POW", width=ancho, height=alto, bg="pale green",command=lambda:clic('**'))
- boton_power.place(x=197, y=420)
- boton_resto = tk.Button(text="%", width=ancho, height=alto, bg="pale green", command=lambda:clic('%'))
- boton_resto.place(x=287, y=420)
- # fila 6: Clear, Factorial, pi, igual
- boton_clear = tk.Button(text="CLEAR", width=ancho, height=alto, bg="SkyBlue3", command=limpieza)
- boton_clear.place(x=17, y=480)
- boton_fact = tk.Button(text="!", width=ancho, height=alto, bg="pale green", command=lambda:clic('factorial('))
- boton_fact.place(x=107, y=480)
- boton_pi = tk.Button(text="PI", width=ancho, height=alto, bg="pale green", command=lambda:clic('pi'))
- boton_pi.place(x=197, y=480)
- boton_igual = tk.Button(text="=", width=ancho, height=alto, bg="SkyBlue3", command=calcular)
- boton_igual.place(x=287, y=480)
- # creamos la pantalla
- pantalla = tk.Entry(
- width = 22,
- font = ('arial',20,'bold'),
- bd = 18,
- bg = 'powder blue',
- justify = 'right',
- textvariable = texto_ingresado,
- state = tk.DISABLED
- )
- pantalla.place(x=10, y=60)
- ventana.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement