线程实时日志记录

我正在编写一个用于调整照片大小的简单脚本.我想要一个带有文本字段的小部件,在调整每个文件的大小后,消息会出现在其中.

#!/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)
    def test(self, i):
        time.sleep(1)
        self.log.emit(str(i))

class Widget(QWidget):
    def __init__(self):
        super().__init__()
        self.ui()
    def process(self):
        self.toLog('some text...')
        worker = Thread()
        worker.log.connect(self.toLog)
        for i in range(1, 5):
            worker.test(i)
    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):
        self.toLog('start')
        self.process()
    def toLog(self, txt):
        self.LogOutputTxt.append(txt)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ui = Widget()
    sys.exit(app.exec_())

到目前为止,所有文件调整大小后,所有消息立即出现.有什么办法可以一一完成(我是说文件大小已调整,消息显示等)?

解决方法:

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

但是请注意,这非常简单,并且不会过分努力以确保线程安全. setItems方法只是对传递给它的数据进行浅表复制-仅对不可变对象列表确实可行.您还必须确保永远不要在辅助线程中执行任何gui操作,包括对pixmap的操作.如果要操作图像,请使用QImage. (如果您想知道如何停止正在运行的线程,请参见例如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_())
上一篇:Delphi在做窗体派生时提示Resource TForm2 not found


下一篇:代码质量打油诗 摘自(迷途@书童 )