Я разрабатываю настольное приложение на основе PYQT5, которое слушает данные с сервера OPC KEPWARE и отображает самое последнее изображение (с камеры или скриншота) на QLabel с использованием QPixMap. Там нет взаимодействия с пользователем (без кнопок, кликов и т. Д.). Приложение также регистрирует данные в базе данных в фоновом режиме. Однако через некоторое время-иногда 1 час, иногда 5-6 часов-графический интерфейс замерзает: Qlabel прекращает обновлять, и последнее изображение остается застрявшим на экране. Бэкэнд продолжает работать, регистрация и вычисления базы данных продолжаются нормально. Нет никаких исключений, и все методы обернуты в блоки Try-Except.
Вот что я пробовал до сих пор:
Убедитесь, что мой Exe (построенный из Pyinstaller) имеет необходимые лицензии Kepware
. /> Считается утечкой памяти или медленной смертью потока графического интерфейса, но не имеет убедительных доказательств < /p>
Проверено без бесконечных петлей или блокировки в основной потоке < /p>
Я ищу помощь от отладки этого замораживания GUI - почему qlabel.setpixmap () молча остановил обновлять, даже хотя остальные программы продолжают работать? Что еще мне следует проверить? < /P>
import os
import asyncio
import datetime
from qasync import asyncSlot
from PyQt5 import uic
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QPixmap, QImage
from PyQt5.QtWidgets import QMainWindow, QLabel
from src.app_data import AppData
from src.kepware_helper import KepwareHelper
class MainScreen(QMainWindow):
def __init__(self):
try:
super(MainScreen, self).__init__()
current_dir = os.getcwd()
print("current dir", current_dir)
ui_file_path = os.path.join(current_dir, "assets", "gui.ui")
uic.loadUi(ui_file_path, self)
self.app_data = AppData()
self.pixmap = QPixmap(os.path.join(current_dir, "assets", "initpixmap.png"))
self.initialize_ui_elements()
QTimer.singleShot(0, self.initialize_thread_definitions)
except Exception as e:
print("Error initializing MainScreen:", str(e))
self.app_data.get_log_helper().append_log(f"Error initializing MainScreen: {str(e)}")
def closeEvent(self, event):
try:
self.update_status('Closing the application...')
if self.kepware_helper:
self.kepware_helper.unsubscribe()
self.kepware_helper.disconnect_kepware()
os._exit(0)
return super().closeEvent(event)
except Exception as e:
print("Error closing MainScreen:", str(e))
self.app_data.get_log_helper().append_log(f"Error closing MainScreen: {str(e)}")
def initialize_ui_elements(self):
try:
self.modelLabel = self.findChild(QLabel, "modelLabel")
self.dummyLabel = self.findChild(QLabel, "dummyLabel")
self.dateLabel = self.findChild(QLabel, "dateLabel")
self.statusLabel = self.findChild(QLabel, "statusLabel")
self.pixmapLabel = self.findChild(QLabel, "pixmapLabel")
self.pixmapLabel.setPixmap(self.pixmap)
self.pixmapLabel.setScaledContents(True)
except Exception as e:
print("Error initializing UI elements:", str(e))
self.app_data.get_log_helper().append_log(f"Error initializing UI elements: {str(e)}")
@asyncSlot()
async def initialize_thread_definitions(self):
try:
self.kepware_helper = KepwareHelper(
self.update_status,
self.update_pixmap,
self.result_handler,
self.update_model_label,
self.update_dummy_label,
)
await self.kepware_helper.connect_kepware()
await self.kepware_helper.initialize_nodes()
await self.kepware_helper.subscribe()
except Exception as e:
print("Error initializing threads:", str(e))
self.app_data.get_log_helper().append_log(f"Error initializing threads: {str(e)}")
def update_pixmap(self, qImage = None):
try:
# Get the size of the settings image label
if qImage:
self.settingImageWidth = qImage.rect().width()
self.settingImageHeight = qImage.rect().height()
self.pixmap = QPixmap.fromImage(qImage)
self.settingLastImage = qImage
else:
self.pixmap = QPixmap()
self.pixmap.fill(Qt.transparent)
original_pixmap = self.pixmap
original_width = original_pixmap.width()
original_height = original_pixmap.height()
label_width = self.pixmapLabel.width()
label_height = self.pixmapLabel.height()
if original_width == 0 or original_height == 0 or label_width == 0 or label_height == 0:
self.pixmapLabel.clear()
return
aspect_ratio = original_width / original_height
if label_width / label_height > aspect_ratio:
new_width = int(label_height * aspect_ratio)
new_height = label_height
else:
new_width = label_width
new_height = int(label_width / aspect_ratio)
scaled_pixmap = original_pixmap.scaled(new_width, new_height, Qt.KeepAspectRatio)
self.pixmapLabel.setPixmap(scaled_pixmap)
self.pixmapLabel.setScaledContents(True)
except Exception as e:
print("Error updating pixmap:", str(e))
self.app_data.get_log_helper().append_log(f"Error updating pixmap: {str(e)}")
def update_status(self, message):
try:
self.statusLabel.setText(message)
except Exception as e:
print("Error updating status:", str(e))
self.app_data.get_log_helper().append_log(f"Error updating status: {str(e)}")
def result_handler(self, result):
try:
if result:
self.statusLabel.setText("Sonuç: Başarılı")
else:
self.statusLabel.setText("Sonuç: Başarısız")
self.dateLabel.setText(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
except Exception as e:
print("Error handling result:", str(e))
self.app_data.get_log_helper().append_log(f"Error handling result: {str(e)}")
def update_model_label(self, model):
try:
if model and model != "":
self.modelLabel.setText(str(model))
else:
self.modelLabel.setText("Ürün Bekleniyor....")
except Exception as e:
print("Error updating model label:", str(e))
self.app_data.get_log_helper().append_log(f"Error updating model label: {str(e)}")
def update_dummy_label(self, dummy_no):
try:
if dummy_no and dummy_no != "":
self.dummyLabel.setText(str(dummy_no))
else:
self.dummyLabel.setText("Ürün Bekleniyor....")
except Exception as e:
print("Error updating dummy label:", str(e))
self.app_data.get_log_helper().append_log(f"Error updating dummy label: {str(e)}")
def convert_to_qImage(self, image):
try:
height, width, channel = image.shape
bytesPerLine = 3 * width
qImg = QImage(image.data, width, height, bytesPerLine, QImage.Format_RGB888)
return qImg
except Exception as e:
print("Error converting to QImage:", str(e))
self.app_data.get_log_helper().append_log(f"Error converting to QImage: {str(e)}")
return None
Подробнее здесь: https://stackoverflow.com/questions/796 ... o-exceptio
Gui Pyqt5 замораживает в нерабочее время, пока фоновая обработка продолжается (никаких исключений не брошено) ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Перехват исключений Kotlin в обработчике исключений замораживает основной поток
Anonymous » » в форуме Android - 0 Ответы
- 19 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Перехват исключений Kotlin в обработчике исключений замораживает основной поток
Anonymous » » в форуме Android - 0 Ответы
- 17 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Перехват исключений Kotlin в обработчике исключений замораживает основной поток
Anonymous » » в форуме Android - 0 Ответы
- 31 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Перехват исключений Kotlin в обработчике исключений замораживает основной поток
Anonymous » » в форуме Android - 0 Ответы
- 10 Просмотры
-
Последнее сообщение Anonymous
-