Skip to content

Commit

Permalink
this code is for calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
lucifer5094 authored Jan 30, 2024
0 parents commit b7f8c01
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions calculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import tkinter as tk

def on_click(button_value):
current_text = entry.get()
if current_text == "Error":
current_text = ""

if button_value == "=":
try:
result = eval(current_text)
entry.delete(0, tk.END)
entry.insert(tk.END, str(result))
except Exception:
entry.delete(0, tk.END)
entry.insert(tk.END, "Error")
elif button_value == "C":
entry.delete(0, tk.END)
else:
entry.insert(tk.END, button_value)

# Create the main window
app = tk.Tk()
app.title("Simple Calculator")

# Entry widget for displaying and entering numbers
entry = tk.Entry(app, width=20, font=('Arial', 16), borderwidth=2, relief="solid")
entry.grid(row=0, column=0, columnspan=4)

# Button layout
buttons = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'0', 'C', '=', '+'
]

# Function to create and place buttons
row_val = 1
col_val = 0
for button in buttons:
tk.Button(app, text=button, width=5, height=2, command=lambda b=button: on_click(b)).grid(row=row_val, column=col_val)
col_val += 1
if col_val > 3:
col_val = 0
row_val += 1

# Run the application
app.mainloop()

0 comments on commit b7f8c01

Please sign in to comment.