我有一个大型机,然后点击按钮打开一个新对话框,
对话框有一个计时器,让计时器以3秒的间隔显示消息,
但如果单击右上方的关闭按钮关闭对话框,则消息框仍会运行,
如何避免它?
谢谢!
from PySide2.QtWidgets import QApplication,QMainWindow,QTabWidget,QWidget
from PySide2.QtWidgets import QMessageBox,QFileDialog,QErrorMessage
from PySide2 import QtCore, QtGui, QtWidgets
class UI_Test20(object):
def setupUi(self, Test202):
Test202.setObjectName("Test202")
Test202.resize(100,100)
self.centralwidget = QtWidgets.QWidget(Test202)
self.centralwidget.setObjectName("centralwidget")
self.pb = QtWidgets.QPushButton(self.centralwidget)
self.pb.setText('push button!')
Test202.setCentralWidget(self.centralwidget)
self.pb.clicked.connect(self.btnClicked)
self.retranslateUi(Test202)
QtCore.QMetaObject.connectSlotsByName(Test202)
def retranslateUi(self, Test202):
Test202.setWindowTitle(QtWidgets.QApplication.translate("Test202", "MainWindow", None, -1))
def btnClicked(self):
self.ui = Ui_Dialog1(self)
self.ui.show()
class Test20(QMainWindow, UI_Test20) :
def __init__(self, parent):
super(Test20, self).__init__(parent)
self.setupUi(self)
和对话框代码
class Ui_Dialog1(QtWidgets.QDialog):
def __init__(self, parent=None):
super(Ui_Dialog1, self).__init__(parent)
self.p = parent
self.setupUi(self)
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.update)
self.timer.start(3000)
def update(self):
QMessageBox.information(self,'123','123')
def setupUi(self, Dialog1):
Dialog1.setObjectName("Dialog1")
Dialog1.resize(333, 173)
主
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
ui = Test20(None)
ui.show()
sys.exit(app.exec_())
答案 0 :(得分:0)
当您使用QDialog调用show()方法时,您的对话框将是无模式的,并且在它关闭时将被隐藏而不会被删除。要确保删除对话框,请在对话框初始化方法中再添加一行:
def __init__(self, parent=None):
super(Ui_Dialog1, self).__init__(parent)
self.p = parent
self.setupUi(self)
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.update)
self.timer.start(3000)
#Make sure that your dialog will be deleted not hidden
self.setAttribute(QtGui.Qt.WA_DeleteOnClose)