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 gráfica
- """
- import tkinter as tk
- from math import sqrt, factorial, pi
- ##### funciones
- def clic(tecla):
- global operacion
- operacion = operacion + tecla
- calculo.set(operacion)
- def limpieza():
- global operacion
- # borro el cálculo de la consola
- operacion = ""
- # borro el cálculo de la pantalla
- calculo.set("0")
- def hacer_calculo():
- global operacion
- try:
- resultado = str(eval(operacion))
- except:
- resultado ="ERROR"
- limpieza()
- calculo.set(resultado)
- def borrar_caracter():
- global operacion
- lista = []
- # creo una lista con los caracteres de operacion
- for i in range(len(operacion)-1):
- lista.append(operacion[i])
- # rearmo la variable operacion
- operacion = "".join(lista)
- calculo.set(operacion)
- # creo una variable de consola mostrar el cálculo en la pantalla
- operacion = "" # por ejemplo, 2+3.69*58, un string
- #### ventana de la calculadora
- ventana = tk.Tk()
- ventana.title("Calculadora ACME")
- ventana.config(width=390, height=600, bg="Light Steel Blue")
- ventana.resizable(0,0)
- # creo una variable gráfica para mostrar el cálculo en la pantalla
- calculo = tk.StringVar() # por ejemplo, 2+3.69*58, un string
- # limpio la pantalla: pongo un cero para empezar
- limpieza()
- # creo la pantalla
- pantalla = tk.Entry(
- font = ('arial', 20, 'bold'),
- width = 20,
- bd = 20,
- bg = 'powder blue',
- justify = 'right',
- state = tk.DISABLED, # deshabilito el ingreso de datos, solo muestro
- textvariable = calculo # en todo momento muestra el calculo
- )
- pantalla.place(x=20, y=50)
- # defino las dimensiones de las teclas
- ancho = 9
- alto = 2
- ###### teclas
- # tecla para borrar un caracter
- boton = tk.Button(text='DEL', width=ancho, height=alto, bg="medium aquamarine", command=borrar_caracter)
- boton.place(x=287, y=140)
- ### primera fila: 1 2 3 +
- boton = tk.Button(text='1', width=ancho, height=alto, command=lambda:clic('1'))
- boton.place(x=17, y=200)
- boton = tk.Button(text='2', width=ancho, height=alto, command=lambda:clic('2'))
- boton.place(x=107, y=200)
- boton = tk.Button(text='3', width=ancho, height=alto, command=lambda:clic('3'))
- boton.place(x=197, y=200)
- boton = tk.Button(text='+', width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic('+'))
- boton.place(x=287, y=200)
- ### segunda fila: 4 5 6 -
- boton = tk.Button(text='4', width=ancho, height=alto, command=lambda:clic('4'))
- boton.place(x=17, y=260)
- boton = tk.Button(text='5', width=ancho, height=alto, command=lambda:clic('5'))
- boton.place(x=107, y=260)
- boton = tk.Button(text='6', width=ancho, height=alto, command=lambda:clic('6'))
- boton.place(x=197, y=260)
- boton = tk.Button(text='-', width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic('-'))
- boton.place(x=287, y=260)
- ### tercera fila: 7 8 9 x
- boton = tk.Button(text='7', width=ancho, height=alto, command=lambda:clic('7'))
- boton.place(x=17, y=320)
- boton = tk.Button(text='8', width=ancho, height=alto, command=lambda:clic('8'))
- boton.place(x=107, y=320)
- boton = tk.Button(text='9', width=ancho, height=alto, command=lambda:clic('9'))
- boton.place(x=197, y=320)
- boton = tk.Button(text='x', width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic('*'))
- boton.place(x=287, y=320)
- ### cuarta fila: ( 0 ) /
- boton = tk.Button(text='(', width=ancho, height=alto, bg="sky blue", command=lambda:clic('('))
- boton.place(x=17, y=380)
- boton = tk.Button(text='0', width=ancho, height=alto, command=lambda:clic('0'))
- boton.place(x=107, y=380)
- boton = tk.Button(text=')', width=ancho, height=alto, bg="sky blue", command=lambda:clic(')'))
- boton.place(x=197, y=380)
- boton = tk.Button(text='/', width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic('/'))
- boton.place(x=287, y=380)
- ### quinta fila: raiz coma decimal potencia resto o módulo
- boton = tk.Button(text='RAIZ', width=ancho, height=alto, bg="sky blue", command=lambda:clic('sqrt('))
- boton.place(x=17, y=440)
- boton = tk.Button(text='.', width=ancho, height=alto, bg="SteelBlue2", command=lambda:clic('.'))
- boton.place(x=107, y=440)
- boton = tk.Button(text='POT', width=ancho, height=alto, bg="sky blue", command=lambda:clic('**'))
- boton.place(x=197, y=440)
- boton = tk.Button(text='%', width=ancho, height=alto, bg="sky blue", command=lambda:clic('%'))
- boton.place(x=287, y=440)
- ### sexta fila: Clear Factorial PI =
- boton = tk.Button(text='CL', width=ancho, height=alto, bg="medium aquamarine", command=limpieza)
- boton.place(x=17, y=500)
- boton = tk.Button(text='!', width=ancho, height=alto, bg="sky blue", command=lambda:clic('factorial('))
- boton.place(x=107, y=500)
- boton = tk.Button(text='PI', width=ancho, height=alto, bg="sky blue", command=lambda:clic(str(pi)))
- boton.place(x=197, y=500)
- boton = tk.Button(text='=', width=ancho, height=alto, bg="SteelBlue2", command=hacer_calculo)
- boton.place(x=287, y=500)
- ventana.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement