Sure! Below is a simple calculator app written in Python using the Tkinter library for the graphical user interface. This app will allow the user to perform basic arithmetic operations like addition, subtraction, multiplication, and division.
python Copy code
import tkinter as tk
class Calculator:
def __init__(self, root):
self.root = root
self.root.title("Simple Calculator")
# Entry widget for displaying calculations
self.entry = tk.Entry(root, width=16, font=('Arial', 24), bd=10, insertwidth=2, bg='powder blue', justify='right')
self.entry.grid(row=0, column=0, columnspan=4)
# Create buttons
self.create_buttons()
def create_buttons(self):
buttons = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'0', '.', '=', '+'
]
row_val = 1
col_val = 0
for button in buttons:
action = lambda x=button: self.on_button_click(x)
tk.Button(self.root, text=button, padx=20, pady=20, font=('Arial', 18), command=action).grid(row=row_val, column=col_val)
col_val += 1
if col_val > 3:
col_val = 0
row_val += 1
# Clear button
tk.Button(self.root, text='C', padx=20, pady=20, font=('Arial', 18), command=self.clear).grid(row=row_val, column=0, columnspan=4)
def on_button_click(self, char):
if char == '=':
try:
result = str(eval(self.entry.get()))
self.entry.delete(0, tk.END)
self.entry.insert(tk.END, result)
except Exception as e:
self.entry.delete(0, tk.END)
self.entry.insert(tk.END, "Error")
else:
self.entry.insert(tk.END, char)
def clear(self):
self.entry.delete(0, tk.END)
if __name__ == "__main__":
root = tk.Tk()
calculator = Calculator(root)
root.mainloop()
You can modify the code to add more features or improve the design! If you have any questions or need help with something else, feel free to ask!