forked from y8tireu/My-Python-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculator 4.py
65 lines (50 loc) · 1.87 KB
/
Calculator 4.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
import tkinter as tk
from tkinter import messagebox
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 x / y
else:
return "ey thumbi poda attam pota pakka pushpa adadey."
def perform_calculation():
try:
operation = operation_var.get()
num1 = float(entry_num1.get())
num2 = float(entry_num2.get())
if operation == "Add":
result = add(num1, num2)
elif operation == "Subtract":
result = subtract(num1, num2)
elif operation == "Multiply":
result = multiply(num1, num2)
elif operation == "Divide":
result = divide(num1, num2)
else:
raise ValueError("Invalid operation")
result_label.config(text=f"Result: {result}")
except ValueError as e:
messagebox.showerror("Error", f"Invalid input: {e}")
# Create the main window
root = tk.Tk()
root.title("Attam Pota Simple Calculator")
# Create widgets
operation_var = tk.StringVar(value="Add")
tk.Label(root, text="Select Operation:").grid(row=0, column=0, padx=10, pady=10)
operations = ["Add", "Subtract", "Multiply", "Divide"]
tk.OptionMenu(root, operation_var, *operations).grid(row=0, column=1, padx=10, pady=10)
tk.Label(root, text="Enter First Number:").grid(row=1, column=0, padx=10, pady=10)
entry_num1 = tk.Entry(root)
entry_num1.grid(row=1, column=1, padx=10, pady=10)
tk.Label(root, text="Enter Second Number:").grid(row=2, column=0, padx=10, pady=10)
entry_num2 = tk.Entry(root)
entry_num2.grid(row=2, column=1, padx=10, pady=10)
tk.Button(root, text="Calculate", command=perform_calculation).grid(row=3, column=0, columnspan=2, pady=10)
result_label = tk.Label(root, text="Result: ")
result_label.grid(row=4, column=0, columnspan=2, pady=10)
# Run the main event loop
root.mainloop()