-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
138 lines (120 loc) · 3.96 KB
/
main.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# -*- coding: utf-8 -*
from PyQt5.QtCore import *
from PyQt5.QtWebChannel import QWebChannel
from PyQt5.QtWidgets import *
from PyQt5.QtNetwork import *
from PyQt5.QtWebEngineWidgets import *
import configparser
import sys
import glob
#import serial
''' 串口工具 '''
'''
class SerialTool():
def __init__(self):
self.ser = serial.Serial()
self.ser.baudrate = 9600
self.ser.parity = "N"
self.ser.bytesize = 8
self.ser.stopbits = 1
self.ser.timeout = 0.25
def getId(self,port):
self.ser.port = port
data = b'AT$DTUID?'
str=""
try:
self.ser.open()
self.ser.write(data)
str = self.ser.readline()
self.ser.close()
except serial.SerialException:
pass
if str=="":
return str
else:
return str.decode("utf-8") # +DTUID:cypyzx001
'''
'''Js 桥接'''
'''前端html需要引入qwebchannel.js'''
'''http://doc.qt.io/archives/qt-5.10/qtwebengine-webenginewidgets-markdowneditor-example.html'''
'''供js调用'''
'''
class PythonJS(QObject):
__pyqtSignals__ = ("contentChanged(const QString &)")
#初始化
@pyqtSlot(str,result=str)
def testJs(self, text):
data = "";
#platform = sys.platform # .lower()
if sys.platform.startswith('win'):
ports = ['COM%s' % (i + 1) for i in range(256)]
elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
# this excludes your current terminal "/dev/tty"
ports = glob.glob('/dev/tty[A-Za-z]*')
elif sys.platform.startswith('darwin'):
ports = glob.glob('/dev/tty.*')
else:
raise EnvironmentError('Unsupported platform')
serialTool = SerialTool()
for port in ports:
try:
ser = serial.Serial(port)
# 向串口发送指令
data = serialTool.getId(ser.portstr)
if data != "":
break
ser.close()
except (OSError, serial.SerialException):
pass
return data
'''
class CustomQwebview(QWebEngineView):
def __init__(self,parent=None):
super(CustomQwebview,self).__init__(parent)
# when you want to destroy the dialog set this to True
self._want_to_close = False
self.loadFinished.connect(self._on_loadFinished)
self.page().featurePermissionRequested.connect(self._on_feature_permission_requested)
#需要重载,否则url无法跳转
def createWindow(self, QWebEnginePage_WebWindowType):
return self
#捕捉窗口关闭事件
def closeEvent(self, event):
if self._want_to_close:
super(CustomQwebview, self).closeEvent(event)
else:
event.ignore()
self.setWindowState(Qt.WindowMinimized)
def resizeEvent(self, event):
pass
@pyqtSlot()
def _on_loadFinished(self):
pass
@pyqtSlot(QUrl, 'QWebEnginePage::Feature')
def _on_feature_permission_requested(self,url,feature):
self.page().setFeaturePermission(url, feature,QWebEnginePage.PermissionGrantedByUser)
if __name__ == '__main__':
app = QApplication(sys.argv)
app.setApplicationName("webview")
cf = configparser.ConfigParser()
cf.read("config.conf")
serverUri = cf.get("common", "server_uri")
#url=QUrl("http://192.168.199.135:8090/cect29_guard/")
url = QUrl(serverUri)
wv =CustomQwebview()
wv._want_to_close=True
#去掉标题栏
#wv.setWindowFlags(Qt.FramelessWindowHint)
#wv.setWindowFlags(Qt.WindowTitleHint)
#全屏
wv.showMaximized()
#screen = QDesktopWidget().availableGeometry()
#wv.setFixedSize(screen.width(), screen.height())
#pjs = PythonJS()
page=wv.page()
#channel = QWebChannel(page)
#channel.registerObject('bridge', pjs)
#page.setWebChannel(channel)
wv.page().setUrl(url)
wv.show()
app.exec()