关闭后执行的 PyQt5 QDialog 代码
Posted
技术标签:
【中文标题】关闭后执行的 PyQt5 QDialog 代码【英文标题】:PyQt5 QDialog code executing after close 【发布时间】:2018-08-16 16:18:18 【问题描述】:使用以下代码:
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!')
我希望在 show_load_errors()
方法中的 self.close()
之后不再执行任何代码。在load_file()
方法中,我检查了两个条件。如果self.show_load_errors()
被第一个条件调用,那么我需要关闭对话框而不进一步执行。相反,代码会在对话框关闭之前检查下一个条件。
所以基本上,如果我在对话框上调用self.close()
,我需要在不进一步执行任何代码的情况下关闭对话框。希望这是有道理的。谢谢!
【问题讨论】:
【参考方案1】:在我看来,您需要重新定义QMessageBox
。
在继承自QMessageBox
的class
中检查条件并执行必要的操作。
例如,如下例所示:
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_())
【讨论】:
以上是关于关闭后执行的 PyQt5 QDialog 代码的主要内容,如果未能解决你的问题,请参考以下文章