مدونة المعرفة
الخميس، 3 أبريل 2025
import tkinter as tk
from tkinter import ttk, filedialog
from PIL import Image, ImageDraw, ImageTk
import os
class SimplePaintWithSave:
def __init__(self, root):
self.root = root
self.root.title("برنامج الرسم مع الحفظ")
# إعدادات الرسم
self.canvas_width = 800
self.canvas_height = 600
self.current_color = "black"
self.line_width = 3
# إنشاء عناصر الواجهة
self.create_widgets()
# متغيرات تتبع الرسم
self.last_x = None
self.last_y = None
self.drawing = False
# صورة الخلفية للتتبع
self.image = Image.new("RGB", (self.canvas_width, self.canvas_height), "white")
self.draw = ImageDraw.Draw(self.image)
def create_widgets(self):
# شريط الأدوات العلوي
toolbar = ttk.Frame(self.root)
toolbar.pack(fill=tk.X, padx=5, pady=5)
# أزرار الألوان
colors = ["black", "red", "green", "blue", "yellow", "purple"]
for color in colors:
btn = tk.Button(toolbar, bg=color, width=3,
command=lambda c=color: self.set_color(c))
btn.pack(side=tk.LEFT, padx=2)
# زر المسح
clear_btn = ttk.Button(toolbar, text="مسح الكل", command=self.clear_canvas)
clear_btn.pack(side=tk.LEFT, padx=5)
# زر الحفظ
save_btn = ttk.Button(toolbar, text="حفظ كصورة", command=self.save_image)
save_btn.pack(side=tk.RIGHT)
# لوحة الرسم
self.canvas = tk.Canvas(self.root, width=self.canvas_width,
height=self.canvas_height, bg="white")
self.canvas.pack(fill=tk.BOTH, expand=True)
# ربط أحداث الماوس
self.canvas.bind("", self.start_drawing)
self.canvas.bind("", self.draw)
self.canvas.bind("", self.stop_drawing)
def set_color(self, new_color):
self.current_color = new_color
def start_drawing(self, event):
self.drawing = True
self.last_x = event.x
self.last_y = event.y
def draw(self, event):
if self.drawing:
# الرسم على Canvas
self.canvas.create_line(
self.last_x, self.last_y, event.x, event.y,
width=self.line_width,
fill=self.current_color,
capstyle=tk.ROUND,
smooth=True
)
# الرسم على صورة PIL
self.draw.line(
[(self.last_x, self.last_y), (event.x, event.y)],
fill=self.current_color,
width=self.line_width
)
self.last_x = event.x
self.last_y = event.y
def stop_drawing(self, event):
self.drawing = False
def clear_canvas(self):
self.canvas.delete("all")
self.image = Image.new("RGB", (self.canvas_width, self.canvas_height), "white")
self.draw = ImageDraw.Draw(self.image)
def save_image(self):
try:
# اختيار مكان الحفظ
file_path = filedialog.asksaveasfilename(
defaultextension=".png",
filetypes=[("PNG Image", "*.png"), ("JPEG Image", "*.jpg"), ("All Files", "*.*")],
title="حفظ الصورة"
)
if file_path:
# حفظ الصورة بنفس نوع الملف المحدد
if file_path.lower().endswith('.jpg') or file_path.lower().endswith('.jpeg'):
self.image.save(file_path, "JPEG", quality=95)
else:
self.image.save(file_path, "PNG")
print(f"تم الحفظ بنجاح: {os.path.basename(file_path)}")
tk.messagebox.showinfo("تم الحفظ", f"تم حفظ الصورة في:\n{file_path}")
except Exception as e:
tk.messagebox.showerror("خطأ", f"حدث خطأ أثناء الحفظ:\n{str(e)}")
if __name__ == "__main__":
root = tk.Tk()
root.geometry("900x700")
root.minsize(400, 300)
# تحسين المظهر
style = ttk.Style()
style.configure("TButton", padding=5)
app = SimplePaintWithSave(root)
root.mainloop()
import tkinter as tk
from tkinter import ttk
class SimplePaint:
def __init__(self, root):
self.root = root
self.root.title("برنامج الرسم - Python 3.13.2")
# إعدادات أساسية
self.canvas_width = 800
self.canvas_height = 600
self.current_color = "black"
self.line_width = 3
# إنشاء عناصر الواجهة
self.create_widgets()
# متغيرات تتبع الرسم
self.last_x = None
self.last_y = None
def create_widgets(self):
# شريط الأدوات
toolbar = ttk.Frame(self.root)
toolbar.pack(fill=tk.X, padx=5, pady=5)
# أزرار الألوان
colors = ["black", "red", "green", "blue", "yellow", "purple"]
for color in colors:
btn = tk.Button(toolbar, bg=color, width=3,
command=lambda c=color: self.set_color(c))
btn.pack(side=tk.LEFT, padx=2)
# زر المسح
clear_btn = ttk.Button(toolbar, text="مسح الكل", command=self.clear_canvas)
clear_btn.pack(side=tk.RIGHT)
# لوحة الرسم
self.canvas = tk.Canvas(self.root, width=self.canvas_width,
height=self.canvas_height, bg="white")
self.canvas.pack(fill=tk.BOTH, expand=True)
# ربط أحداث الماوس
self.canvas.bind("", self.start_drawing)
self.canvas.bind("", self.draw)
self.canvas.bind("", self.stop_drawing)
def set_color(self, new_color):
self.current_color = new_color
def start_drawing(self, event):
self.last_x = event.x
self.last_y = event.y
def draw(self, event):
if self.last_x and self.last_y:
self.canvas.create_line(
self.last_x, self.last_y, event.x, event.y,
width=self.line_width,
fill=self.current_color,
capstyle=tk.ROUND,
smooth=True
)
self.last_x = event.x
self.last_y = event.y
def stop_drawing(self, event):
self.last_x = None
self.last_y = None
def clear_canvas(self):
self.canvas.delete("all")
if __name__ == "__main__":
# إعداد النافذة الرئيسية
root = tk.Tk()
# تحسينات للتشغيل على 3.13.2
root.tk.call('tk', 'scaling', 1.5) # تحسين الدقة للشاشات الحديثة
# ضبط حجم النافذة
root.geometry("900x700")
root.minsize(400, 300)
# تشغيل التطبيق
app = SimplePaint(root)
# التحقق من الإصدار
print(f"يعمل على Python {tk.TkVersion}")
root.mainloop()
الخميس، 27 مارس 2025
الأربعاء، 26 مارس 2025
الاثنين، 24 مارس 2025
الاشتراك في:
الرسائل (Atom)