في هذا المثال سنقوم بمحاكاة عملية معالجة مجموعة من الصور (وهمية) باستخدام شريط التقدم QProgressBar
.
الفكرة هنا مختلفة عن التحميل، لأننا سنتعامل مع عدد من الملفات ونحسب التقدم بناءً على عدد الصور المعالجة.
الكود
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QProgressBar, QPushButton, QListWidget
from PyQt5.QtCore import QTimer
import time
class ImageProcessingSimulator(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("محاكاة معالجة الصور")
self.resize(400, 300)
layout = QVBoxLayout()
# عنوان
self.label = QLabel("اضغط على الزر لبدء المعالجة")
layout.addWidget(self.label)
# قائمة الصور الوهمية
self.image_list = QListWidget()
self.images = [f"صورة_{i}.jpg" for i in range(1, 11)]
self.image_list.addItems(self.images)
layout.addWidget(self.image_list)
# شريط التقدم
self.progress_bar = QProgressBar()
self.progress_bar.setValue(0)
layout.addWidget(self.progress_bar)
# زر البدء
self.start_button = QPushButton("بدء المعالجة")
self.start_button.clicked.connect(self.start_processing)
layout.addWidget(self.start_button)
self.setLayout(layout)
# المؤقت
self.timer = QTimer()
self.timer.timeout.connect(self.process_next_image)
self.current_index = 0
def start_processing(self):
self.current_index = 0
self.progress_bar.setValue(0)
self.label.setText("جاري معالجة الصور...")
self.timer.start(500) # تحديث كل نصف ثانية
def process_next_image(self):
if self.current_index < len(self.images):
# محاكاة المعالجة
time.sleep(0.1) # وقت المعالجة
self.image_list.setCurrentRow(self.current_index)
progress = int(((self.current_index + 1) / len(self.images)) * 100)
self.progress_bar.setValue(progress)
self.current_index += 1
else:
self.timer.stop()
self.label.setText("✅ تم الانتهاء من معالجة جميع الصور")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = ImageProcessingSimulator()
window.show()
sys.exit(app.exec_())
ما المختلف هذا الدرس؟
1-بدل تحميل ملف واحد، نحن نعالج مجموعة صور.2-نستخدم
QListWidget
لعرض أسماء الصور وتحديد الصورة التي يتم معالجتها حاليًا.3-نسبة التقدم تعتمد على عدد الصور المعالجة، وليس زمن محدد.
4-الفكرة فريدة ويمكن تطويرها لاحقًا لإضافة:
- اختيار مجلد صور حقيقي.
- تعديل أو حفظ الصور بعد المعالجة.
سنتعلم في التمرين القادم كيف نعمل النسخة المطورة من محاكاة معالجة الصور بحيث يتم حفظ سجل كل صورة مع وقت بدء المعالجة ووقت الانتهاء في ملف Excel.
النسخة المطورة – حفظ سجل المعالجة في Excel
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QProgressBar, QPushButton, QListWidget
from PyQt5.QtCore import QTimer
from datetime import datetime
import time
import openpyxl
import os
class ImageProcessingSimulator(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("محاكاة معالجة الصور مع حفظ السجل")
self.resize(450, 350)
layout = QVBoxLayout()
# عنوان
self.label = QLabel("اضغط على الزر لبدء المعالجة")
layout.addWidget(self.label)
# قائمة الصور الوهمية
self.image_list = QListWidget()
self.images = [f"صورة_{i}.jpg" for i in range(1, 11)]
self.image_list.addItems(self.images)
layout.addWidget(self.image_list)
# شريط التقدم
self.progress_bar = QProgressBar()
self.progress_bar.setValue(0)
layout.addWidget(self.progress_bar)
# زر البدء
self.start_button = QPushButton("بدء المعالجة")
self.start_button.clicked.connect(self.start_processing)
layout.addWidget(self.start_button)
self.setLayout(layout)
# المؤقت
self.timer = QTimer()
self.timer.timeout.connect(self.process_next_image)
self.current_index = 0
# ملف Excel
self.excel_file = "سجل_معالجة_الصور.xlsx"
self.create_excel_file()
def create_excel_file(self):
"""إنشاء ملف Excel إذا لم يكن موجود"""
if not os.path.exists(self.excel_file):
wb = openpyxl.Workbook()
ws = wb.active
ws.append(["اسم الصورة", "وقت البدء", "وقت الانتهاء"])
wb.save(self.excel_file)
def start_processing(self):
self.current_index = 0
self.progress_bar.setValue(0)
self.label.setText("جاري معالجة الصور...")
self.start_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
self.timer.start(500)
def process_next_image(self):
if self.current_index < len(self.images):
current_image = self.images[self.current_index]
start_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# محاكاة المعالجة
time.sleep(0.1)
self.image_list.setCurrentRow(self.current_index)
# تحديث شريط التقدم
progress = int(((self.current_index + 1) / len(self.images)) * 100)
self.progress_bar.setValue(progress)
# حفظ في Excel
end_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
self.save_to_excel(current_image, start_time, end_time)
self.current_index += 1
else:
self.timer.stop()
self.label.setText("✅ تم الانتهاء من معالجة جميع الصور")
def save_to_excel(self, image_name, start_time, end_time):
wb = openpyxl.load_workbook(self.excel_file)
ws = wb.active
ws.append([image_name, start_time, end_time])
wb.save(self.excel_file)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = ImageProcessingSimulator()
window.show()
sys.exit(app.exec_())
الميزات المضافة
1-لكل صورة يتم حفظ:- اسم الصورة.
- وقت بدء المعالجة.
- وقت الانتهاء.
سجل_معالجة_الصور.xlsx
.3- يمكن الرجوع للسجل لاحقًا لمعرفة ترتيب ووقت معالجة كل صورة.
سنضيف ميزة عرض السجل في جدول داخل البرنامج باستخدام QTableWidget
بحيث تقدر تشوف بيانات Excel من داخل التطبيق بدون ما تفتحه خارجيًا.
النسخة النهائية – عرض سجل معالجة الصور داخل البرنامج
import sys
from PyQt5.QtWidgets import (
QApplication, QWidget, QVBoxLayout, QLabel, QProgressBar, QPushButton,
QListWidget, QTableWidget, QTableWidgetItem, QDialog
)
from PyQt5.QtCore import QTimer
from datetime import datetime
import time
import openpyxl
import os
class ImageProcessingSimulator(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("محاكاة معالجة الصور مع عرض السجل")
self.resize(500, 400)
layout = QVBoxLayout()
# عنوان
self.label = QLabel("اضغط على الزر لبدء المعالجة")
layout.addWidget(self.label)
# قائمة الصور
self.image_list = QListWidget()
self.images = [f"صورة_{i}.jpg" for i in range(1, 11)]
self.image_list.addItems(self.images)
layout.addWidget(self.image_list)
# شريط التقدم
self.progress_bar = QProgressBar()
layout.addWidget(self.progress_bar)
# أزرار التحكم
self.start_button = QPushButton("بدء المعالجة")
self.start_button.clicked.connect(self.start_processing)
layout.addWidget(self.start_button)
self.show_log_button = QPushButton("عرض سجل المعالجة")
self.show_log_button.clicked.connect(self.show_log)
layout.addWidget(self.show_log_button)
self.setLayout(layout)
# مؤقت
self.timer = QTimer()
self.timer.timeout.connect(self.process_next_image)
self.current_index = 0
# ملف Excel
self.excel_file = "سجل_معالجة_الصور.xlsx"
self.create_excel_file()
def create_excel_file(self):
if not os.path.exists(self.excel_file):
wb = openpyxl.Workbook()
ws = wb.active
ws.append(["اسم الصورة", "وقت البدء", "وقت الانتهاء"])
wb.save(self.excel_file)
def start_processing(self):
self.current_index = 0
self.progress_bar.setValue(0)
self.label.setText("جاري معالجة الصور...")
self.timer.start(500)
def process_next_image(self):
if self.current_index < len(self.images):
current_image = self.images[self.current_index]
start_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
time.sleep(0.1) # محاكاة المعالجة
self.image_list.setCurrentRow(self.current_index)
progress = int(((self.current_index + 1) / len(self.images)) * 100)
self.progress_bar.setValue(progress)
end_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
self.save_to_excel(current_image, start_time, end_time)
self.current_index += 1
else:
self.timer.stop()
self.label.setText("✅ تم الانتهاء من معالجة جميع الصور")
def save_to_excel(self, image_name, start_time, end_time):
wb = openpyxl.load_workbook(self.excel_file)
ws = wb.active
ws.append([image_name, start_time, end_time])
wb.save(self.excel_file)
def show_log(self):
"""عرض السجل في نافذة جدول"""
if not os.path.exists(self.excel_file):
return
wb = openpyxl.load_workbook(self.excel_file)
ws = wb.active
dialog = QDialog(self)
dialog.setWindowTitle("سجل معالجة الصور")
dialog.resize(500, 300)
table = QTableWidget()
table.setRowCount(ws.max_row - 1)
table.setColumnCount(ws.max_column)
table.setHorizontalHeaderLabels(["اسم الصورة", "وقت البدء", "وقت الانتهاء"])
for row in range(2, ws.max_row + 1):
for col in range(1, ws.max_column + 1):
value = ws.cell(row=row, column=col).value
table.setItem(row - 2, col - 1, QTableWidgetItem(str(value)))
layout = QVBoxLayout()
layout.addWidget(table)
dialog.setLayout(layout)
dialog.exec_()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = ImageProcessingSimulator()
window.show()
sys.exit(app.exec_())
التطويرات المضافة
1-زر "عرض سجل المعالجة" يفتح نافذة جديدة.2-داخل النافذة يتم عرض البيانات من ملف Excel في جدول مرتب باستخدام
QTableWidget
.3-الجدول يعرض:
- اسم الصورة.
- وقت بدء المعالجة.
- وقت الانتهاء.