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
- def clic(tecla):
- global operador
- operador = operador + str(tecla) # "2+3"
- calculo.set(operador)
- def limpieza():
- global operador
- # mando un cero a la pantalla
- calculo.set("0")
- # borro la variable operador
- operador = ""
- def calcular():
- global operador
- try:
- total = str(eval(operador))
- except Exception:
- operador = ""
- total = "Syntax Error"
- calculo.set(total)
- # ventana que contiene la calculadora
- ventana = tk.Tk()
- ventana.title("CALCULADORA ACME")
- ventana.config(
- width = 390,
- height = 600,
- bg = "Steel Blue",
- )
- ventana.resizable(0,0)
- # creo la variable para mostrar en la pantalla de la calculadora
- calculo = tk.StringVar()
- # creo la variable de consola para almacenar el cálculo a realizar
- operador = "" # "12.58+87/sqrt(22.5)"
- # ancho y alto de cada tecla:
- ancho = 7
- alto = 2
- # creamos la pantalla de
- pantalla = tk.Entry(
- font = ('arial',20,'bold','italic'),
- width = 20,
- bd = 20, # tamaño del borde
- bg = 'powder blue', # color de fondo
- justify = 'right',
- state = tk.DISABLED, # impido escribir en la pantalla
- textvariable = calculo
- )
- pantalla.place(x=20, y=50)
- ############## filas de botones ####################################
- ##### fila 1: 1 2 3 +
- boton = tk.Button(text='1', width=ancho, height=alto, font=('bold'), command=lambda:clic(1))
- boton.place(x=20, y=170)
- boton = tk.Button(text='2', width=ancho, height=alto, font=('bold'), command=lambda:clic(2))
- boton.place(x=110, y=170)
- boton = tk.Button(text='3', width=ancho, height=alto, font=('bold'), command=lambda:clic(3))
- boton.place(x=200, y=170)
- boton = tk.Button(text='+', width=ancho, height=alto, font=('bold'), bg="snow3", command=lambda:clic('+'))
- boton.place(x=290, y=170)
- ##### fila 2: 4 5 6 -
- boton = tk.Button(text='4', width=ancho, height=alto, font=('bold'), command=lambda:clic(4))
- boton.place(x=20, y=230)
- boton = tk.Button(text='5', width=ancho, height=alto, font=('bold'), command=lambda:clic(5))
- boton.place(x=110, y=230)
- boton = tk.Button(text='6', width=ancho, height=alto, font=('bold'), command=lambda:clic(6))
- boton.place(x=200, y=230)
- boton = tk.Button(text='-', width=ancho, height=alto, font=('bold'), bg="snow3", command=lambda:clic('-'))
- boton.place(x=290, y=230)
- ##### fila 3: 7 8 9 x
- boton = tk.Button(text='7', width=ancho, height=alto, font=('bold'), command=lambda:clic(7))
- boton.place(x=20, y=290)
- boton = tk.Button(text='8', width=ancho, height=alto, font=('bold'), command=lambda:clic(8))
- boton.place(x=110, y=290)
- boton = tk.Button(text='9', width=ancho, height=alto, font=('bold'), command=lambda:clic(9))
- boton.place(x=200, y=290)
- boton = tk.Button(text='x', width=ancho, height=alto, font=('bold'), bg="snow3", command=lambda:clic('*'))
- boton.place(x=290, y=290)
- ##### fila 4: ( 0 ) /
- boton = tk.Button(text='(', width=ancho, height=alto, font=('bold'), bg="snow3", command=lambda:clic('('))
- boton.place(x=20, y=350)
- boton = tk.Button(text='0', width=ancho, height=alto, font=('bold'), command=lambda:clic(0))
- boton.place(x=110, y=350)
- boton = tk.Button(text=')', width=ancho, height=alto, font=('bold'), bg="snow3", command=lambda:clic(')'))
- boton.place(x=200, y=350)
- boton = tk.Button(text='/', width=ancho, height=alto, font=('bold'), bg="snow3", command=lambda:clic('/'))
- boton.place(x=290, y=350)
- ##### fila 5: Raiz PuntoDecimal Potencia Resto
- boton = tk.Button(text='RAIZ', width=ancho, height=alto, font=('bold'), bg="snow3", command=lambda:clic('sqrt('))
- boton.place(x=20, y=410)
- boton = tk.Button(text='.', width=ancho, height=alto, font=('bold'), bg="snow3", command=lambda:clic('.'))
- boton.place(x=110, y=410)
- boton = tk.Button(text='POW', width=ancho, height=alto, font=('bold'), bg="snow3", command=lambda:clic('**'))
- boton.place(x=200, y=410)
- boton = tk.Button(text='%', width=ancho, height=alto, font=('bold'), bg="snow3", command=lambda:clic('%'))
- boton.place(x=290, y=410)
- ##### fila 6: Clear, factorial pi =
- boton = tk.Button(text='CLS', width=ancho, height=alto, font=('bold'), bg="snow3", command=limpieza)
- boton.place(x=20, y=470)
- boton = tk.Button(text='!', width=ancho, height=alto, font=('bold'), bg="snow3", command=lambda:clic('factorial('))
- boton.place(x=110, y=470)
- boton = tk.Button(text='PI', width=ancho, height=alto, font=('bold'), bg="snow3", command=lambda:clic('pi'))
- boton.place(x=200, y=470)
- boton = tk.Button(text='=', width=ancho, height=alto, font=('bold'), bg="tan", command=calcular)
- boton.place(x=290, y=470)
- ##### programa principal #########
- # limpio la pantalla
- limpieza()
- """
- INSTRUCCIONES PARA HACER UN EJECUTABLE
- 1) descargar la biblioteca pyinstaller
- python -m pip install pyinstaller
- 2) Crear el ejecutable
- pyinstaller --noconsole --onefile calc.py
- 3) Se crean dos carpetas: build (construcción) y dist(para distribuir el software)
- e dist se guardan los proyectos (por ejemplo, calc) y adentro, los ejecutables
- PORTABLES .No se necesita int+erprete de python, funciona en cualquier
- máquina de similar sistema operativo
- """
- ventana.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement