0
안녕하세요 저는 PyQt에서 초보자입니다. 누군가 아래의 프로그램이 text.py 위젯에 실시간으로 print.py의 stdout을 채우지 않는 이유를 제안 할 수 있습니까?이 문제는 명령 줄에서 실행 중일 때 존재합니다. pycharm에서 samecode를 실행중인 경우 실시간 출력을 얻습니다. 명령 줄 실행은 데이터를 텍스트 상자에 청크 (약 4K)로 표시합니다. 여기 PyQt 프로그램이 콘솔 출력을 실시간으로 라우팅하지 않습니다.
from PyQt4 import QtGui,QtCore
import sys
class gui(QtGui.QMainWindow):
def __init__(self):
super(gui, self).__init__()
self.initUI()
def dataReady(self):
cursor = self.output.textCursor()
cursor.movePosition(cursor.End)
cursor.insertText(str(self.process.readAll()))
self.output.ensureCursorVisible()
def initUI(self):
# Layout are better for placing widgets
layout = QtGui.QHBoxLayout()
self.output = QtGui.QTextEdit()
layout.addWidget(self.output)
centralWidget = QtGui.QWidget()
centralWidget.setLayout(layout)
self.setCentralWidget(centralWidget)
# QProcess object for external app
self.process = QtCore.QProcess(self)
# QProcess emits `readyRead` when there is data to be read
self.process.readyRead.connect(self.dataReady)
# Run program
self.process.start('python', ['./print.py'])
#Function Main Start
def main():
app = QtGui.QApplication(sys.argv)
ui=gui()
ui.show()
sys.exit(app.exec_())
#Function Main END
if __name__ == '__main__':
main()
은 하위 프로세스에서 실행 print.py
import time
n=10
while(n):
print "Hello world"
time.sleep(1)
n = n-1
print.py에서 print를 호출 할 때마다 'sys.stdout.flush()'로 명시 적으로 플러시 표준을 시도 했습니까? –
멋지다 ... 잘 작동한다 ..! – ZeeBlue