Best way to display logs in pyqt?

前端 未结 6 987
迷失自我
迷失自我 2020-12-01 01:42

I am currently working on a GUI using qt designer. I am wondering how I should go about printing strings on the GUI that acts like a logger window. I am using pyqt5.

6条回答
  •  甜味超标
    2020-12-01 02:24

    Alex's answer should be ok in a single thread scenario, but if you are logging in another thread (QThread) you may get the following warning:

    QObject::connect: Cannot queue arguments of type 'QTextCursor'
    (Make sure 'QTextCursor' is registered using qRegisterMetaType().)
    

    This is because you are modifying the GUI (self.widget.appendPlainText(msg)) from a thread other than the main thread without using the Qt Signal/Slot mechanism.

    Here is my solution:

    # my_logger.py
    
    import logging
    from PyQt5.QtCore import pyqtSignal, QObject
    
    class Handler(QObject, logging.Handler):
        new_record = pyqtSignal(object)
    
        def __init__(self, parent):
            super().__init__(parent)
            super(logging.Handler).__init__()
            formatter = Formatter('%(asctime)s|%(levelname)s|%(message)s|', '%d/%m/%Y %H:%M:%S')
            self.setFormatter(formatter)
    
        def emit(self, record):
            msg = self.format(record)
            self.new_record.emit(msg) # <---- emit signal here
    
    class Formatter(logging.Formatter):
        def formatException(self, ei):
            result = super(Formatter, self).formatException(ei)
            return result
    
        def format(self, record):
            s = super(Formatter, self).format(record)
            if record.exc_text:
                s = s.replace('\n', '')
            return s
    
    
       # gui.py
    
       ... # GUI code
       ...
       def setup_logger(self)
            handler = Handler(self)
            log_text_box = QPlainTextEdit(self)
            self.main_layout.addWidget(log_text_box)
            logging.getLogger().addHandler(handler)
            logging.getLogger().setLevel(logging.INFO)
            handler.new_record.connect(log_text_box.appendPlainText) # <---- connect QPlainTextEdit.appendPlainText slot
       ...
    

提交回复
热议问题