2012-07-13 28 views
1

重定向輸出I具有的QApplication,有很多的類和函數,它在控制檯上顯示很多標準輸出。我想重定向這個輸出和錯誤的QTextBrowser(這也是的QApplication的一部分)。有沒有任何調整來做到這一點。在PyQt的

+0

請檢查此鏈接以獲取有關將輸出重定向到python的信息: [如何捕獲Python解釋器的輸出並在Text小部件中顯示?](http://stackoverflow.com/questions/8356336/how-to-capture -python -output-of-pythons-interpreter-and-show-in-a-text-widget) – kasa 2012-08-03 07:32:42

回答

3

我已經制作了一個開源PyQt定製窗口小部件庫,其中一個是記錄器類(XLoggerWidget),另一個是完整的Python解釋器(XConsoleEdit)。它符合你的要求。

你可以在這裏得到它,如果你想:http://dev.projexsoftware.com/projects/projexui

你要找的是在XConsoleEdit(projexui.widgets.xconsoleedit)的一部分,但它的一般要點是:

import logging 
import sys 

from PyQt4.QtCore import QObject,\ 
         pyqtSignal 

from PyQt4.QtGui import QDialog, \ 
         QVBoxLayout, \ 
         QPushButton, \ 
         QTextBrowser,\ 
         QApplication 

logger = logging.getLogger(__name__) 

class XStream(QObject): 
    _stdout = None 
    _stderr = None 

    messageWritten = pyqtSignal(str) 

    def flush(self): 
     pass 

    def fileno(self): 
     return -1 

    def write(self, msg): 
     if (not self.signalsBlocked()): 
      self.messageWritten.emit(unicode(msg)) 

    @staticmethod 
    def stdout(): 
     if (not XStream._stdout): 
      XStream._stdout = XStream() 
      sys.stdout = XStream._stdout 
     return XStream._stdout 

    @staticmethod 
    def stderr(): 
     if (not XStream._stderr): 
      XStream._stderr = XStream() 
      sys.stderr = XStream._stderr 
     return XStream._stderr 

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

     # setup the ui 
     self._console = QTextBrowser(self) 
     self._button = QPushButton(self) 
     self._button.setText('Test Me') 

     # create the layout 
     layout = QVBoxLayout() 
     layout.addWidget(self._console) 
     layout.addWidget(self._button) 
     self.setLayout(layout) 

     # create connections 
     XStream.stdout().messageWritten.connect(self._console.insertPlainText) 
     XStream.stderr().messageWritten.connect(self._console.insertPlainText) 

     self._button.clicked.connect(self.test) 

    def test(self): 
     # print some stuff 
     print 'testing' 
     print 'testing2' 

     # log some stuff 
     logger.debug('Testing debug') 
     logger.info('Testing info') 
     logger.warning('Testing warning') 
     logger.error('Testing error') 

     # error out something 
     print blah 

if (__name__ == '__main__'): 
    logging.basicConfig() 

    app = None 
    if (not QApplication.instance()): 
     app = QApplication([]) 

    dlg = MyDialog() 
    dlg.show() 

    if (app): 
     app.exec_() 

這是什麼在XConsoleEdit的簡化版本,但其總體思路,仍然應該對你要什麼,如果你不想下載代碼工作。

在這個例子中壽,你會發現,只有打印和錯誤日誌被反射到編輯路由。如果您想將Python日誌記錄系統連接到編輯中,那麼在定義logging.Handler並將其鏈接到您的小部件的地方需要更復雜一些。

這代碼將在projexui.widgets.xloggerwidget

發現它有點更長,更復雜,所以我不打算在這裏加載它......但如果你對此有任何疑問,讓我知道。

+1

#請檢查此鏈接以獲取有關重新生成PYTHON中的輸出的信息http://stackoverflow.com/questions/8356336/how-to-捕獲輸出的 - 蟒蛇口譯和秀-IN-A-文本控件 – kasa 2012-08-03 07:31:49