python倒计时弹框提示带注释_带倒计时时间的PyQt4 Q消息框

使用PyQt4创建一个倒计时消息框,该消息框在设定时间后自动点击默认按钮。代码中定义了一个`TimedMBox`类,继承自`QMessageBox`,并添加了定时器功能。`question`静态方法用于展示带有倒计时的询问对话框,用户可以选择默认答案或在倒计时结束后自动选择。
摘要由CSDN通过智能技术生成

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"

{cd3>中的第一个实参中的第一个}是默认的。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值