-
Notifications
You must be signed in to change notification settings - Fork 0
/
webview.py
executable file
·64 lines (55 loc) · 1.86 KB
/
webview.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
# -*- coding: utf-8 -*
from PyQt5.QtCore import *
from PyQt5.QtWebChannel import QWebChannel
from PyQt5.QtWidgets import *
from PyQt5.QtNetwork import *
from PyQt5.QtWebEngineCore import *
from PyQt5.QtWebEngineWidgets import *
#import jsnative
import sys
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)
#捕捉窗口关闭事件
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)
url = QUrl("http://www.baidu.com/")
# url = QUrl("http://localhost")
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 = jsnative.PythonJS()
page = wv.page()
channel = QWebChannel(page)
channel.registerObject('python', pjs)
page.setWebChannel(channel)
'''
# wv.page().setUrl(url)
wv.load(url)
wv.show()
sys.exit(app.exec_())