-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathRunCode.py
65 lines (58 loc) · 1.81 KB
/
RunCode.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
64
65
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2019年1月9日
@author: Irony
@site: https://pyqt.site https://github.com/PyQt5
@email: 892768447@qq.com
@file: Utils.RunCode
@description: 过滤排序Model
"""
import os
import runpy
import sys
import traceback
def escape(s):
s = s.replace("&", "&")
s = s.replace("<", "<")
s = s.replace(">", ">")
s = s.replace('"', """)
s = s.replace('\'', "'")
s = s.replace('\n', '<br/>')
s = s.replace(' ', ' ')
return s
def showError(message):
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QApplication, QCheckBox, QErrorMessage, QLabel,
QPushButton, QStyle)
QApplication.addLibraryPath('./Qt/plugins')
app = QApplication(sys.argv)
app.setQuitOnLastWindowClosed(True)
# 设置内置错误图标
app.setWindowIcon(app.style().standardIcon(QStyle.SP_MessageBoxCritical))
w = QErrorMessage()
w.finished.connect(lambda _: app.quit)
w.resize(600, 400)
# 去掉右上角?
w.setWindowFlags(w.windowFlags() & ~Qt.WindowContextHelpButtonHint)
w.setWindowTitle(w.tr('Error'))
# 隐藏图标、勾选框、按钮
w.findChild(QLabel, '').setVisible(False)
w.findChild(QCheckBox, '').setVisible(False)
w.findChild(QPushButton, '').setVisible(False)
w.showMessage(escape(message))
sys.exit(app.exec_())
def runCode(file):
"""运行文件
:param file: python file
"""
try:
dirPath = os.path.dirname(file)
sys.argv = [file]
sys.path.insert(0, dirPath)
os.chdir(dirPath)
runpy.run_path(file, run_name='__main__')
except SystemExit:
pass
except:
showError(traceback.format_exc())