关闭QtGui窗口

时间:2014-09-14 11:04:30

标签: python multithreading pyqt

我是python的初学者。最近我遇到了问题。问题陈述如下:我的应用程序中需要一个进度条。所以我用Google搜索并找到了类似的代码。使用此代码即使进度为100%,主窗口也不会关闭(进度窗口关闭时)。 请帮我解决这个问题。

搜索后,我发现了以下代码:

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()

1 个答案:

答案 0 :(得分:1)

您找到我的answer进度条;)。但是我的答案显示了如何实现QThread打开文件,而不是关闭窗口,直到100%进展。但只是关闭是,当进度成功时,请使用def statusLoadCallBack (flag)中的self.close()关闭它;

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()