【发布时间】:2017-04-17 09:21:07
【问题描述】:
我正在尝试做一个警告消息框,它会在几秒钟后自动消失。 我已经完成了这段代码:
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秒内自动关闭”,并且永远不会关闭。
知道如何避免这种情况吗? 问候
【问题讨论】:
-
试试我的解决方案
标签: python pyqt pyqt4 pyqt5 qmessagebox