import os, sys, time, calendar, math from PyQt4 import QtCore, QtGui class EmittingStream(QtCore.QObject): textWritten = QtCore.pyqtSignal(str) def write(self, text): self.textWritten.emit(str(text)) class myWrapper(QtGui.QMainWindow): def __init__(self): super(myWrapper, self).__init__() self.toolbar = self.addToolBar("MainMenu") self.toolbar.addAction(QtGui.QAction("myProg", self, triggered=self.myProgActions)) def myProgActions(self): self.setCentralWidget(myWidget()) class myWidget(QtGui.QWidget): def __init__(self): super(myWidget, self).__init__() self.myBtn = QtGui.QPushButton('Run!', self) self.myBtn.clicked.connect(self.startTest) self.outputViewer = QtGui.QTextEdit() self.grid = QtGui.QGridLayout() self.grid.addWidget(self.myBtn) self.grid.addWidget(self.outputViewer) self.setLayout(self.grid) def startTest(self): self.myLongTask = TaskThread() sys.stdout = EmittingStream(textWritten=self.normalOutputWritten) self.myLongTask.start() def normalOutputWritten(self, text): cursor = self.outputViewer.textCursor() cursor.movePosition(QtGui.QTextCursor.End) cursor.insertText(text) self.outputViewer.setTextCursor(cursor) self.outputViewer.ensureCursorVisible() QtGui.qApp.processEvents() class TaskThread(QtCore.QThread): def __init__(self): super(TaskThread, self).__init__() def run(self): myProgClass() class myProgClass: def __init__(self): for i in range(0,100): print "thread", i+1, " ", math.sqrt(i) #time.sleep(0.005) if __name__ == '__main__': app = QtGui.QApplication(sys.argv) myApp = myWrapper() myApp.show() sys.exit(app.exec_())
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question