PyQt5 QDialog代码在关闭后执行

时间:2018-08-16 16:18:18

标签: python-3.x pyqt5

使用以下代码:

from PyQt5.QtWidgets import QDialog,QMessageBox
from PyQt5.uic import loadUi


class FileLoader(QDialog):
    loadCompleteSig = pyqtSignal()

    def __init__(self,*args):
        super(FileLoader,self).__init__(*args)
        loadUi("fileloader/fileloader.ui",self)
        self.clientcode = ""
        self.filename = ""
        self.foldername = ""
        self.filetype = ""
        self.clearflag = True
        self.thread = None

    @pyqtSlot()
    def showEvent(self, e):
        QTimer.singleShot(20,self.load_file)

    def show_load_errors(self,msg):        
        message = QMessageBox()
        message.setIcon(QMessageBox.Critical)
        message.setWindowTitle("File Loader Error")
        message.setText("The File Loader Has Encountered An Error:")
        message.setInformativeText(msg)
        message.exec_()
        self.close()

    def set_folder_name(self,foldername):
        self.foldername = foldername + '/' if foldername[-1:] != "/" else foldername

    def set_file_type(self,filetype):
        self.filetype = filetype.upper()

    def set_client_code(self,clientcode):
        self.clientcode = clientcode.upper()

    def set_clear_flag(self,clearflag):
        self.clearflag = clearflag

    def load_file(self):
        if not isdir(self.foldername):
            self.show_load_errors('The Selected Folder {0} Cannot Be Found!'.format(self.foldername))
        if self.filetype == "":
            self.show_load_errors('The File Load Type Must Be Set!')

我希望在self.close()方法中的show_load_errors()之后不再执行任何其他代码。在load_file()方法中,我检查了两个条件。如果第一个条件调用了self.show_load_errors(),那么我需要关闭对话框而不进行进一步执行。相反,代码会在对话框关闭之前检查下一个条件。

因此,基本上,如果我在对话框上调用self.close(),则需要关闭该对话框而不进一步执行任何代码。希望这是有道理的。谢谢!

1 个答案:

答案 0 :(得分:0)

我认为您需要重新定义QMessageBox。 在class继承的QMessageBox中,检查条件并执行必要的操作。

例如,如下例所示:

import sys
from PyQt5.QtWidgets import QApplication, QPushButton, QMessageBox
from PyQt5.QtCore    import QTimer
from random          import randrange

class MessageBox(QMessageBox):
    def __init__(self, *args, count=5, time=1000, auto=False, **kwargs):
        super(MessageBox, self).__init__(*args, **kwargs)

        self._count = count
        self._time  = time
        self._auto  = auto                       # Automatic closing
        assert count > 0                         # Should be more 0
        assert time >= 500                       # Should be >= 500 milliseconds
        self.setStandardButtons(self.Close)      # Close Button
        self.closeBtn = self.button(self.Close)  # The button `Close`
        self.closeBtn.setText('Close (%s)' % count)
        self.closeBtn.setEnabled(False)
        self._timer = QTimer(self, timeout=self.doCountDown)
        self._timer.start(self._time)
        print('Automatic closing->', auto)

    def doCountDown(self):
        self.closeBtn.setText('Close (%s)' % self._count)
        self._count -= 1
        if self._count <= 0:
            self.closeBtn.setText('Close')
            self.closeBtn.setEnabled(True)
            self._timer.stop()
            if self._auto:                        # Automatic closing
                self.accept()
                self.close()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = QPushButton('Click, the pop-up dialog box appears')
    w.resize(300, 200)
    w.show()
    w.clicked.connect(lambda: MessageBox(
        w, text='Close dialog the countdown', auto=randrange(0, 2)).exec_())
    sys.exit(app.exec_())

enter image description here