-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask2_Calculator.py
More file actions
51 lines (41 loc) · 1.4 KB
/
Task2_Calculator.py
File metadata and controls
51 lines (41 loc) · 1.4 KB
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
import tkinter as tk
from tkinter import messagebox
def calculate():
try:
num1 = float(num1_entry.get())
num2 = float(num2_entry.get())
operation = operation_var.get()
if operation == "+":
result = num1 + num2
elif operation == "-":
result = num1 - num2
elif operation == "*":
result = num1 * num2
elif operation == "/":
if num2 != 0:
result = num1 / num2
else:
messagebox.showerror("Error", "Division by zero is not allowed.")
return
result_label.config(text=f"Result: {result}")
except ValueError:
messagebox.showerror("Error", "Invalid input. Please enter numbers only.")
root = tk.Tk()
root.title("Simple Calculator")
num1_label = tk.Label(root, text="Number 1:")
num1_label.pack()
num1_entry = tk.Entry(root, width=20)
num1_entry.pack()
num2_label = tk.Label(root, text="Number 2:")
num2_label.pack()
num2_entry = tk.Entry(root, width=20)
num2_entry.pack()
operation_var = tk.StringVar()
operation_var.set("+")
operation_menu = tk.OptionMenu(root, operation_var, "+", "-", "*", "/")
operation_menu.pack()
calculate_button = tk.Button(root, text="Calculate", command=calculate)
calculate_button.pack()
result_label = tk.Label(root, text="Result:")
result_label.pack()
root.mainloop()