Untitled
unknown
plain_text
2 years ago
1.7 kB
13
Indexable
سلام! البته به دلیل محدودیتها در تایپ متون فارسی در این محیط، ممکن است نویسهها بهطور صحیح نمایش داده نشوند. اما میتوانیم به زبان انگلیسی ادامه دهیم.
Here's a simple example of a basic calculator using Tkinter in Python:
```python
import tkinter as tk
def on_click(button_value):
current = entry.get()
entry.delete(0, tk.END)
entry.insert(tk.END, current + str(button_value))
def clear_entry():
entry.delete(0, tk.END)
def calculate():
try:
result = eval(entry.get())
entry.delete(0, tk.END)
entry.insert(tk.END, str(result))
except Exception as e:
entry.delete(0, tk.END)
entry.insert(tk.END, "Error")
# Create the main window
root = tk.Tk()
root.title("Simple Calculator")
# Entry widget to display input and output
entry = tk.Entry(root, width=20, borderwidth=5)
entry.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
# Define buttons
buttons = [
'7', '8', '9', '/',
'4', '5', '6', '*',
'1', '2', '3', '-',
'0', 'C', '=', '+'
]
# Add buttons to the grid
row_val = 1
col_val = 0
for button in buttons:
tk.Button(root, text=button, padx=20, pady=20, command=lambda b=button: on_click(b) if b != '=' else calculate() if b == '=' else clear_entry()).grid(row=row_val, column=col_val)
col_val += 1
if col_val > 3:
col_val = 0
row_val += 1
# Run the main loop
root.mainloop()
```
این کد یک رابط کاربری ساده برای یک ماشین حساب ایجاد میکند. از Tkinter برای ایجاد پنجره و عناصر ویزوالی استفاده شده است.Editor is loading...
Leave a Comment