Created
August 1, 2011 17:32
-
-
Save mauriciodev/1118585 to your computer and use it in GitHub Desktop.
Minimal Python Thread example with a progress bar and something that keeps emiting signals from time to time. Requires a simples UI with a progress bar.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from progress_ui import Ui_Dialog | |
from PyQt4 import QtCore, QtGui | |
import sys, time | |
class mythread(QtCore.QThread): | |
def __init__(self,parent,n): | |
QtCore.QThread.__init__(self,parent) | |
self.n=n | |
def run(self): | |
self.emit(QtCore.SIGNAL("total(PyQt_PyObject)"),self.n) | |
i=0 | |
while (i<self.n): | |
if (time.time() % 1==0): | |
i+=1 | |
#print str(i) | |
self.emit(QtCore.SIGNAL("update()")) | |
# create the dialog for zoom to point | |
class progress(QtGui.QDialog): | |
def __init__(self): | |
QtGui.QDialog.__init__(self) | |
# Set up the user interface from Designer. | |
self.ui = Ui_Dialog() | |
self.ui.setupUi(self) | |
self.ui.progressBar.setValue(0) | |
self.t=mythread(self,100) | |
QtCore.QObject.connect(self.t, QtCore.SIGNAL("total(PyQt_PyObject)"), self.total) | |
QtCore.QObject.connect(self.t, QtCore.SIGNAL("update()"), self.update) | |
self.n=0 | |
self.t.start() | |
def update(self): | |
self.n+=1 | |
print self.n | |
self.ui.progressBar.setValue(self.n) | |
def total(self,total): | |
self.ui.progressBar.setMaximum(total) | |
if __name__=="__main__": | |
app = QtGui.QApplication([]) | |
c=progress() | |
c.show() | |
sys.exit(app.exec_()) |
@shawnkirsch I'm assuming process_ui the file containing the UI Class. Something exported from designer that contains the bulk of his UI code.
Example
class Ui_Dialog(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(923, 619)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
self.statusbar.showMessage('Welcome!')
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Test"))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what is progress_ui?