آلة حاسبة بلغة Python مع الألوان
إليك كود لآلة حاسبة بسيطة باستخدام مكتبة tkinter مع إضافة ألوان جميلة:
python
import tkinter as tk
from tkinter import messagebox
# إنشاء النافذة الرئيسية
root = tk.Tk()
root.title("آلة حاسبة ملونة")
root.geometry("300x400")
root.resizable(False, False)
root.configure(bg='#2C3E50')
# متغير لتخزين العملية الحسابية
calculation = ""
# دالة لإضافة عناصر إلى العملية الحسابية
def add_to_calculation(symbol):
global calculation
calculation += str(symbol)
text_result.delete(1.0, "end")
text_result.insert(1.0, calculation)
# دالة لتقييم العملية الحسابية
def evaluate_calculation():
global calculation
try:
calculation = str(eval(calculation))
text_result.delete(1.0, "end")
text_result.insert(1.0, calculation)
except:
clear_field()
text_result.insert(1.0, "خطأ")
# دالة لمسح الحقل
def clear_field():
global calculation
calculation = ""
text_result.delete(1.0, "end")
# إنشاء حقل النتيجة
text_result = tk.Text(root, height=2, width=16, font=('Arial', 24),
bg='#34495E', fg='#ECF0F1', bd=0,
highlightthickness=0, padx=10, pady=10)
text_result.grid(columnspan=5, padx=10, pady=10)
# أزرار الآلة الحاسبة مع ألوان
buttons = [
('7', '#3498DB'), ('8', '#3498DB'), ('9', '#3498DB'), ('/', '#E74C3C'),
('4', '#3498DB'), ('5', '#3498DB'), ('6', '#3498DB'), ('*', '#E74C3C'),
('1', '#3498DB'), ('2', '#3498DB'), ('3', '#3498DB'), ('-', '#E74C3C'),
('0', '#3498DB'), ('.', '#3498DB'), ('=', '#2ECC71'), ('+', '#E74C3C')
]
# وظيفة لإنشاء الأزرار
row_val = 2
col_val = 0
for (text, color) in buttons:
if text == '=':
btn = tk.Button(root, text=text, command=evaluate_calculation,
width=5, height=2, font=('Arial', 14),
bg=color, fg='white', bd=0,
activebackground='#27AE60', activeforeground='white')
else:
btn = tk.Button(root, text=text,
command=lambda t=text: add_to_calculation(t),
width=5, height=2, font=('Arial', 14),
bg=color, fg='white', bd=0,
activebackground='#2980B9', activeforeground='white')
btn.grid(row=row_val, column=col_val, padx=5, pady=5)
col_val += 1
if col_val > 3:
col_val = 0
row_val += 1
# زر المسح
btn_clear = tk.Button(root, text="C", command=clear_field,
width=23, height=2, font=('Arial', 14),
bg='#E67E22', fg='white', bd=0,
activebackground='#D35400', activeforeground='white')
btn_clear.grid(columnspan=4, row=row_val+1, padx=5, pady=5)
# تشغيل التطبيق
root.mainloop()شرح الكود:
الواجهة الرئيسية: نافذة باللون الأزرق الداكن (#2C3E50)
حقل النتيجة: خلفية زرقاء متوسطة (#34495E) ونص أبيض
أزرار الأرقام: زرقاء (#3498DB)
أزرار العمليات الحسابية: حمراء (#E74C3C)
زر المساواة: أخضر (#2ECC71)
زر المسح: برتقالي (#E67E22)
المميزات:
تصميم ملون وجذاب
أزرار تفاعلية تتغير ألوانها عند الضغط
معالجة الأخطاء عند إدخال عمليات غير صحيحة
واجهة سهلة الاستخدام
يمكنك تعديل الألوان حسب رغبتك بتغيير قيم الألوان في الكود.
