2013-03-26 160 views
1

參照帖子here。能有人給我如何print語句的輸出追加到QEditext在PyQt的詳細解釋......我試過上面給出的代碼,但它是不完整的,我得到了:PYQT:如何捕獲python解釋器的輸出並將其顯示在QEditText中?

TypeError: connect() slot argument should be a callable or a signal, not 'QTextEdit' 

在第一個文件我寫道:

from PyQt4 import QtCore 

class EmittingStream(QtCore.QObject): 
    textWritten = QtCore.pyqtSignal(str) 

    def write(self, text): 
     self.textWritten.emit(str(text)) 

在一個單獨的文件我導入的第一個文件,它是這樣的:

from PyQt4 import QtGui, QtCore 

import os, sys 

class Window(QtGui.QWidget): 
    def __init__(self): 
     QtGui.QWidget.__init__(self) 

     self.et=QtGui.QTextEdit() 

     layout = QtGui.QVBoxLayout(self)    
     layout.addWidget(self.et) 

     sys.stdout = EmittingStream(textWritten=self.et)   

    def __del__(self): 
     # Restore sys.stdout 
     sys.stdout = sys.__stdout__ 

    def normalOutputWritten(self, text): 
     """Append text to the QTextEdit.""" 
     # Maybe QTextEdit.append() works as well, but this is how I do it: 
     cursor = self.et.textCursor() 
     cursor.movePosition(QtGui.QTextCursor.End) 
     cursor.insertText(text) 
     self.et.setTextCursor(cursor) 
     self.et.ensureCursorVisible() 

if __name__ == '__main__': 
    import sys 
    app = QtGui.QApplication(sys.argv) 
    window = Window() 
    window.show() 
    sys.exit(app.exec_()) 

我知道我的代碼是不完整 ...我應該添加什麼信號?

回答

3

看看這對你的作品:

#!/usr/bin/env python 
#-*- coding:utf-8 -*- 

from PyQt4 import QtGui, QtCore 

class MyStream(QtCore.QObject): 
    message = QtCore.pyqtSignal(str) 
    def __init__(self, parent=None): 
     super(MyStream, self).__init__(parent) 

    def write(self, message): 
     self.message.emit(str(message)) 

class MyWindow(QtGui.QWidget): 
    def __init__(self, parent=None): 
     super(MyWindow, self).__init__(parent) 

     self.pushButtonPrint = QtGui.QPushButton(self) 
     self.pushButtonPrint.setText("Click Me!") 
     self.pushButtonPrint.clicked.connect(self.on_pushButtonPrint_clicked) 

     self.textEdit = QtGui.QTextEdit(self) 

     self.layoutVertical = QtGui.QVBoxLayout(self) 
     self.layoutVertical.addWidget(self.pushButtonPrint) 
     self.layoutVertical.addWidget(self.textEdit) 

    @QtCore.pyqtSlot() 
    def on_pushButtonPrint_clicked(self): 
     print "Button Clicked!" 

    @QtCore.pyqtSlot(str) 
    def on_myStream_message(self, message): 
     self.textEdit.moveCursor(QtGui.QTextCursor.End) 
     self.textEdit.insertPlainText(message) 

if __name__ == "__main__": 
    import sys 

    app = QtGui.QApplication(sys.argv) 
    app.setApplicationName('MyWindow') 

    main = MyWindow() 
    main.show() 

    myStream = MyStream() 
    myStream.message.connect(main.on_myStream_message) 

    sys.stdout = myStream   
    sys.exit(app.exec_()) 
+0

雅代碼工作正常,無論打印「XXX」被印在PyQt的窗口。作爲下一步我想打電話給我的Python文件在這個inon_pushButtonPrint_clicked函數並希望我的文件的輸出在PYQT窗口中打印(即,所有打印語句) – user2081918 2013-03-29 05:35:07

+0

這不是Stackoverflow打算工作的方式。請閱讀[about](http://stackoverflow.com/about)頁面,然後回來,一旦完成,我將很樂意爲您提供幫助。 – 2013-03-29 09:26:53

+1

對不起..但你要感謝你的代碼。它真的幫了我很多:) – user2081918 2013-03-29 10:44:57

相關問題