2010-08-23 72 views
1

Pyqt中是否有類似的PyOnDemandOutputWindow函數? 此功能將控制檯輸出重定向到單獨的窗口。Wxwidgets和Pyqt

回答

4

可以用包裝器代替sys.std[out|err],該包裝器將所有輸出寫入例如一個QPlainTextEdit。一個很簡單的例子:

class StdoutWrapper(object): 
    def __init__(self, outwidget): 
     self.widget = outwidget 
     self.widget.setReadOnly(True) # assuming QPlainTextEdit 
     self.widget.hide() 

    def write(self, s): 
     self.widget.show() 
     self.widget.appendPlainText(s) # again assuming QPlainTextEdit 

而且別的地方:

import sys 

sys.stdout = StdoutWrapper(yourwidget) 
# similar for stderr, but you might want an error dialog or make 
# the text stand out using appendHtml