Skip to content

Commit 50571d4

Browse files
committed
解决无法启动的问题,解决启动时一闪而过自动关闭的问题
1 parent 2e700f1 commit 50571d4

2 files changed

Lines changed: 25 additions & 9 deletions

File tree

kdPythonAPIViewer/kdPythonAPIViewer.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
import json
77
import inspect
88
import pkgutil
9+
from traceback import format_exception
910
from PyQt5.QtCore import pyqtSlot, Qt
10-
from PyQt5.QtWidgets import QWidget,QTreeWidgetItem,QApplication
11+
from PyQt5.QtWidgets import QWidget,QTreeWidgetItem,QApplication, QMessageBox
1112
from PyQt5.QtGui import QTextDocument, QTextCursor,QIcon
1213
from .fileutil import config_file,check_and_create,class_file
1314
from .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
330343
def 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

338352
if __name__ == "__main__":
339353
main()

main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
from tkinter import messagebox
99

1010
if __name__ == '__main__':
11-
from kdPythonAPIViewer.kdPythonAPIViewer import main
1211
try:
12+
# 防止初始化异常
13+
from kdPythonAPIViewer.kdPythonAPIViewer import main
1314
main()
1415
except Exception as e:
16+
print("系统异常", str(e))
1517
messagebox.showerror("系统异常", str(e))

0 commit comments

Comments
 (0)