Simple calculator in Python
import tkinter as tk
from tkinter import messagebox
def perform_operation(operation):
try:
x = float(entry_x.get())
y = float(entry_y.get())
if operation == 'add':
result = x + y
elif operation == 'subtract':
# Switch order if y is greater than x
if y > x:
result = y - x
else:
result = x - y
elif operation == 'multiply':
result = x * y
elif operation == 'divide':
# Switch order if y is greater than x
if y > x:
result = y / x
else:
result = x / y
if y > x:
messagebox.showwarning("Warning", "Note: y is greater than x in the division operation.")
result_label.config(text=f"Result: {result}")
return result
except ValueError:
messagebox.showerror("Error", "Invalid input. Please enter valid numbers.")
return None
# Function to calculate and display all results
def calculate_all_results():
if entry_x.get() == '' or entry_y.get() == '':
messagebox.showwarning("Warning", "Please enter numbers before calculating.")
return
add_result = perform_operation('add')
subtract_result = perform_operation('subtract')
multiply_result = perform_operation('multiply')
divide_result = perform_operation('divide')
results = []
if add_result is not None:
results.append(f"Addition: {add_result}\n")
if subtract_result is not None:
results.append(f"Subtraction: {subtract_result}\n")
if multiply_result is not None:
results.append(f"Multiplication: {multiply_result}\n")
if divide_result is not None:
results.append(f"Division: {divide_result}\n")
if not results:
messagebox.showerror("Error", "Invalid input. Please enter valid numbers.")
else:
messagebox.showinfo("Results", "".join(results))
# Function to reset the entries and result label
def reset_entries():
entry_x.delete(0, tk.END)
entry_y.delete(0, tk.END)
result_label.config(text="Result:")
entry_x.focus_set()
# Create the main window
root = tk.Tk()
root.title("Simple Calculator")
# Set the size of the window
root.geometry("300x250")
# Create entry widgets
entry_x = tk.Entry(root, width=10)
entry_y = tk.Entry(root, width=10)
# Create and place labels
label_x = tk.Label(root, text="Enter x:")
label_y = tk.Label(root, text="Enter y:")
result_label = tk.Label(root, text="Result:")
label_x.grid(row=0, column=0, padx=10, pady=5)
label_y.grid(row=1, column=0, padx=10, pady=5)
entry_x.grid(row=0, column=1, padx=10, pady=5)
entry_y.grid(row=1, column=1, padx=10, pady=5)
result_label.grid(row=2, column=0, columnspan=2, pady=10)
# Create and place buttons
add_button = tk.Button(root, text="Add", command=lambda: perform_operation('add'))
subtract_button = tk.Button(root, text="Subtract", command=lambda: perform_operation('subtract'))
multiply_button = tk.Button(root, text="Multiply", command=lambda: perform_operation('multiply'))
divide_button = tk.Button(root, text="Divide", command=lambda: perform_operation('divide'))
calculate_all_button = tk.Button(root, text="Calculate All", command=calculate_all_results)
reset_button = tk.Button(root, text="Reset", command=reset_entries)
add_button.grid(row=3, column=0, pady=5)
subtract_button.grid(row=3, column=1, pady=5)
multiply_button.grid(row=4, column=0, pady=5)
divide_button.grid(row=4, column=1, pady=5)
calculate_all_button.grid(row=5, column=0,padx=5, pady=5)
reset_button.grid(row=5, column=1, pady=5)
# Set focus on entry_x
entry_x.focus_set()
# Run the GUI
root.mainloop()
Comments
Post a Comment