2015-02-22 22 views
5

Tôi hiện đang làm việc trên một gui sử dụng thiết kế qt. Tôi tự hỏi làm thế nào tôi nên đi về dây in trên gui mà hành vi giống như một cửa sổ nhật ký. Tôi đang sử dụng pyqt5Cách tốt nhất để hiển thị nhật ký trong pyqt?

+0

người dùng Bing nên đi [ở đây] (http://stackoverflow.com/questions/24469662/how-to-redirect-logger- output-into-pyqt-text-widget): P – Carel

Trả lời

1

Có vẻ như bạn sẽ muốn sử dụng tiện ích QPlainTextEdit được đặt thành chỉ đọc.

Cân nhắc thay đổi màu nền thành màu xám để cung cấp cho người dùng một gợi ý rằng nó không thể chỉnh sửa được. Nó cũng tùy thuộc vào bạn nếu bạn muốn nó có thể cuộn được hoặc văn bản có thể chọn được.

This answer có thể giúp bạn bắt đầu phân lớp QPlainTextEdit để cuộn với đầu ra, lưu vào tệp, bất kỳ điều gì.

7

Nếu bạn đang sử dụng mô-đun Python logging để có thể dễ dàng tạo trình xử lý ghi nhật ký tùy chỉnh chuyển các thông điệp tường trình qua một ví dụ QPlainTextEdit (như mô tả của Christopher).

Để thực hiện việc này, trước tiên bạn hãy chia lớp con logging.Handler. Trong số __init__, chúng tôi tạo QPlainTextEdit sẽ chứa nhật ký. Các bit quan trọng ở đây là xử lý sẽ nhận được tin nhắn thông qua các chức năng emit(). Vì vậy, chúng tôi quá tải chức năng này và chuyển văn bản tin nhắn vào QPlainTextEdit.

import logging 

class QPlainTextEditLogger(logging.Handler): 
    def __init__(self, parent): 
     super(Logger, self).__init__() 

     self.widget = QPlainTextEdit(parent) 
     self.widget.setReadOnly(True) 

    def emit(self, record): 
     msg = self.format(record) 
     self.widget.textCursor().appendPlainText(msg) 

    def write(self, m): 
     pass 

Tạo đối tượng từ lớp này, chuyển cho cha mẹ QPlainTextEdit (ví dụ: cửa sổ chính hoặc bố cục). Sau đó bạn có thể thêm trình xử lý này cho trình ghi nhật ký hiện tại.

# Set up logging to use your widget as a handler 
log_handler = QPlainTextEditLogger(<parent widget>) 
logging.getLogger().addHandler(log_handler) 
7

Dưới đây là một ví dụ làm việc hoàn toàn dựa trên mfitzp's answer:

import sys 
from PyQt4 import QtCore, QtGui 
import logging 

# Uncomment below for terminal log messages 
# logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(name)s - %(levelname)s - %(message)s')  

class QPlainTextEditLogger(logging.Handler): 
    def __init__(self, parent): 
     super().__init__() 
     self.widget = QtGui.QPlainTextEdit(parent) 
     self.widget.setReadOnly(True)  

    def emit(self, record): 
     msg = self.format(record) 
     self.widget.appendPlainText(msg)  


class MyDialog(QtGui.QDialog, QPlainTextEditLogger): 
    def __init__(self, parent=None): 
     super().__init__(parent)  

     logTextBox = QPlainTextEditLogger(self) 
     # You can format what is printed to text box 
     logTextBox.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')) 
     logging.getLogger().addHandler(logTextBox) 
     # You can control the logging level 
     logging.getLogger().setLevel(logging.DEBUG) 

     self._button = QtGui.QPushButton(self) 
     self._button.setText('Test Me')  

     layout = QtGui.QVBoxLayout() 
     # Add the new logging box widget to the layout 
     layout.addWidget(logTextBox.widget) 
     layout.addWidget(self._button) 
     self.setLayout(layout)  

     # Connect signal to slot 
     self._button.clicked.connect(self.test)  

    def test(self): 
     logging.debug('damn, a bug') 
     logging.info('something to remember') 
     logging.warning('that\'s not right') 
     logging.error('foobar') 

if (__name__ == '__main__'): 
    app = None 
    if (not QtGui.QApplication.instance()): 
     app = QtGui.QApplication([]) 
    dlg = MyDialog() 
    dlg.show() 
    dlg.raise_() 
    if (app): 
     app.exec_() 
+0

Đây có thể là một câu hỏi ngu ngốc, nhưng mục đích kế thừa từ 'QPlainTextEditLogger' trong' MyDialog' là gì? Tôi đang cố gắng để chuyển đổi ví dụ này để PyQt5, và không thể làm cho nó hoạt động mà không cần loại bỏ thừa kế thứ hai đó. Dường như nó hoạt động tốt mà không có nó. –

Các vấn đề liên quan