当我的QProgressDialog窗口自动关闭时,为什么会生成pythonw窗口?

时间:2013-08-25 13:23:36

标签: pyqt progressdialog

我是pyqt和python的新手,所以我确信我的问题看起来很愚蠢,所以谢谢你阅读并回答它。这真的很有帮助。

这是我的源代码

# -*- coding: utf-8 -*-   
from PyQt4.QtGui import *  
from PyQt4.QtCore import *  
import sys  

QTextCodec.setCodecForTr(QTextCodec.codecForName("utf8"))
class Prog(QDialog):
    def __init__(self,parent=None):
        super(Prog,self).__init__(parent)
        self.start_P()
    def start_P(self):
        progressDialog=QProgressDialog(self)  
        progressDialog.setWindowModality(Qt.WindowModal)  
        progressDialog.setMinimumDuration(5)  
        progressDialog.setWindowTitle(self.tr("请等待"))  
        progressDialog.setLabelText(self.tr("拷贝..."))
        progressDialog.setCancelButtonText(self.tr("取消"))
        progressDialog.setRange(0,100)  
        progressDialog.setAutoClose(True)

        for i in range(101):  
            progressDialog.setValue(i)  
            QThread.msleep(10)  
            if progressDialog.wasCanceled():  
                return
        self.connect(progressDialog,SIGNAL("closed()"))
def main():

    app = QApplication(sys.argv)
    pp = Prog()
    pp.show()
    app.exec_()

if __name__ == '__main__':
    main()

有一些汉字,但它无关紧要。奇怪的是,当我执行这个程序时,我会得到一个进度对话窗口,这就是我想要的。但是当它自动关闭时会自动生成一个pythonw窗口。

我很好奇为什么会生成这个pythonw窗口,并想知道如何避免它。

1 个答案:

答案 0 :(得分:0)

那是因为您正在创建QProgressDialog作为QDialog的孩子,请查看progressDialog=QProgressDialog(self)行。查看此示例的工作原理:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

#---------
# IMPORT
#---------
from PyQt4 import QtGui, QtCore

#---------
# MAIN
#---------
class MyThread(QtCore.QThread):
    progress = QtCore.pyqtSignal(int)
    _stopped = False

    def __init__(self, parent=None):
        super(MyThread, self).__init__(parent)

    def stop(self):
        self._stopped = True

    def start(self):
        self._stopped = False

        super(MyThread, self).start()

    def run(self):           
        for progressNumber in range(101):
            self.progress.emit(progressNumber)
            self.msleep(22)

            if self._stopped:
                return

class MyWindow(QtGui.QProgressDialog):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)

        self.threadProgress = MyThread(self)
        self.threadProgress.progress.connect(self.setValue)

    def stop(self):
        self.threadProgress.stop()

    def start(self):
        self.threadProgress.start()

    def hideEvent(self, event):
        self.close()

if __name__ == "__main__":
    import sys

    codec = QtCore.QTextCodec.codecForName("utf8")
    QtCore.QTextCodec.setCodecForTr(codec)

    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('MyWindow')

    main = MyWindow()
    main.setWindowTitle(main.tr("请等待"))  
    main.setLabelText(main.tr("拷贝..."))
    main.setCancelButtonText(main.tr("取消"))
    main.setRange(0,100)
    main.canceled.connect(main.stop)
    main.show()
    main.start()

    sys.exit(app.exec_())