66import json
77import inspect
88import pkgutil
9+ from traceback import format_exception
910from PyQt5 .QtCore import pyqtSlot , Qt
10- from PyQt5 .QtWidgets import QWidget ,QTreeWidgetItem ,QApplication
11+ from PyQt5 .QtWidgets import QWidget ,QTreeWidgetItem ,QApplication , QMessageBox
1112from PyQt5 .QtGui import QTextDocument , QTextCursor ,QIcon
1213from .fileutil import config_file ,check_and_create ,class_file
1314from .pydocc import Helper ,resolve
@@ -18,6 +19,7 @@ class kdPythonAPIViewer(QWidget,Ui_main_win):
1819 def __init__ (self ):
1920 super ().__init__ ()
2021 self .setupUi (self )
22+ self .patch_excepthook ()
2123# self.setWindowIcon(QIcon("logo.jpg"))
2224# self.setWindowIcon(QIcon(get_file_realpath("logo.png")))
2325# 获取API文档的类
@@ -41,6 +43,7 @@ def __init__(self):
4143 self .cb_sub_text .currentTextChanged .connect (self .on_cb_sub_text_currentIndexChanged )
4244
4345 self .le_class .get_api_doc_signal .connect (self .on_lv_class_seleted )
46+
4447
4548# 悬浮置顶按钮
4649# self.show_status = True
@@ -96,7 +99,8 @@ def on_cb_library_currentIndexChanged(self):
9699 check_and_create (config_file )
97100 with open (class_file ,"w" ) as f :
98101 f .write (json .dumps (self .package_map ))
99- elif hasattr (self ,"package_map" ) and hasattr (self .le_class ,"class_list" ) and cur_module != "" :
102+ # elif all([hasattr(self,"package_map"), hasattr(self.le_class,"class_list"), cur_module != ""]):
103+ elif all ([hasattr (self ,"package_map" ), hasattr (self .le_class ,"class_list" ), cur_module != "" ,hasattr (self .package_map , cur_module )]):
100104 self .le_class .class_list = self .package_map [cur_module ]
101105 children = self .get_children (cur_module )
102106 self .fillWidget (children )
@@ -327,13 +331,23 @@ def on_le_search_returnPressed(self):
327331# self.show_status = not self.show_status
328332# self.setVisible(self.show_status)
329333
334+ def new_except_hook (self ,etype , evalue , tb ):
335+ QMessageBox .information (None ,
336+ str ('error' ),
337+ '' .join (format_exception (etype , evalue , tb )))
338+ sys .exit ()
339+
340+ # 注册全局异常处理类
341+ def patch_excepthook (self ):
342+ sys .excepthook = self .new_except_hook
330343def main ():
331- app = QApplication (sys .argv )
332- win = kdPythonAPIViewer ()
333- win .showMaximized ()
334- # win.show()
335- # win.cb_library.showPopup()
336- sys .exit (app .exec_ ())
344+
345+ app = QApplication (sys .argv )
346+ # win.show()
347+ # win.cb_library.showPopup()
348+ win = kdPythonAPIViewer ()
349+ win .showMaximized ()
350+ sys .exit ( app .exec_ ())
337351
338352if __name__ == "__main__" :
339353 main ()
0 commit comments