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
- ##### funciones
- def clic(tecla):
- global calculo_consola
- calculo_consola = calculo_consola + tecla
- calculo.set(calculo_consola)
- def limpieza():
- global calculo_consola
- calculo_consola = ""
- calculo.set("0")
- def hacer_cuenta():
- global calculo_consola
- try:
- calculo_consola = str(eval(calculo_consola))
- except Exception:
- limpieza()
- calculo_consola = "ERROR"
- calculo.set(calculo_consola)
- def borrar_caracter():
- global calculo_consola
- calculo_consola = calculo_consola[:-1]
- if calculo_consola:
- calculo.set(calculo_consola)
- else:
- calculo.set("0")
- # variables 'del lado consola'
- calculo_consola = "" # variable que se muestra por pantalla
- ######### Lado gráfico ##################
- calculadora = tk.Tk()
- calculadora.title("Calculadora ACME")
- calculadora.config(width=390, height=600, bg="Light Steel Blue")
- calculadora.resizable(0,0)
- # variables 'del lado gráfico'
- calculo = tk.StringVar() # variable que se muestra por pantalla
- # inicializo la calculadora
- limpieza()
- # creamos una pantalla
- pantalla = tk.Entry(
- font = ["arial",20,"bold"],
- width = 22,
- bd = 14, # grosor del borde
- bg = "powder blue",
- justify = "right",
- state = tk.DISABLED, # la hago sólo lectura
- textvariable = calculo # la pantalla muestra en todo momento
- ) # el contenido de la variable calculo
- pantalla.place(x=10, y=50)
- # defino las dimensiones de las teclas
- ancho = 10
- alto = 2
- #### Teclas
- # boton retroceso
- tecla = tk.Button(text="\u2190", width=ancho, height=alto, command=borrar_caracter)
- tecla.place(x=288, y=140)
- ## Primera fila: 1 2 3 +
- tecla = tk.Button(text="1", width=ancho, height=alto, command=lambda:clic("1"))
- tecla.place(x=18, y=200)
- tecla = tk.Button(text="2", width=ancho, height=alto, command=lambda:clic("2"))
- tecla.place(x=108, y=200)
- tecla = tk.Button(text="3", width=ancho, height=alto, command=lambda:clic("3"))
- tecla.place(x=198, y=200)
- tecla = tk.Button(text="+", width=ancho, height=alto, bg="Steel Blue", command=lambda:clic("+"))
- tecla.place(x=288, y=200)
- ## Segunda fila: 4 5 6 -
- tecla = tk.Button(text="4", width=ancho, height=alto, command=lambda:clic("4"))
- tecla.place(x=18, y=260)
- tecla = tk.Button(text="5", width=ancho, height=alto, command=lambda:clic("5"))
- tecla.place(x=108, y=260)
- tecla = tk.Button(text="6", width=ancho, height=alto, command=lambda:clic("6"))
- tecla.place(x=198, y=260)
- tecla = tk.Button(text="-", width=ancho, height=alto, bg="Steel Blue", command=lambda:clic("-"))
- tecla.place(x=288, y=260)
- ## Tercera fila: 7 8 9 x
- tecla = tk.Button(text="7", width=ancho, height=alto, command=lambda:clic("7"))
- tecla.place(x=18, y=320)
- tecla = tk.Button(text="8", width=ancho, height=alto, command=lambda:clic("8"))
- tecla.place(x=108, y=320)
- tecla = tk.Button(text="9", width=ancho, height=alto, command=lambda:clic("9"))
- tecla.place(x=198, y=320)
- tecla = tk.Button(text="x", width=ancho, height=alto, bg="Steel Blue", command=lambda:clic("*"))
- tecla.place(x=288, y=320)
- ## Cuarta fila: ( 0 ) /
- tecla = tk.Button(text="(", width=ancho, height=alto, bg="Sky Blue", command=lambda:clic("("))
- tecla.place(x=18, y=380)
- tecla = tk.Button(text="0", width=ancho, height=alto, command=lambda:clic("0"))
- tecla.place(x=108, y=380)
- tecla = tk.Button(text=")", width=ancho, height=alto, bg="Sky Blue", command=lambda:clic(")"))
- tecla.place(x=198, y=380)
- tecla = tk.Button(text="/", width=ancho, height=alto, bg="Steel Blue", command=lambda:clic("/"))
- tecla.place(x=288, y=380)
- ## Quinta fila: Raiz Coma decimal Potencia Modulo
- tecla = tk.Button(text="\u221A", width=ancho, height=alto, bg="Steel Blue", command=lambda:clic("sqrt("))
- tecla.place(x=18, y=440)
- tecla = tk.Button(text=".", width=ancho, height=alto, command=lambda:clic("."))
- tecla.place(x=108, y=440)
- tecla = tk.Button(text="\u005E", width=ancho, height=alto, bg="Steel Blue", command=lambda:clic("**"))
- tecla.place(x=198, y=440)
- tecla = tk.Button(text="%", width=ancho, height=alto, bg="Steel Blue", command=lambda:clic("%"))
- tecla.place(x=288, y=440)
- ## Sexta fila: Clear Factorial pi =
- tecla = tk.Button(text="CL", width=ancho, height=alto, bg="Sky Blue", command=limpieza)
- tecla.place(x=18, y=500)
- tecla = tk.Button(text="!", width=ancho, height=alto, bg="Steel Blue", command=lambda:clic("factorial("))
- tecla.place(x=108, y=500)
- tecla = tk.Button(text="\u03C0", width=ancho, height=alto, bg="Sky Blue", command=lambda:clic(str(pi)))
- tecla.place(x=198, y=500)
- tecla = tk.Button(text="=", width=ancho, height=alto, bg="thistle", command=hacer_cuenta)
- tecla.place(x=288, y=500)
- calculadora.mainloop()
- """
- 1)instalar pyinstaller:
- python -m pip install pyinstaller
- 2) Navegar hasta la carpeta con el script y ejecutar
- pyinstaller --noconsole --onefile calc.py
- 3) Buscar el ejecutable dentro de la carpeta dist
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement