To close a QtGui Window
Question: I am a beginner in python . Recently i got stuck in a problem . Problem is
stated as follows : I needed a progressbar in my app . So i googled and found
a similar code . With this code even if the progress is 100% main window is
not closing (while the progress window closes). Please help me in resolving
this issue .

After searching i found the following code :

    
    
    from threading import *
    import sys
    import time
    from PyQt4 import QtGui
    from PyQt4 import QtCore
    class QCustomThread (QtCore.QThread):
        startLoad    = QtCore.pyqtSignal(int)
        progressLoad = QtCore.pyqtSignal(int)
        statusLoad   = QtCore.pyqtSignal(bool)
        def __init__ (self, parentQWidget = None):
            super(QCustomThread, self).__init__(parentQWidget)
            self.wasCanceled = False
        def run (self):
            # Simulate data load estimation
            numberOfprogress = 100
            self.startLoad.emit(numberOfprogress)
            for progress in range(numberOfprogress + 1):
                # Delay
                time.sleep(0.1)
                if not self.wasCanceled:
                    self.progressLoad.emit(progress)
                else:
                    break
            self.statusLoad.emit(True if progress == numberOfprogress else False)
            self.exit(0)
        def cancel (self):
            self.wasCanceled = True
    class QCustomMainWindow (QtGui.QMainWindow):
        def __init__ (self):
            super(QCustomMainWindow, self).__init__()        
            self.loadingQProgressDialog = QtGui.QProgressDialog(self)
            self.loadingQProgressDialog.setLabelText('Processing')
            self.loadingQProgressDialog.setCancelButtonText('Cancel')
            self.loadingQProgressDialog.setWindowModality(QtCore.Qt.WindowModal)
            self.startWork()
        def startWork (self):
            myQCustomThread = QCustomThread(self)
            def startLoadCallBack (numberOfprogress):
                self.loadingQProgressDialog.setMinimum(0)
                self.loadingQProgressDialog.setMaximum(numberOfprogress)
                self.loadingQProgressDialog.show()
            def progressLoadCallBack (progress):
                self.loadingQProgressDialog.setValue(progress)
            def statusLoadCallBack (flag):
                print 'SUCCESSFUL' if flag else 'FAILED'
            myQCustomThread.startLoad.connect(startLoadCallBack)
            myQCustomThread.progressLoad.connect(progressLoadCallBack)
            myQCustomThread.statusLoad.connect(statusLoadCallBack)
            self.loadingQProgressDialog.canceled.connect(myQCustomThread.cancel)
            myQCustomThread.start()
            self.loadingQProgressDialog.hide()
    def app():
        myQApplication = QtGui.QApplication(sys.argv)
        myQCustomMainWindow = QCustomMainWindow()
        myQCustomMainWindow.show()
        print 'main complete'
    ##    myQCustomMainWindow.loadingQProgressDialog.hide()
        (myQApplication.exec_())
    def deep():
        print 'hello'
        app()
        print 'hi'
    if __name__=="__main__":
        deep()
        deep()
    


Answer: Your found my
[answer](http://stackoverflow.com/questions/25583418/implementing-
qprogressbar-against-a-class/25584954#25584954) of progress bar ;). But my
answer to show how to implement `QThread` open file, not close window until
100% progress. But simply to close is, when progress is successful, close it
by use
[`self.close()`](http://pyqt.sourceforge.net/Docs/PyQt4/qwidget.html#close) in
`def statusLoadCallBack (flag)`;

    
    
    import sys
    import time
    from PyQt4 import QtGui
    from PyQt4 import QtCore
    
    class QCustomThread (QtCore.QThread):
        startLoad    = QtCore.pyqtSignal(int)
        progressLoad = QtCore.pyqtSignal(int)
        statusLoad   = QtCore.pyqtSignal(bool)
    
        def __init__ (self, parentQWidget = None):
            super(QCustomThread, self).__init__(parentQWidget)
            self.wasCanceled = False
    
        def run (self):
            # Simulate data load estimation
            numberOfprogress = 100
            self.startLoad.emit(numberOfprogress)
            for progress in range(numberOfprogress + 1):
                # Delay
                time.sleep(0.001)
                if not self.wasCanceled:
                    self.progressLoad.emit(progress)
                else:
                    break
            self.statusLoad.emit(True if progress == numberOfprogress else False)
            self.exit(0)
    
        def cancel (self):
            self.wasCanceled = True
    
    class QCustomMainWindow (QtGui.QMainWindow):
        def __init__ (self):
            super(QCustomMainWindow, self).__init__()        
            self.loadingQProgressDialog = QtGui.QProgressDialog(self)
            self.loadingQProgressDialog.setLabelText('Processing')
            self.loadingQProgressDialog.setCancelButtonText('Cancel')
            self.loadingQProgressDialog.setWindowModality(QtCore.Qt.WindowModal)
            self.startWork()
    
        def startWork (self):
            myQCustomThread = QCustomThread(self)
            def startLoadCallBack (numberOfprogress):
                self.loadingQProgressDialog.setMinimum(0)
                self.loadingQProgressDialog.setMaximum(numberOfprogress)
                self.loadingQProgressDialog.show()
            def progressLoadCallBack (progress):
                self.loadingQProgressDialog.setValue(progress)
            def statusLoadCallBack (flag):
                print 'SUCCESSFUL' if flag else 'FAILED'
                if flag:
                    self.close()
            myQCustomThread.startLoad.connect(startLoadCallBack)
            myQCustomThread.progressLoad.connect(progressLoadCallBack)
            myQCustomThread.statusLoad.connect(statusLoadCallBack)
            self.loadingQProgressDialog.canceled.connect(myQCustomThread.cancel)
            myQCustomThread.start()
    
    def app():
        myQApplication = QtGui.QApplication(sys.argv)
        myQCustomMainWindow = QCustomMainWindow()
        myQCustomMainWindow.show()
        print 'main complete'
        (myQApplication.exec_())
    
    def deep():
        print 'hello'
        app()
        print 'hi'
    
    if __name__=="__main__":
        deep()
        deep()
    

