[英]PyQt: displaying Python interpreter output
I've implemented this answer into my code, which I was hoping would do what I want. 我已经在我的代码中实现了这个答案 ,我希望它能做我想要的。 However, I'm running a method via a connection on a
QPushButton
, and I want to pipe what happens in this method to the GUI. 但是,我正在通过
QPushButton
上的连接运行一种方法,并且我想将此方法中发生的事情传递给GUI。
The first time I click the button the stdout
appears in the interpreter window; 我第一次单击该按钮时,
stdout
出现在解释器窗口中。 however, on subsequent presses of the button, the stdout
appears in the QTextEdit
- I assume there's some intricacy of the print statement, or of QPushButton
, that I don't understand - if anyone can give any pointers where I need to start changing my code I'll be eternally grateful! 但是,在随后按下该按钮时,
stdout
出现在QTextEdit
-我认为print语句或QPushButton
存在一些我不理解的复杂性-如果有人可以在我需要开始更改我的位置的地方提供任何指针代码我将永远感激不已!
I think this is the smallest amount of code I can use to demonstrate the problem.. 我认为这是我可以用来证明问题的最少代码。
import os, sys
from PyQt4 import QtCore, QtGui
def main():
app = QtGui.QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())
class MyWindow(QtGui.QWidget):
def __init__(self, *args):
QtGui.QWidget.__init__(self, *args)
self.runBtn = QtGui.QPushButton('Run!', self)
self.runBtn.clicked.connect(self.runCmd)
self.te = QtGui.QTextEdit()
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.runBtn)
layout.addWidget(self.te)
self.setLayout(layout)
def runCmd(self):
print "here"
print sys.stdout
sys.stdout = EmittingStream(textWritten=self.normalOutputWritten)
def __del__(self):
sys.stdout = sys.__stdout__
def normalOutputWritten(self, text):
cursor = self.te.textCursor()
cursor.movePosition(QtGui.QTextCursor.End)
cursor.insertText(text)
self.te.setTextCursor(cursor)
self.te.ensureCursorVisible()
class EmittingStream(QtCore.QObject):
textWritten = QtCore.pyqtSignal(str)
def write(self, text):
self.textWritten.emit(str(text))
if __name__ == "__main__":
main()
You're mixing signals with method calls: 您正在将信号与方法调用混合:
sys.stdout = EmittingStream(textWritten=self.normalOutputWritten)
I'm not sure what this is supposed to do. 我不确定这应该做什么。 You should do this instead:
您应该这样做:
self.stream = EmittingStream()
self.stream.textWritten.connect(self.normalOutputWritten)
but only once when you start the program. 但启动程序时只有一次。 When you want to see the output, do this:
当您想查看输出时,请执行以下操作:
try:
sys.stdout = self.stream
... code to print something ...
finally:
sys.stdout = sys.__stdout__ # reset stdout to default
Aaron made a very good point, but my problem had a much simpler answer than the intricacies of object orientation in python... Aaron提出了一个很好的观点,但是我的问题的答案比python中面向对象的复杂性要简单得多。
sys.stdout = EmittingStream(textWritten=self.normalOutputWritten)
needs to be after any print statements - print statements before this will be directed to the standard stdout
, ie the interpreter console. 需要在 任何打印语句之后-在此之前的打印语句将定向到标准
stdout
,即解释器控制台。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.