forked from PyQt5/PyQt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CountDownClose.py
63 lines (53 loc) · 1.85 KB
/
CountDownClose.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2018年6月22日
@author: Irony
@site: https://pyqt5.com , 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_())