-
Notifications
You must be signed in to change notification settings - Fork 2
/
testpyqt.py
83 lines (65 loc) · 2.41 KB
/
testpyqt.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
import os, sys, ctypes
sys.path.append(os.getcwd())
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
from webviewpy import Webview, webview_native_handle_kind_t
from PyQt5.QtGui import QResizeEvent
from PyQt5.QtCore import Qt, QUrl
from PyQt5.QtWidgets import (
QApplication,
QWidget,
QVBoxLayout,
QLineEdit,
QPushButton,
QHBoxLayout,
)
testQWebEngineView = False
class WebivewWidget(QWidget):
def __init__(self, parent=None, debug=False) -> None:
super().__init__(parent)
self.webview = Webview(debug=debug, window=int(self.winId()))
self.webview.bind("__on_load", self.on_load)
self.webview.init("window.__on_load(window.location)")
def on_load(self, location):
print(location)
def __getattr__(self, name):
return getattr(self.webview, (name))
def resizeEvent(self, a0: QResizeEvent) -> None:
hwnd = self.webview.get_native_handle(
webview_native_handle_kind_t.WEBVIEW_NATIVE_HANDLE_KIND_UI_WIDGET
)
r = QApplication.instance().devicePixelRatio()
ctypes.windll.User32.MoveWindow(
hwnd, 0, 0, int(r * a0.size().width()), int(r * a0.size().height()), True
)
if testQWebEngineView:
from PyQt5.QtWebEngineWidgets import QWebEngineView
class MyWindow(QWidget):
def __init__(self) -> None:
super().__init__()
layout = QVBoxLayout()
self.setLayout(layout)
btn = QPushButton("navigate")
edit = QLineEdit("https://www.bilibili.com/")
if testQWebEngineView:
self.webview = QWebEngineView()
self.webview.load(QUrl("https://www.baidu.com/"))
btn.clicked.connect(lambda _: self.webview.load(QUrl(edit.text())))
else:
self.webview = WebivewWidget()
self.webview.navigate("https://www.baidu.com/")
btn.clicked.connect(lambda _: self.webview.navigate(edit.text()))
hl = QHBoxLayout()
hl.addWidget(edit)
hl.addWidget(btn)
layout.addLayout(hl)
layout.addWidget(self.webview)
self.resize(1000, 500)
QApplication.setAttribute(Qt.ApplicationAttribute.AA_EnableHighDpiScaling)
QApplication.setAttribute(Qt.ApplicationAttribute.AA_UseHighDpiPixmaps)
QApplication.setHighDpiScaleFactorRoundingPolicy(
Qt.HighDpiScaleFactorRoundingPolicy.PassThrough
)
app = QApplication([])
window = MyWindow()
window.show()
app.exec_()