且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

线程实时日志记录

更新时间:2023-01-18 18:12:07

下面是您想要的脚本的重新编写.

Below is a re-write of your script that should do want you want.

但是请注意,这非常简单,并且不会为确保线程安全而过分努力. setItems方法只是对传递给它的数据进行浅表复制-仅对于不可变对象列表确实可以.您还必须确保永远不要在辅助线程内执行任何gui操作,包括对pixmap进行的操作.如果要操作图像,请使用QImage. (并且,如果您想知道如何停止正在运行的线程,请参见例如此答案).

But note that this is quite simplistic, and doesn't try too hard to ensure thread-safety. The setItems method just makes a shallow copy of the data passed to it - which is only really okay for a list of immutable objects. You also must make sure you never do any gui operations inside the worker thread, which includes operations on pixmaps. If you want to manipulate images, use QImage. (And if you want to know how to stop a running thread, see for example this answer).

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import time, sys
from PyQt5.QtCore import pyqtSignal, QThread
from PyQt5.QtWidgets import (
    QApplication, QPushButton, QTextEdit, QWidget, QVBoxLayout
    )

class Thread(QThread):
    log = pyqtSignal(str)

    def __init__(self, parent=None):
        super(Thread, self).__init__(parent)
        self._items = []

    def setItems(self, items):
        if not self.isRunning():
            self._items[:] = items

    def run(self):
        for item in self._items:
            time.sleep(1)
            self.log.emit('processing: %s' % item)

class Widget(QWidget):
    def __init__(self):
        super().__init__()
        self.ui()
        self._worker = Thread(self)
        self._worker.log.connect(self.toLog)
        self._worker.started.connect(lambda: self.toLog('start'))
        self._worker.finished.connect(lambda: self.toLog('finished'))

    def process(self):
        items = ['Image%02d.png' % i for i in range(10)]
        self._worker.setItems(items)
        self._worker.start()

    def ui(self):
        self.LogOutputTxt = QTextEdit()
        self.LogOutputTxt.setReadOnly(True)
        startBtn = QPushButton('Start')
        startBtn.clicked.connect(self.start)
        layout = QVBoxLayout()
        layout.addWidget(self.LogOutputTxt)
        layout.addWidget(startBtn)
        self.setLayout(layout)
        self.resize(400, 300)
        self.show()

    def start(self):
        if not self._worker.isRunning():
            self.process()

    def toLog(self, txt):
        self.LogOutputTxt.append(txt)

if __name__ == '__main__':

    app = QApplication(sys.argv)
    ui = Widget()
    sys.exit(app.exec_())