Skip to content

Calc.py #12714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
seun3-beep opened this issue May 8, 2025 · 0 comments
Open

Calc.py #12714

seun3-beep opened this issue May 8, 2025 · 0 comments

Comments

@seun3-beep
Copy link

import tkinter as tk
import tkinter.messagebox
from tkinter.constants import SUNKEN

win = tk.Tk()
win.title('Calculator')

frame = tk.Frame(win, bg="skyblue", padx=10)
frame.pack()

entry = tk.Entry(frame, relief=SUNKEN, borderwidth=3, width=30)
entry.grid(row=0, column=0, columnspan=3, ipady=2, pady=2)

def click(num):
entry.insert(tk.END, num)

def equal():
try:
res = str(eval(entry.get()))
entry.delete(0, tk.END)
entry.insert(0, res)
except:
tk.messagebox.showinfo("Error", "Syntax Error")

def clear():
entry.delete(0, tk.END)

buttons = [
('1', 1, 0), ('2', 1, 1), ('3', 1, 2),
('4', 2, 0), ('5', 2, 1), ('6', 2, 2),
('7', 3, 0), ('8', 3, 1), ('9', 3, 2),
('0', 4, 1), ('+', 5, 0), ('-', 5, 1),
('*', 5, 2), ('/', 6, 0)
]

for txt, r, c in buttons:
tk.Button(frame, text=txt, padx=15, pady=5, width=3, command=lambda t=txt: click(t)).grid(row=r, column=c, pady=2)

tk.Button(frame, text="Clear", padx=15, pady=5, width=12, command=clear).grid(row=6, column=1, columnspan=2, pady=2)
tk.Button(frame, text="=", padx=15, pady=5, width=9, command=equal).grid(row=7, column=0, columnspan=3, pady=2)

win.mainloop()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant