【问题标题】:pyqt: messagebox automatically closing after few secondspyqt:消息框在几秒钟后自动关闭
【发布时间】: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


【解决方案1】:

试试我的解决方案, 我根据您的要求创建了一种新型 QMessageBox

import sys
from PyQt4 import QtCore
from PyQt4 import QtGui


class TimerMessageBox(QtGui.QMessageBox):
    def __init__(self, timeout=3, parent=None):
        super(TimerMessageBox, self).__init__(parent)
        self.setWindowTitle("wait")
        self.time_to_wait = timeout
        self.setText("wait (closing automatically in {0} secondes.)".format(timeout))
        self.setStandardButtons(QtGui.QMessageBox.NoButton)
        self.timer = QtCore.QTimer(self)
        self.timer.setInterval(1000)
        self.timer.timeout.connect(self.changeContent)
        self.timer.start()

    def changeContent(self):
        self.setText("wait (closing automatically in {0} secondes.)".format(self.time_to_wait))
        self.time_to_wait -= 1
        if self.time_to_wait <= 0:
            self.close()

    def closeEvent(self, event):
        self.timer.stop()
        event.accept()


class Example(QtGui.QWidget):
    def __init__(self):
        super(Example, self).__init__()
        btn = QtGui.QPushButton('Button', self)
        btn.resize(btn.sizeHint())
        btn.move(50, 50)
        self.setWindowTitle('Example')
        btn.clicked.connect(self.warning)

    def warning(self):
        messagebox = TimerMessageBox(5, self)
        messagebox.exec_()


def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

【讨论】:

  • 您的计数器有一个错误。您应该将self.time_to_wait -= 1 移动到changeContent 的末尾。然后在启动计时器之前删除__init__ 中的setText 调用并调用self.changeContent()。 (PS:如果你使用new-style signal and slot syntax也会更好)。
  • @ekhumoro 这是 pyqt4,新式信号槽语法仅适用于 pyqt5
  • 不,它们也在 PyQt4 中。我提供的链接适用于 PyQt4 文档。
  • @ekhumoro 更新答案
  • 太棒了!但最重要的部分是一个错误。您的代码首先等待 2 秒,然后将文本更改为“4”,然后最终在“2”而不是“1”处停止。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-10
  • 2022-10-06
  • 2017-12-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多