forked from fineanmol/hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator_python
More file actions
40 lines (33 loc) · 1.03 KB
/
Copy pathCalculator_python
File metadata and controls
40 lines (33 loc) · 1.03 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
import tkinter as tk
# Function to evaluate the expression and update the display
def evaluate():
try:
result = str(eval(entry.get()))
result_label.config(text="Result: " + result)
except Exception as e:
result_label.config(text="Invalid input")
# Create the main window
window = tk.Tk()
window.title("Simple Calculator")
# Create an Entry widget for user input
entry = tk.Entry(window, width=30)
entry.grid(row=0, column=0, columnspan=4)
# Create buttons for numbers and operators
buttons = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'0', '.', '=', '+'
]
row, col = 1, 0
for button in buttons:
tk.Button(window, text=button, padx=20, pady=20, command=lambda b=button: entry.insert(tk.END, b) if b != '=' else evaluate()).grid(row=row, column=col)
col += 1
if col > 3:
col = 0
row += 1
# Create a label to display the result
result_label = tk.Label(window, text="Result: ")
result_label.grid(row=5, column=0, columnspan=4)
# Run the GUI
window.mainloop()