CREATE SIMPLE CALCULATOR
Certainly! Here's an example of a simple calculator GUI (Graphical User Interface) application using the `tkinter` library in Python:
```python
import tkinter as tk
def btn_click(event):
text = event.widget.cget("text")
if text == "=":
try:
result = str(eval(screen.get()))
screen.set(result)
except Exception as e:
screen.set("Error")
elif text == "C":
screen.set("")
else:
screen.set(screen.get() + text)
root = tk.Tk()
root.geometry("400x600")
root.title("Calculator")
screen = tk.StringVar()
screen.set("")
entry = tk.Entry(root, textvar=screen, font="Arial 30 bold", justify="right")
entry.pack(fill=tk.BOTH, ipadx=8, padx=10, pady=10)
buttons = [
("7", "8", "9", "/"),
("4", "5", "6", "*"),
("1", "2", "3", "-"),
("0", ".", "=", "+"),
("C")
]
for button_row in buttons:
frame = tk.Frame(root)
frame.pack(fill=tk.BOTH)
for button_text in button_row:
btn = tk.Button(frame, text=button_text, font="Arial 20 bold")
btn.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
btn.bind("<Button-1>", btn_click)
root.mainloop()
```
Copy and paste this code into a `.py` file and run it using a Python interpreter. This program will create a basic calculator GUI where you can perform arithmetic calculations. It includes buttons for digits (0-9), decimal point (.), basic arithmetic operations (+, -, *, /), equal (=) to calculate the result, and a clear (C) button to clear the input.
Please note that this example is a basic calculator and may not handle all edge cases or error handling scenarios. You can further enhance this GUI by adding features like parentheses, scientific functions, and more advanced operations.

No comments:
Post a Comment