消息对话框倒计时关闭

This commit is contained in:
Irony 2018-06-22 13:40:57 +08:00
parent 520bb94047
commit ba79212a8f
2 changed files with 64 additions and 0 deletions

View file

@ -59,6 +59,7 @@ encoding//\u68A6\u5E7B\u6811/DreamTree.py=utf-8
encoding//\u6C14\u6CE1\u63D0\u793A/BubbleTips.py=utf-8
encoding//\u6D4F\u89C8\u5668\u83B7\u53D6Cookie/WebEngineView.py=utf-8
encoding//\u6D4F\u89C8\u5668\u83B7\u53D6Cookie/WebView.py=utf-8
encoding//\u6D88\u606F\u5BF9\u8BDD\u6846\u5012\u8BA1\u65F6\u5173\u95ED/MessageBox.py=utf-8
encoding//\u754C\u9762\u7F8E\u5316/QFileSystemModel\u56FE\u6807/FileSystemModel.py=utf-8
encoding//\u754C\u9762\u7F8E\u5316/QLabel\u5706\u5F62\u5934\u50CF/CircleLabel.py=utf-8
encoding//\u754C\u9762\u7F8E\u5316/QMessageBox\u6837\u5F0F/\u65B9\u6848\u4E00/critical.py=utf-8

View file

@ -0,0 +1,63 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2018年6月22日
@author: Irony
@site: https://github.com/892768447
@email: 892768447@qq.com
@file: MessageBox
@description:
"""
from random import randrange
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QMessageBox
__Author__ = """By: Irony
QQ: 892768447
Email: 892768447@qq.com"""
__Copyright__ = "Copyright (c) 2018 Irony"
__Version__ = "Version 1.0"
class MessageBox(QMessageBox):
def __init__(self, *args, count=5, time=1000, auto=False, **kwargs):
super(MessageBox, self).__init__(*args, **kwargs)
self._count = count
self._time = time
self._auto = auto # 是否自动关闭
assert count > 0 # 必须大于0
assert time >= 500 # 必须>=500毫秒
self.setStandardButtons(self.Close) # 关闭按钮
self.closeBtn = self.button(self.Close) # 获取关闭按钮
self.closeBtn.setText('关闭(%s)' % count)
self.closeBtn.setEnabled(False)
self._timer = QTimer(self, timeout=self.doCountDown)
self._timer.start(self._time)
print('是否自动关闭', auto)
def doCountDown(self):
self.closeBtn.setText('关闭(%s)' % self._count)
self._count -= 1
if self._count <= 0:
self.closeBtn.setText('关闭')
self.closeBtn.setEnabled(True)
self._timer.stop()
if self._auto: # 自动关闭
self.accept()
self.close()
if __name__ == '__main__':
import sys
from PyQt5.QtWidgets import QApplication, QPushButton
app = QApplication(sys.argv)
w = QPushButton('点击弹出对话框')
w.resize(200, 200)
w.show()
w.clicked.connect(lambda: MessageBox(
w, text='倒计时关闭对话框', auto=randrange(0, 2)).exec_())
sys.exit(app.exec_())