import tkinter as tk
import math
import cmath
# Function for basic operations
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
if y == 0:
return "Error! Division by zero."
else:
return x / y
# Function for scientific operations
def square_root(x):
try:
return [Link](x)
except ValueError:
return "Error! Negative input for square root."
def power(x, y):
return [Link](x, y)
def logarithm(x, base=10):
if x <= 0 or base <= 0:
return "Error! Logarithm of non-positive numbers is undefined."
else:
return [Link](x, base)
def sine(x):
return [Link]([Link](x))
def cosine(x):
return [Link]([Link](x))
def tangent(x):
return [Link]([Link](x))
def factorial(x):
if x < 0:
return "Error! Factorial is undefined for negative numbers."
else:
return [Link](int(x))
# Handle complex number operations
def complex_add(x, y):
return x + y
def complex_multiply(x, y):
return x * y
def complex_conjugate(x):
return [Link]()
# Clear the input field
def clear():
[Link]("")
# Calculate the result based on the user input
def calculate():
try:
expression = [Link]()
result = eval(expression, {"__builtins__": None}, {
"sin": [Link],
"cos": [Link],
"tan": [Link],
"log": [Link],
"sqrt": [Link],
"pow": [Link],
"factorial": [Link],
"abs": abs,
"pi": [Link],
"e": math.e,
"exp": [Link],
"complex": complex,
"pi": [Link],
})
[Link](result)
except Exception as e:
[Link](f"Error: {str(e)}")
# Switch between complex number and real input mode
def toggle_complex():
current = complex_mode.get()
complex_mode.set(not current)
# Create the main window
window = [Link]()
[Link]("Scientific Calculator")
[Link]("400x600")
# StringVar for the input field
display = [Link]()
# Entry widget for displaying the expression and result
entry = [Link](window, textvar=display, font=("Arial", 14), bd=10,
relief="solid", justify="right")
[Link](row=0, column=0, columnspan=4)
# Button functions
button_texts = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'C', '0', '=', '+',
'sin', 'cos', 'tan', 'sqrt',
'log', 'pow', 'abs', 'exp',
'pi', 'e', 'factorial', '(',
')', '.', 'complex', 'clear'
]
buttons = []
# Add buttons dynamically based on button_texts
for i, text in enumerate(button_texts):
if text == "=":
button = [Link](window, text=text, width=10, height=3, font=("Arial",
14),
command=calculate)
elif text == "clear":
button = [Link](window, text="Clear", width=10, height=3, font=("Arial",
14), command=clear)
else:
button = [Link](window, text=text, width=10, height=3, font=("Arial",
14),
command=lambda t=text: [Link]([Link], t) if t !=
"=" else None)
row = (i // 4) + 1
col = i % 4
[Link](row=row, column=col, padx=5, pady=5)
# Special button for clear
clear_button = [Link](window, text="C", width=10, height=3, font=("Arial", 14),
command=clear)
clear_button.grid(row=5, column=0, padx=5, pady=5)
# Toggle button for switching complex mode
complex_mode = [Link](value=False)
toggle_button = [Link](window, text="Complex Mode", variable=complex_mode,
font=("Arial", 12))
toggle_button.grid(row=5, column=1, padx=5, pady=5)
# Run the application
[Link]()