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

Code

Uploaded by

brut3kidoo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Code

Uploaded by

brut3kidoo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

import tkinter as tk

from tkinter import messagebox

# Function to update the display with the button clicked


def button_click(value):

current = display.get()

display.delete(0, tk.END) # Clear the display

display.insert(tk.END, current + value)

# Function to perform the calculation

def calculate():
try:
result = eval(display.get()) # Evaluate the expression entered

display.delete(0, tk.END)

display.insert(tk.END, str(result))

except Exception as e:

messagebox.showerror("Error", "Invalid input")

# Function to clear the display


def clear():

display.delete(0, tk.END)

# Create the main window

root = tk.Tk()

root.title("Colorful Calculator")

root.geometry("400x400")
root.config(bg="lightblue")

# Create the display entry widget


display = tk.Entry(root, font=("Arial", 14), bg="lightyellow", fg="black", bd=10,
relief="ridge", justify="right")

display.grid(row=0, column=0, columnspan=4)

# Button layout (numbers, operators, and function buttons)

buttons = [

("7", 1, 0), ("8", 1, 1), ("9", 1, 2), ("/", 1, 3),


("4", 2, 0), ("5", 2, 1), ("6", 2, 2), ("*", 2, 3),

("1", 3, 0), ("2", 3, 1), ("3", 3, 2), ("-", 3, 3),

("0", 4, 0), (".", 4, 1), ("=", 4, 2), ("+", 4, 3),

("C", 5, 0)

# Create buttons and add them to the grid


for (text, row, col) in buttons:

button = tk.Button(root, text=text, font=("Arial", 14), width=4, height=1,


bg="lightgreen", fg="black", bd=4, relief="raised")

button.grid(row=row, column=col, padx=5, pady=5)

if text == "=":

button.config(bg="orange", command=calculate)

elif text == "C":

button.config(bg="red", command=clear)

else:

button.config(command=lambda value=text: button_click(value))

# Start the GUI loop

root.mainloop()

You might also like