线程实时记录

时间:2016-11-13 15:23:29

标签: python multithreading pyqt pyqt5

我正在编写一个简单的脚本来调整照片大小。我希望有一个带文本字段的小部件,在调整每个文件大小后会显示消息。

#!/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_())

到目前为止,调整所有文件大小后,所有消息都会立即出现。有没有办法一个一个地做(我的意思是文件调整大小,显示消息等)?

1 个答案:

答案 0 :(得分:2)

下面是你想要的脚本的重写。

但请注意,这非常简单,并且不会太努力以确保线程安全。 setItems方法只是传递给它的数据的浅表副本 - 这对于不可变对象列表来说真的没问题。您还必须确保在工作线程中不执行任何gui操作,其中包括对pixmaps的操作。如果您想操纵图像,请使用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_())