import tkinter as tk
import math
# Function to update expression
def press(key):
global expression
if key == "=":
try:
expression = str(eval(expression))
[Link](expression)
except:
[Link]("Error")
expression = ""
elif key == "C":
expression = ""
[Link]("0")
elif key == "CE":
expression = expression[:-1]
[Link](expression if expression else "0")
elif key == "√":
try:
expression = str([Link](float(expression)))
[Link](expression)
except:
[Link]("Error")
expression = ""
elif key == "x²":
try:
expression = str(float(expression) ** 2)
[Link](expression)
except:
[Link]("Error")
expression = ""
elif key == "1/x":
try:
expression = str(1 / float(expression))
[Link](expression)
except:
[Link]("Error")
expression = ""
elif key == "±":
if [Link]("-"):
expression = expression[1:]
else:
expression = "-" + expression
[Link](expression)
else:
expression += str(key)
[Link](expression)
# Create main window
root = [Link]()
[Link]("Cute Calculator")
[Link](bg="#ffe6f0") # pastel pink background
expression = ""
equation = [Link]()
[Link]("0")
# Display
display = [Link](root, textvariable=equation, font=('Comic Sans MS', 24, "bold"), bd=8,
insertwidth=2, width=15, borderwidth=4, relief="ridge",
justify="right", bg="#fff0f5", fg="#ff69b4")
[Link](row=0, column=0, columnspan=4, pady=12)
# Button layout
buttons = [
["%", "CE", "C", "⌫"],
["1/x", "x²", "√", "/"],
["7", "8", "9", "*"],
["4", "5", "6", "-"],
["1", "2", "3", "+"],
["±", "0", ".", "="]
# Pastel color palette
btn_colors = {
"num": "#ffd6e0", # pastel pink
"op": "#c9e4de", # pastel mint
"func": "#f9f7d9", # pastel yellow
"clear": "#f7c5cc", # light rose
"spec": "#d4a5ff", # pastel purple
"equal": "#ffb347", # soft orange
# Create buttons
for r, row in enumerate(buttons, 1):
for c, char in enumerate(row):
action = lambda x=char: press(x if x != "⌫" else "CE")
# Decide button color
if [Link]() or char == ".":
color = btn_colors["num"]
elif char in ["+", "-", "*", "/", "%"]:
color = btn_colors["op"]
elif char in ["√", "x²", "1/x"]:
color = btn_colors["func"]
elif char in ["C", "CE", "⌫"]:
color = btn_colors["clear"]
elif char == "=":
color = btn_colors["equal"]
else:
color = btn_colors["spec"]
[Link](root, text=char, width=5, height=2,
font=('Comic Sans MS', 16, "bold"),
bg=color, fg="black",
activebackground="#ffdae0",
activeforeground="black",
command=action, relief="raised", bd=3)\
.grid(row=r, column=c, padx=6, pady=6)
[Link]()