-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTimerMessageBox.py
57 lines (43 loc) · 1.6 KB
/
TimerMessageBox.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from soma.qt_gui.qt_backend import QtCore, QtWidgets
class TimerMessageBox(QtWidgets.QMessageBox):
""" Message box with a countdown timer that closes automatically """
def __init__(self, timeout=3, parent=None):
super(TimerMessageBox, self).__init__(parent)
self.setWindowTitle("NOT FOR MEDICAL USE")
self.time_to_wait = timeout
self.changeContent()
self.setStandardButtons(QtWidgets.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("NOT FOR MEDICAL USE (closing automatically in {0} secondes.)".format(self.time_to_wait))
if self.time_to_wait <= 0:
self.close()
self.time_to_wait -= 1
def closeEvent(self, event):
self.timer.stop()
event.accept()
class ExampleTimerMessageBox(QtWidgets.QWidget):
""" Class for testing TimerMessageBox """
def __init__(self):
super(ExampleTimerMessageBox, self).__init__()
btn = QtWidgets.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 = QtWidgets.QApplication(sys.argv)
ex = ExampleTimerMessageBox()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()