-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.py
77 lines (62 loc) · 2.55 KB
/
calculator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import tkinter as tk
from tkinter import messagebox
# Define the arithmetic functions
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."
return x / y
# Function to perform the selected operation and display the result
def perform_operation():
try:
num1 = float(entry1.get()) # Get the first number from the entry widget
num2 = float(entry2.get()) # Get the second number from the entry widget
choice = operation_var.get() # Get the selected operation
if choice == '1':
result = add(num1, num2)
operation = "+"
elif choice == '2':
result = subtract(num1, num2)
operation = "-"
elif choice == '3':
result = multiply(num1, num2)
operation = "*"
elif choice == '4':
result = divide(num1, num2)
operation = "/"
else:
result = "Invalid operation"
operation = ""
result_label.config(text=f"{num1} {operation} {num2} = {result}") # Display the result
except ValueError:
messagebox.showerror("Input error", "Please enter valid numbers") # Show error message if input is invalid
# Create the main window
root = tk.Tk()
root.title("Simple Calculator")
# Create and place the widgets
tk.Label(root, text="Enter first number:").grid(row=0, column=0, padx=10, pady=5)
entry1 = tk.Entry(root)
entry1.grid(row=0, column=1, padx=10, pady=5)
tk.Label(root, text="Enter second number:").grid(row=1, column=0, padx=10, pady=5)
entry2 = tk.Entry(root)
entry2.grid(row=1, column=1, padx=10, pady=5)
# Create a variable to store the selected operation
operation_var = tk.StringVar()
operation_var.set("1") # Set default operation to "Add"
# Create and place the option menu for selecting the operation
tk.Label(root, text="Select operation:").grid(row=2, column=0, padx=10, pady=5)
operations = [("Add", "1"), ("Subtract", "2"), ("Multiply", "3"), ("Divide", "4")]
for operation, value in operations:
tk.Radiobutton(root, text=operation, variable=operation_var, value=value).grid(sticky='W', padx=10)
# Create and place the button to perform the operation
tk.Button(root, text="Calculate", command=perform_operation).grid(row=3, column=0, columnspan=2, pady=10)
# Create and place the label to display the result
result_label = tk.Label(root, text="Result:")
result_label.grid(row=4, column=0, columnspan=2)
# Run the main event loop
root.mainloop()