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

Calc

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

Calc

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

from tkinter import *

root = Tk()
root.title("CASIO")
font=("Eurostiile extended bold")
root.iconbitmap("img/calculator.ico")

# Evitar el redimensionamiento de ventana


root.resizable(0, 0)

# Establecer un tamaño de ventana


root.geometry("293x305")

# Crear la pantalla de la calculadora


pantalla = Entry(root,
width=22,
bg="#c6da52",
fg="black",
borderwidth=0,
font=('Embed code', 18, 'bold'))

pantalla.grid(row=0, padx=2, pady=2, columnspan=4)

# Funcionamiento de la calculadora
def envia_boton(valor):
anterior = pantalla.get()
pantalla.delete(0, END)
pantalla.insert(0, str(anterior) + str(valor))

def suma():
global num1
global operacion
num1 = pantalla.get()
num1 = float(num1)
pantalla.delete(0,END)
operacion = "+"

def resta():
global num1
global operacion
num1 = pantalla.get()
num1 = float(num1)
pantalla.delete(0,END)
operacion = "-"

def multiplicacion():
global num1
global operacion
num1 = pantalla.get()
num1 = float(num1)
pantalla.delete(0,END)
operacion = "*"

def division():
global num1
global operacion
num1 = pantalla.get()
num1 = float(num1)
pantalla.delete(0,END)
operacion = "/"
def porcentaje():
try:
current_value = float(pantalla.get())
pantalla.delete(0, END)
pantalla.insert(0, str(current_value / 100))
except:
pantalla.delete(0, END)
pantalla.insert(0, "Error")

def igual():
global num2
num2 = pantalla.get()
pantalla.delete(0, END)
if operacion == "+":
pantalla.insert(0, num1 + float(num2))
elif operacion == "-":
pantalla.insert(0, num1 - float(num2))
elif operacion == "*":
pantalla.insert(0, num1 * float(num2))
elif operacion == "/":
if float(num2) != 0:
pantalla.insert(0, num1 / float(num2))
else:
pantalla.insert(0, "Error")
elif operacion == "%":
pantalla.insert(0, num1 % float(num2))

def despejar():
pantalla.delete(0, END)

def AC():
pantalla.delete(0, END)

# Añadir los botones de la calculadora


boton_1 = Button(root, text="1",
width=9,
height=3,
bg="grey",
fg="white",
borderwidth=0,
cursor="hand2",
command=lambda: envia_boton(1)).grid(row=5, column=0, padx=1,
pady=1)

boton_2 = Button(root, text="2",


width=9,
height=3,
bg="grey",
fg="white",
borderwidth=0,
cursor="hand2",
command=lambda: envia_boton(2)).grid(row=5, column=1, padx=1,
pady=1)

boton_3 = Button(root, text="3",


width=9,
height=3,
bg="grey",
fg="white",
borderwidth=0,
cursor="hand2",
command=lambda: envia_boton(3)).grid(row=5, column=2, padx=1,
pady=1)

boton_4 = Button(root, text="4",


width=9,
height=3,
bg="grey",
fg="white",
borderwidth=0,
cursor="hand2",
command=lambda: envia_boton(4)).grid(row=4, column=0, padx=1,
pady=1)

boton_5 = Button(root, text="5",


width=9,
height=3,
bg="grey",
fg="white",
borderwidth=0,
cursor="hand2",
command=lambda: envia_boton(5)).grid(row=4, column=1, padx=1,
pady=1)

boton_6 = Button(root, text="6",


width=9,
height=3,
bg="grey",
fg="white",
borderwidth=0,
cursor="hand2",
command=lambda: envia_boton(6)).grid(row=4, column=2, padx=1,
pady=1)

boton_7 = Button(root, text="7",


width=9,
height=3,
bg="grey",
fg="white",
borderwidth=0,
cursor="hand2",
command=lambda: envia_boton(7)).grid(row=3, column=0, padx=1,
pady=1)

boton_8 = Button(root, text="8",


width=9,
height=3,
bg="grey",
fg="white",
borderwidth=0,
cursor="hand2",
command=lambda: envia_boton(8)).grid(row=3, column=1, padx=1,
pady=1)

boton_9 = Button(root, text="9",


width=9,
height=3,
bg="grey",
fg="white",
borderwidth=0,
cursor="hand2",
command=lambda: envia_boton(9)).grid(row=3, column=2, padx=1,
pady=1)

boton_0 = Button(root, text="0",


width=9,
height=3,
bg="grey",
fg="white",
borderwidth=0,
cursor="hand2",
command=lambda: envia_boton(0)).grid(row=6, column=0, padx=0,
pady=0)

boton_igual = Button(root, text="=",


width=9,
height=3,
bg="grey",
fg="white",
borderwidth=0,
cursor="hand2",
command=igual).grid(row=6, column=2, padx=0, pady=0)

boton_punto = Button(root, text=".",


width=9,
height=3,
bg="grey",
fg="white",
cursor="hand2",
borderwidth=0,
command=lambda: envia_boton(".")).grid(row=6, column=1, padx=0,
pady=0)

boton_mas = Button(root, text="+",


width=9,
height=3,
bg="grey",
fg="white",
borderwidth=0,
cursor="hand2",
command=suma).grid(row=5, column=3, padx=0, pady=0)

boton_menos = Button(root, text="-",


width=9,
height=3,
bg="grey",
fg="white",
borderwidth=0,
cursor="hand2",
command=resta).grid(row=4, column=3, padx=1, pady=1)

boton_multiplicacion = Button(root, text="*",


width=9,
height=3,
bg="grey",
fg="white",
borderwidth=0,
cursor="hand2",
command=multiplicacion).grid(row=3, column=3, padx=1, pady=1)

boton_division = Button(root, text="/",


width=9,
height=3,
bg="grey",
fg="white",
borderwidth=0,
cursor="hand2",
command=division).grid(row=2, column=3, padx=1, pady=1)

boton_despejar = Button(root, text="CE",


width=9,
height=3,
bg="orange",
fg="white",
borderwidth=0,
cursor="hand2",
command=despejar).grid(row=2, column=1, padx=1, pady=1)

boton_AC = Button(root, text="AC",


width=9,
height=3,
bg="orange",
fg="white",
borderwidth=0,
cursor="hand2",
command=AC).grid(row=2, column=0, padx=1, pady=1)

boton_porcentaje = Button(root, text="%",


width=9,
height=3,
bg="grey",
fg="white",
borderwidth=0,
cursor="hand2",
command=porcentaje).grid(row=2, column=2, padx=1, pady=1)

mainloop()

You might also like