-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqt_error_handling.py
60 lines (48 loc) · 1.97 KB
/
qt_error_handling.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
from PyQt5 import QtWidgets
import traceback
def python_exception_dialog(err, parent):
dialog = PythonExceptionDialog(err, parent)
dialog.show()
class PythonExceptionDialog(QtWidgets.QDialog):
def __init__(self, error, parent=None):
super(PythonExceptionDialog, self).__init__(parent)
label = QtWidgets.QLabel(self)
label.setText("Error: %s" % str(error))
self.setLayout(QtWidgets.QVBoxLayout())
self.layout().addWidget(label)
self.info_container = QtWidgets.QTextEdit(self)
self.info_container_visible = False
self.info_container.setVisible(self.info_container_visible)
self.info_container.setMinimumHeight(300)
self.info_container.setText(traceback.format_exc())
self.layout().addWidget(self.info_container)
buttons = QtWidgets.QWidget(self)
buttons.setLayout(QtWidgets.QHBoxLayout())
buttons.layout().addStretch()
ok_button = QtWidgets.QPushButton(self)
ok_button.setText("Ok")
ok_button.setDefault(True)
ok_button.clicked.connect(self.accept)
self.info_button = QtWidgets.QPushButton(self)
self.info_button.setText("More Info")
self.info_button.clicked.connect(self.info)
buttons.layout().addWidget(self.info_button)
buttons.layout().addWidget(ok_button)
self.layout().addWidget(buttons)
self.setMinimumWidth(300)
def info(self):
self.info_container_visible = not self.info_container_visible
self.info_container.setVisible(self.info_container_visible)
if self.info_container_visible:
self.info_button.setText("Hide Info")
else:
self.info_button.setText("More Info")
if __name__ == '__main__':
import sys
try:
raise Exception("Do Exception...!")
except Exception as err:
app = QtWidgets.QApplication(sys.argv)
diag = PythonExceptionDialog(err)
diag.show()
sys.exit(app.exec_())