<i id='W3ZC6'><tr id='W3ZC6'><dt id='W3ZC6'><q id='W3ZC6'><span id='W3ZC6'><b id='W3ZC6'><form id='W3ZC6'><ins id='W3ZC6'></ins><ul id='W3ZC6'></ul><sub id='W3ZC6'></sub></form><legend id='W3ZC6'></legend><bdo id='W3ZC6'><pre id='W3ZC6'><center id='W3ZC6'></center></pre></bdo></b><th id='W3ZC6'></th></span></q></dt></tr></i><div id='W3ZC6'><tfoot id='W3ZC6'></tfoot><dl id='W3ZC6'><fieldset id='W3ZC6'></fieldset></dl></div>
    <bdo id='W3ZC6'></bdo><ul id='W3ZC6'></ul>

<small id='W3ZC6'></small><noframes id='W3ZC6'>

<legend id='W3ZC6'><style id='W3ZC6'><dir id='W3ZC6'><q id='W3ZC6'></q></dir></style></legend>

    <tfoot id='W3ZC6'></tfoot>

        线程实时记录

        时间:2023-08-04
        <tfoot id='IjWQg'></tfoot>

                <bdo id='IjWQg'></bdo><ul id='IjWQg'></ul>

                  <tbody id='IjWQg'></tbody>
                • <i id='IjWQg'><tr id='IjWQg'><dt id='IjWQg'><q id='IjWQg'><span id='IjWQg'><b id='IjWQg'><form id='IjWQg'><ins id='IjWQg'></ins><ul id='IjWQg'></ul><sub id='IjWQg'></sub></form><legend id='IjWQg'></legend><bdo id='IjWQg'><pre id='IjWQg'><center id='IjWQg'></center></pre></bdo></b><th id='IjWQg'></th></span></q></dt></tr></i><div id='IjWQg'><tfoot id='IjWQg'></tfoot><dl id='IjWQg'><fieldset id='IjWQg'></fieldset></dl></div>
                  <legend id='IjWQg'><style id='IjWQg'><dir id='IjWQg'><q id='IjWQg'></q></dir></style></legend>

                • <small id='IjWQg'></small><noframes id='IjWQg'>

                  本文介绍了线程实时记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

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

                  I'm writing a simple script for resizing photos. I'd like to have a widget with text-field in which messages appear after resizing each file.

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

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

                  So far all the messages appear at once after all files are resized. Is there any way to do it one by one (I mean file resized, message displayed, etc.)?

                  推荐答案

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

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

                  但请注意,这非常简单,并且不会太努力地确保线程安全.setItems 方法只是制作传递给它的数据的浅表副本 - 这仅适用于不可变对象列表.您还必须确保您永远不会在工作线程中执行任何 gui 操作,其中包括对像素图的操作.如果要操作图像,请使用 QImage.(如果您想知道如何停止正在运行的线程,请参阅例如 this answer).

                  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_())
                  

                  这篇关于线程实时记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Python 3.5 + PyQt5 到独立 exe 下一篇:带有 FlowLayout 小部件的 QScrollArea 未正确调整大小

                  相关文章

                  最新文章

                  <i id='61jFR'><tr id='61jFR'><dt id='61jFR'><q id='61jFR'><span id='61jFR'><b id='61jFR'><form id='61jFR'><ins id='61jFR'></ins><ul id='61jFR'></ul><sub id='61jFR'></sub></form><legend id='61jFR'></legend><bdo id='61jFR'><pre id='61jFR'><center id='61jFR'></center></pre></bdo></b><th id='61jFR'></th></span></q></dt></tr></i><div id='61jFR'><tfoot id='61jFR'></tfoot><dl id='61jFR'><fieldset id='61jFR'></fieldset></dl></div>
                  <tfoot id='61jFR'></tfoot>

                  1. <legend id='61jFR'><style id='61jFR'><dir id='61jFR'><q id='61jFR'></q></dir></style></legend>
                      • <bdo id='61jFR'></bdo><ul id='61jFR'></ul>

                    1. <small id='61jFR'></small><noframes id='61jFR'>