0% found this document useful (0 votes)
2 views

CALCULADORA PYTHON

This document contains a Python script that creates a graphical calculator using the Tkinter library. It includes functionalities for basic arithmetic operations, memory functions, and various mathematical operations such as square and square root. The calculator features a user interface with buttons for input and display areas for showing calculations and results.

Uploaded by

nizeron26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

CALCULADORA PYTHON

This document contains a Python script that creates a graphical calculator using the Tkinter library. It includes functionalities for basic arithmetic operations, memory functions, and various mathematical operations such as square and square root. The calculator features a user interface with buttons for input and display areas for showing calculations and results.

Uploaded by

nizeron26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

import tkinter as tk

#----------Ventana---------------
ventana = tk.Tk()

ventana.geometry('320x500')
ventana.title('Calculadora')
ventana.iconbitmap("Calculator_icon.ico")
ventana.configure(bg='#272727')

oper = ''

#--------Parte superior(Estándar)-------
titulo = tk.Frame(ventana)
titulo.columnconfigure(0,weight=1)
titulo.columnconfigure(1,weight=1)
titulo.columnconfigure(2,weight=1)

botralla = tk.Button(titulo, text='≡', font=('Segoe UI', 20),


bg='#272727', fg='#FDFEFE', borderwidth=0)
botralla.grid(row=0,column=0, sticky='W')
negro = tk.Label(titulo, text=' ',
font=('Segoe UI', 32), bg='#272727', fg='#FDFEFE', borderwidth=0)
negro.grid(row=0,column=1, sticky='WE')
estandar = tk.Label(titulo, text=' Estándar □', font=('Segoe UI', 18),
bg='#272727', fg='#FDFEFE', borderwidth=0)
estandar.grid(row=0,column=1, sticky='W')
cuad = tk.Button(titulo, text='○', font=('Segoe UI', 20), bg='#272727',
fg='#FDFEFE', borderwidth=0)
cuad.grid(row=0,column=2, sticky='WE')

titulo.pack(fill='x')

#-------------Pantallas----------------
texto_pant = tk.StringVar()
texto_pant2 = tk.StringVar()

#-----------Funciones de los botones---------

def modificar_texto(num):
texto_pant2.set(texto_pant2.get() + num)

def opracion(nombre, num1, num2):


if nombre =='suma':
resultado = num1 + num2
elif nombre == 'resta':
resultado = num1 - num2
elif nombre == 'mult':
resultado = num1 * num2
elif nombre == 'divi':
resultado = num1 / num2
elif nombre == 'sqr':
resultado = num1 **2
return resultado

def backspace():
texto_pant2.set(texto_pant2.get()[:-1])

def suma():
global oper
texto_pant.set(texto_pant2.get() + ' + ' )
texto_pant2.set('')
oper = 'suma'

def resta():
global oper
texto_pant.set(texto_pant2.get() + ' - ' )
texto_pant2.set('')
oper = 'resta'

def mult():
global oper
texto_pant.set(texto_pant2.get() + ' x ' )
texto_pant2.set('')
oper = 'mult'

def divi():
global oper
texto_pant.set(texto_pant2.get() + ' ÷ ' )
texto_pant2.set('')
oper = 'divi'

def sqr():
num = float(texto_pant2.get())
resu = num **2
texto_pant.set(texto_pant2.get() + '^2')
texto_pant2.set(f'{resu}')

def C():
texto_pant.set('')
texto_pant2.set('')

def CE():
texto_pant2.set('')
def raiz():
num = float(texto_pant2.get())
resu = num **0.5
texto_pant.set('²√'+texto_pant2.get())
texto_pant2.set(f'{resu}')

def menos():
num = float(texto_pant2.get())
resu = num * -1
texto_pant2.set(f'{resu}')

def inv():
num = float(texto_pant2.get())
resu = 1 / num
texto_pant.set('1/'+texto_pant2.get())
texto_pant2.set(f'{resu}')

def igual():
num1 = float(texto_pant.get()[:-2])
num2 = float(texto_pant2.get())
resultado = opracion(oper, num1, num2)
texto_pant.set(texto_pant.get()+texto_pant2.get()+'=')
texto_pant2.set(f'{resultado}')

#----------entradas-------
entrada = tk.Entry(bg='#272727', fg='#959595', font=('Segoe UI',16),
justify='right', borderwidth=0, text=texto_pant)
entrada.pack(fill='x')

entrada2 = tk.Entry(bg='#272727', fg='#FDFEFE', font=('Segoe UI',34),


justify='right', borderwidth=0, text=texto_pant2)
entrada2.pack(fill='x')
#--------MC botones----------
emes = tk.Frame(ventana)
emes.columnconfigure(0,weight=1)
emes.columnconfigure(1,weight=1)
emes.columnconfigure(2,weight=1)
emes.columnconfigure(3,weight=1)
emes.columnconfigure(4,weight=1)
emes.columnconfigure(5,weight=1)

botMC = tk.Label(emes, text="MC", font=('Arial', 10), bg='#272727',


fg='#626567')
botMC.grid(row=0,column=0, sticky='WE')
botMR = tk.Label(emes, text="MR", font=('Arial', 10), bg='#272727',
fg='#626567')
botMR.grid(row=0,column=1, sticky='WE')
botMplus = tk.Label(emes, text="M+", font=('Arial', 10), bg='#272727',
fg='#FDFEFE')
botMplus.grid(row=0,column=2, sticky='WE')
botMmenos = tk.Label(emes, text="M-", font=('Arial', 10), bg='#272727',
fg='#FDFEFE')
botMmenos.grid(row=0,column=3, sticky='WE')
botMS = tk.Label(emes, text="MS", font=('Arial', 10), bg='#272727',
fg='#FDFEFE')
botMS.grid(row=0,column=4, sticky='WE')
botMdown = tk.Label(emes, text="M˅", font=('Arial', 10), bg='#272727',
fg='#626567')
botMdown.grid(row=0,column=5, sticky='WE')
emes.pack(fill='x')
#-----------botones-------------
grad = tk.Frame(ventana)
grad.columnconfigure(0,weight=1)
grad.columnconfigure(1,weight=1)
grad.columnconfigure(2,weight=1)
grad.columnconfigure(3,weight=1)
grad.rowconfigure(0,weight=1)
grad.rowconfigure(1,weight=1)
grad.rowconfigure(2,weight=1)
grad.rowconfigure(3,weight=1)
grad.rowconfigure(4,weight=1)
grad.rowconfigure(5,weight=1)

#primera fila
botpor = tk.Button(grad, text='%', font=('Segoe UI', 17), bg='#3D3D3D',
fg='#FDFEFE')
botpor.grid(row=0,column=0, sticky='WE')
botce = tk.Button(grad, text='CE', font=('Segoe UI', 17), bg='#3D3D3D',
fg='#FDFEFE', command=CE)
botce.grid(row=0,column=1, sticky='WE')
botC = tk.Button(grad, text='C', font=('Segoe UI', 17), bg='#3D3D3D',
fg='#FDFEFE', command=C)
botC.grid(row=0,column=2, sticky='WE')
botdel = tk.Button(grad, text='←', font=('Segoe UI', 17), bg='#3D3D3D',
fg='#FDFEFE', command=backspace)
botdel.grid(row=0,column=3, sticky='WE')
#segunda fila
botinv = tk.Button(grad, text='1/x', font=('Segoe UI', 17),
bg='#3D3D3D', fg='#FDFEFE', command=inv)
botinv.grid(row=1,column=0, sticky='WE')
botpot = tk.Button(grad, text='x²', font=('Segoe UI', 17),
bg='#3D3D3D', fg='#FDFEFE', command=sqr)
botpot.grid(row=1,column=1, sticky='WE')
botrai = tk.Button(grad, text='²√x', font=('Segoe UI', 17), bg='#3D3D3D',
fg='#FDFEFE', command=raiz)
botrai.grid(row=1,column=2, sticky='WE')
botdiv = tk.Button(grad, text='÷', font=('Segoe UI', 17), bg='#3D3D3D',
fg='#FDFEFE', command=divi)
botdiv.grid(row=1,column=3, sticky='WE')
#tercera fila
bot7 = tk.Button(grad, text='7', font=('Segoe UI', 17), bg='#555555',
fg='#FDFEFE', command= lambda: modificar_texto('7'))
bot7.grid(row=2,column=0, sticky='WE')
bot8 = tk.Button(grad, text='8', font=('Segoe UI', 17), bg='#555555',
fg='#FDFEFE', command= lambda: modificar_texto('8'))
bot8.grid(row=2,column=1, sticky='WE')
bot9 = tk.Button(grad, text='9', font=('Segoe UI', 17), bg='#555555',
fg='#FDFEFE', command=lambda: modificar_texto('9'))
bot9.grid(row=2,column=2, sticky='WE')
botMul = tk.Button(grad, text='X', font=('Segoe UI', 17), bg='#3D3D3D',
fg='#FDFEFE', command=mult)
botMul.grid(row=2,column=3, sticky='WE')
#cuarta fila
bot4 = tk.Button(grad, text='4', font=('Segoe UI', 17), bg='#555555',
fg='#FDFEFE', command=lambda: modificar_texto('4'))
bot4.grid(row=3,column=0, sticky='WE')
bot5 = tk.Button(grad, text='5', font=('Segoe UI', 17), bg='#555555',
fg='#FDFEFE', command=lambda: modificar_texto('5'))
bot5.grid(row=3,column=1, sticky='WE')
bot6 = tk.Button(grad, text='6', font=('Segoe UI', 17), bg='#555555',
fg='#FDFEFE', command=lambda: modificar_texto('6'))
bot6.grid(row=3,column=2, sticky='WE')
botmen = tk.Button(grad, text='-', font=('Segoe UI', 17), bg='#3D3D3D',
fg='#FDFEFE', command=resta)
botmen.grid(row=3,column=3, sticky='WE')
#quinta fila
bot1 = tk.Button(grad, text='1', font=('Segoe UI', 17), bg='#555555',
fg='#FDFEFE', command=lambda: modificar_texto('1'))
bot1.grid(row=4,column=0, sticky='WE')
bot2 = tk.Button(grad, text='2', font=('Segoe UI', 17), bg='#555555',
fg='#FDFEFE', command=lambda: modificar_texto('2'))
bot2.grid(row=4,column=1, sticky='WE')
bot3 = tk.Button(grad, text='3', font=('Segoe UI', 17), bg='#555555',
fg='#FDFEFE', command=lambda: modificar_texto('3'))
bot3.grid(row=4,column=2, sticky='WE')
botmas = tk.Button(grad, text='+', font=('Segoe UI', 17), bg='#3D3D3D',
fg='#FDFEFE', command=suma)
botmas.grid(row=4,column=3, sticky='WE')
#sexta fila
botresicro = tk.Button(grad, text='+/_', font=('Segoe UI', 17),
bg='#555555', fg='#FDFEFE', command=menos)
botresicro.grid(row=5,column=0, sticky='WE')
bot0 = tk.Button(grad, text='0', font=('Segoe UI', 17), bg='#555555',
fg='#FDFEFE', command=lambda: modificar_texto('0'))
bot0.grid(row=5,column=1, sticky='WE')
botcoma = tk.Button(grad, text='.', font=('Segoe UI', 17),
bg='#555555', fg='#FDFEFE', command=lambda: modificar_texto('.'))
botcoma.grid(row=5,column=2, sticky='WE')
botigual = tk.Button(grad, text='=', font=('Segoe UI', 17),
bg='#1D8348', fg='#17202A', command=igual)
botigual.grid(row=5,column=3, sticky='WE')

grad.pack(fill='x')
ventana.mainloop()

You might also like