我正在尝试创建一个警告消息框,它会在几秒钟后自动消失。
我已经编写了以下代码:
def warning(self):
messagebox = QtGui.QMessageBox(self)
messagebox.setWindowTitle("wait")
messagebox.setText("wait (closing automatically in {0} secondes.)".format(3))
messagebox.setStandardButtons(messagebox.NoButton)
self.timer2 = QtCore.QTimer()
self.time_to_wait = 3
def close_messagebox(e):
e.accept()
self.timer2.stop()
self.time_to_wait = 3
def decompte():
messagebox.setText("wait (closing automatically in {0} secondes.)".format(self.time_to_wait))
if self.time_to_wait <= 0:
messagebox.closeEvent = close_messagebox
messagebox.close()
self.time_to_wait -= 1
self.connect(self.timer2,QtCore.SIGNAL("timeout()"),decompte)
self.timer2.start(1000)
messagebox.exec_()
实际上,自动关闭部分是正常工作的。
我的问题是,当有人在几秒钟内手动关闭它时,通过单击窗口的x按钮,消息框永远不会关闭。 "等待时间" 变为负数,消息框显示例如 "在-4秒内自动关闭",并且它永远不会关闭。
有什么想法可以避免这种情况吗? 谢谢。