0% found this document useful (0 votes)
11 views5 pages

Python Calculator GUI with Tkinter

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)
11 views5 pages

Python Calculator GUI with Tkinter

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

from tkinter import *

root = Tk()
[Link]("CASIO")
font=("Eurostiile extended bold")
[Link]("img/[Link]")

# Evitar el redimensionamiento de ventana


[Link](0, 0)

# Establecer un tamaño de ventana


[Link]("293x305")

# Crear la pantalla de la calculadora


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

[Link](row=0, padx=2, pady=2, columnspan=4)

# Funcionamiento de la calculadora
def envia_boton(valor):
anterior = [Link]()
[Link](0, END)
[Link](0, str(anterior) + str(valor))

def suma():
global num1
global operacion
num1 = [Link]()
num1 = float(num1)
[Link](0,END)
operacion = "+"

def resta():
global num1
global operacion
num1 = [Link]()
num1 = float(num1)
[Link](0,END)
operacion = "-"

def multiplicacion():
global num1
global operacion
num1 = [Link]()
num1 = float(num1)
[Link](0,END)
operacion = "*"

def division():
global num1
global operacion
num1 = [Link]()
num1 = float(num1)
[Link](0,END)
operacion = "/"
def porcentaje():
try:
current_value = float([Link]())
[Link](0, END)
[Link](0, str(current_value / 100))
except:
[Link](0, END)
[Link](0, "Error")

def igual():
global num2
num2 = [Link]()
[Link](0, END)
if operacion == "+":
[Link](0, num1 + float(num2))
elif operacion == "-":
[Link](0, num1 - float(num2))
elif operacion == "*":
[Link](0, num1 * float(num2))
elif operacion == "/":
if float(num2) != 0:
[Link](0, num1 / float(num2))
else:
[Link](0, "Error")
elif operacion == "%":
[Link](0, num1 % float(num2))

def despejar():
[Link](0, END)

def AC():
[Link](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()

Common questions

Powered by AI

The calculator employs a color scheme where most buttons are grey with white text, and crucial function buttons like 'CE' and 'AC' are orange, emphasizing their importance. This color differentiation supports intuitive use by visually distinguishing regular numeric keys from special function keys, enhancing user interaction and reducing errors by making critical functions more noticeable .

Error handling for division by zero in the calculator is explicitly managed within the 'igual' function. Before performing the division, it checks if the second operand ('num2') is zero. If it is, the function sets the display to "Error" instead of trying to compute the division, thus preventing a runtime error .

The calculator window is configured to be non-resizable by setting 'root.resizable(0, 0)'. This decision ensures a consistent user interface where button and display positions remain fixed, preventing layout disruptions that might occur if users resized the window. This stability is essential for maintaining usability and avoiding misalignment of interactive elements .

The 'porcentaje' function is designed to compute the percentage of the current display value by dividing it by 100. It retrieves the current value from the display, converts it to a float, and divides by 100. If any error occurs during this process, such as invalid input, it clears the display and shows "Error" to the user. This operation ensures percentage calculations are straightforward and user errors are minimal .

The calculator application initializes the UI components by setting up the main window parameters such as title, icon, resize properties, and dimensions using 'root = Tk()' and related configuration methods. It creates an 'Entry' widget 'pantalla' for the display and organizes calculator buttons using the 'Button' widget with functions handling button actions. Buttons are positioned using 'grid()' method, specifying row and column positions for layout .

The 'envia_boton' function appends the input value to the existing content on the calculator's display. It retrieves the current content from the display via 'pantalla.get()', deletes the display with 'pantalla.delete()', and inserts the new value combined with the existing content using 'pantalla.insert()' .

The grid system is used extensively to layout buttons logically across rows and columns, ensuring a structured and intuitive interface. By aligning buttons in a calculator-like grid, it mimics familiar physical calculator designs, enhancing user comfort and decreasing learning time. This approach ensures efficient use of space while offering logical groupings of functions and numbers, crucial for quick navigation and minimal user frustration .

The calculator defines a global variable 'num1' to store the first operand and a global variable 'operacion' to store the operation type. For each operation (addition, subtraction, multiplication, and division), a function is defined to update these global variables and clear the display. For example, in the 'suma' function, 'num1' is set to the current display value converted to float format, and 'operacion' is set to '+'. After entering the second operand, the 'igual' function evaluates the expression depending on the current 'operacion' and updates the display with the result .

Both 'despejar' and 'AC' functions are implemented to clear the display using 'pantalla.delete(0, END)'. However, they serve different user scenarios: 'despejar' might be used for clearing recent entry errors, while 'AC' is typically used to reset all calculations back to a clear state, represented by clearing the display's entire content, facilitating a fresh start .

The program provides the 'pantalla' GUI for text input and appends input using 'envia_boton'. However, it does not include explicit code to handle multiple decimal points directly. Users must avoid entering multiple decimals, or it will result in incorrect input. Implementing additional checks in 'envia_boton' could ensure only one decimal point exists per numerical entry, improving robustness and usability .

You might also like