带有倒数计时器的 PyQt4 QMessageBox [英] PyQt4 QMessageBox with a countdown timer
问题描述
我编写了一个用于实验室测量的 Python 应用程序,它通过不稳定的网络连接将数据保存到远程数据库中.当连接中断时,会弹出一个问题(我使用QMessageBox.question
),询问用户是重复上次交易还是取消交易.
I write a python application for measurements in a laboratory, which saves data into a remote database over an unstable network connection. When connection is broken, a question pops up (I use QMessageBox.question
), asking user to whether repeat the last transaction or cancel it.
最近修改了应用程序以在夜间自主进行自动测量.不再有操作员点击默认选项重试"!它应该在一段时间超时后自动选择,仍然给用户机会做出其他选择.
Recently application was modified to do automatic measurement autonomously during the night. There is no operator anymore to click on the default option "Retry"! It should be selected automatically after some timeout, still giving the user opportunity to make other choice.
这意味着,如果用户没有做出任何其他选择,我需要一个 QMessageBox 版本,在超时过后点击默认按钮.
This means, I need a version of QMessageBox that clicks a default button after a timeout is elapsed, if user did not make any other choice.
有类似问题,但它是C++
特定的.
推荐答案
这个答案是 Jeremy Friesner 在一个类似的问题中:
This answer is a Python
adaptation of C++
code posted by Jeremy Friesner in a similar question:
from PyQt4.QtGui import QMessageBox as MBox, QApplication
from PyQt4.QtCore import QTimer
class TimedMBox(MBox):
"""
Variant of QMessageBox that automatically clicks the default button
after the timeout is elapsed
"""
def __init__(self, timeout=5, buttons=None, **kwargs):
if not buttons:
buttons = [MBox.Retry, MBox.Abort, MBox.Cancel]
self.timer = QTimer()
self.timeout = timeout
self.timer.timeout.connect(self.tick)
self.timer.setInterval(1000)
super(TimedMBox, self).__init__(parent=None)
if "text" in kwargs:
self.setText(kwargs["text"])
if "title" in kwargs:
self.setWindowTitle(kwargs["title"])
self.t_btn = self.addButton(buttons.pop(0))
self.t_btn_text = self.t_btn.text()
self.setDefaultButton(self.t_btn)
for button in buttons:
self.addButton(button)
def showEvent(self, e):
super(TimedMBox, self).showEvent(e)
self.tick()
self.timer.start()
def tick(self):
self.timeout -= 1
if self.timeout >= 0:
self.t_btn.setText(self.t_btn_text + " (%i)" % self.timeout)
else:
self.timer.stop()
self.defaultButton().animateClick()
@staticmethod
def question(**kwargs):
"""
Ask user a question, which has a default answer. The default answer is
automatically selected after a timeout.
Parameters
----------
title : string
Title of the question window
text : string
Textual message to the user
timeout : float
Number of seconds before the default button is pressed
buttons : {MBox.DefaultButton, array}
Array of buttons for the dialog, default button first
Returns
-------
button : MBox.DefaultButton
The button that has been pressed
"""
w = TimedMBox(**kwargs)
w.setIcon(MBox.Question)
return w.exec_()
if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
w = TimedMBox.question(text="Please select something",
title="Timed Message Box",
timeout=8)
if w == MBox.Retry:
print "Retry"
if w == MBox.Cancel:
print "Cancel"
if w == MBox.Abort:
print "Abort"
在这个实现中,默认按钮是参数 buttons
中的第一个按钮.
In this implementation the default button is the first one in the argument buttons
.
这篇关于带有倒数计时器的 PyQt4 QMessageBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!