我正在尝试将绘图嵌入到 PyQt5 webengine 视图中。我能够使用以下方法做到这一点:
如果您阅读它,文章会解释说,在使用 webengine 视图时,您不能直接在 HTML 中包含 javascript(加载 2 MB 以上的文件时会出现问题)。但是,我正在尝试使 javascript 的源是 plotly-min.js 的本地副本(保存在项目文件夹中),以便使用该程序的任何人都不需要互联网连接即可查看图表它生成。我尝试在 html 上遵循一些示例,但无济于事。
与互联网连接一起工作的原始代码是:
raw_html = '<html><head><meta charset="utf-8" />'
raw_html += '<script src="https://cdn.plot.ly/plotly-latest.min.js"></script></head>'
raw_html += '<body>'
raw_html += plot(fig, include_plotlyjs=False, output_type='div')
raw_html += '</body></html>'
temp_view = QWebEngineView()
temp_view.setHtml(graph)
Run Code Online (Sandbox Code Playgroud)
如果我理解正确,我需要更改这部分:
<script src="https://cdn.plot.ly/plotly-latest.min.js">
Run Code Online (Sandbox Code Playgroud)
我已经试过了:
<script type="text/javascript" src="file:///C:/Users/UserName/PycharmProjects/ProjectFolder/plotly-latest.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
老实说,我不知道 HTML。这是代码中唯一的 HTML(其余在 Python 3 中)。有谁知道为什么上述可能不起作用以及我需要改变什么?我怀疑我可能以某种方式遇到了上面提到的问题的 2 MB 限制,因为我在网上找到的关于如何在 HTML 中引用本地文件的不同变体似乎没有改变任何东西。
我正在构建一个从 Python 调用的简单 Web 应用程序。我正在使用下面的代码。加载此页面时,以编程方式授予对 Cam & Mic 访问权限的最简单方法是什么?我只在网上找到了 C++ 示例,但找不到在 Python 代码中执行此操作的方法。
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import QUrl
app = QApplication([])
view = QWebEngineView()
view.load(QUrl("https://test.webrtc.org/"))
view.show()
app.exec_()
Run Code Online (Sandbox Code Playgroud) 我创建了自己的代理来拦截 127.0.0.1:6666 上的数据包,我想要它以便从 QWebEngine 发送的所有数据包都发送到代理。我查了一下,发现一些网站说要使用 QNetworkProxy,但它从未显示如何使其正常工作。
这是我在网上找到的一个普通 QWebEngine 程序的代码示例(它工作正常)
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.webView = QtWebEngineWidgets.QWebEngineView(self.centralwidget)
self.webView.setUrl(QtCore.QUrl("http://www.google.com/"))
self.webView.setObjectName("webView")
self.gridLayout.addWidget(self.webView, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
from PyQt5 import QtWebEngineWidgets
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)
现在如何设置代理设置或其他任何内容以转到 …
我正在尝试创建 QWebEngineView 的打印预览,但我无法让它工作。
这是我的代码:
...
self.view = QWebEngineView()
...
def handle_preview(self):
dialog = QPrintPreviewDialog()
dialog.paintRequested.connect(self.view.print_)
dialog.exec_()
Run Code Online (Sandbox Code Playgroud)
代码给了我这个错误:
AttributeError: 'QWebEngineView' 对象没有属性 'print_'
当我使用 QTextEdit 时,代码运行良好。但这不是我想要的。我想使用 QWebEngineView。
我正在尝试使用 Python QWebEngineView 创建自己的浏览器。我遵循了适用于 PyQt5 之前版本(2015 年左右)的教程,但由于最近的更新,之前代码的某些部分不再起作用。
我已经修复了大多数错误,但无法执行 html 文件打开/保存。当我单击“保存”按钮时,我总是收到系统错误。以下是我的文件保存代码:
(QMainWindow类)
save_file_action = QAction(QIcon("disk--pencil.png"), "Save Page As...", self)
save_file_action.setStatusTip("Save current page to file")
file_menu.addAction(save_file_action)
Run Code Online (Sandbox Code Playgroud)
(保存文件函数)
def save_file(self):
filename, _ = QFileDialog.getSaveFilename(self, "Save Page As", "",
"Hypertext Markup Language (*.htm *.html);;"
"All files(*.*)")
if filename:
html = self.browser.page().mainFrame().toHtml()
with open(filename, 'w') as f:
f.write(html)
Run Code Online (Sandbox Code Playgroud)
谢谢。
我打算从 pyqt QWebEngine 执行一个 javascript 函数。我遵循了一个使用地图的示例,并在按下 Qt 应用程序按钮时检索了地图绑定,并编写了一个小示例。
html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<script>
function helloWorld(param1, param2) {
return "Hello world " + param1 + " " + param2;
}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
Python:
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget
from PyQt5.QtWebEngineWidgets import QWebEngineView
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.form_widget = FormWidget(self)
_widget = QWidget()
_layout = QVBoxLayout(_widget)
_layout.addWidget(self.form_widget)
self.setCentralWidget(_widget)
class FormWidget(QWidget):
def __init__(self, parent):
super(FormWidget, self).__init__(parent)
self.__controls()
self.__layout()
self.browser.page().runJavaScript("helloWorld()", self.ready) …Run Code Online (Sandbox Code Playgroud) 我想禁用创建 QWebEngineView 时默认显示的右键单击菜单。
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import QUrl
app = QApplication(sys.argv)
webBrowser = QWebEngineView()
#Some line here to delete the contextMenu
webBrowser.load(QUrl("https://stackoverflow.com/"))
webBrowser.show()
sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)
在文档中,我们可以找到一个类QWebEngineContextMenuData,它“提供上下文数据,用于通过操作填充或扩展上下文菜单...”,但这里没有什么可删除的?
我有pdf.js和Qt 5.8的问题,我试图在这个链接中使用相同的代码 在我的应用程序中使用pdf.js与Qt5.8但他不工作我不知道为什么,qt告诉我这个消息关于JS:
"js:Uncaught TypeError:无法读取未定义的属性'PDFJS'".
这是我在mainwindow中的代码:
QWebEngineView *view;
QString pdfFileURL;
QString pathToPDFjs = QString("file:///"+qApp->applicationDirPath()+"/libraries/PDF/viewer.html");
pdfFileURL = "file:///C:/Users/Administrateur/Desktop/CV.pdf";
view = new QWebEngineView();
this->setCentralWidget(view);
view->load(QUrl::fromUserInput(pathToPDFjs + QString("?file=") + pdfFileURL));
view->show();
Run Code Online (Sandbox Code Playgroud) 代码片段的示例在这里:
from PySide2 import QtCore, QtGui, QtWidgets, QtWebChannel
from PySide2.QtWebEngineWidgets import QWebEngineView, QWebEngineSettings
class AppWindow(QtWidgets.QMainWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.view = WebView(self)
self.setCentralWidget(self.view)
self.view.settings().setAttribute(QWebEngineSettings.JavascriptEnabled, True)
self.view.settings().setAttribute(QWebEngineSettings.PluginsEnabled, True);
self.page = self.view.page()
self.page.setDevToolsPage(self.page)
#self.inspector = QWebInspector(self) # Crash is here!!!
#self.inspector.setPage(self.page)
#self.inspector.hide()
Run Code Online (Sandbox Code Playgroud)
上面的三行在以前的版本中运行良好。
QWebInspectorPySide2的替代方案是什么?
我的问题的解决方法可能很简单,但仍然超出我的理解范围。我正在尝试使用PyQt5将HTML文件加载到QWebEngineView中。我这样做的方式是:
self.webView = QtWebEngineWidgets.QWebEngineView(self.splitter)
html = r"C:\DATI\git\webgis\map.html"
self.webView.setHtml(html)
Run Code Online (Sandbox Code Playgroud)
我唯一得到的是一个字符串,代表我的HTML文件的路径和名称:
C:\ DATI \ git \ webgis \ map.html
我的map.html看起来像这样:
<html>
<head>
<title>Simple Map</title>
<link rel="stylesheet" href="https://openlayers.org/en/v4.5.0/css/ol.css" type="text/css">
<!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
<script src="https://openlayers.org/en/v4.5.0/build/ol.js"></script>
<script src=".js/qwebchannel.js"></script>
<style>
body { padding: 0; margin: 0; }
html, body, #map { height: 100%; }
</style>
</head>
<body>
<div id="map" class="map"></div>
<script src="./js/map.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
奇怪的是(至少对我来说),如果这样做的话self.webView.setHtml("<html><head></head><body><h1>ciao</h1></body></html>"),它将正确地呈现HTML。
我想念什么?
我正在尝试构建一个简单的示例,在 PyQt 窗口中显示 HTML(包括 JavaScript 代码):
python
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl,QDir
from PyQt5.QtWebEngineWidgets import QWebEngineView
class Example(QWidget):
def __init__(self):
super().__init__()
vbox = QVBoxLayout(self)
self.webEngineView = QWebEngineView()
self.webEngineView.setHtml("""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="file:///home/path/to/jquery-3.6.0.min.js"></script>
</head>
<body>
<!-- page content -->
<span id="aaa">toto</span>
<script>
$("#aaa").hide()
</script>
</body>
</html>""")
vbox.addWidget(self.webEngineView)
self.setLayout(vbox)
self.show()
sys.argv.append("--disable-web-security")
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)
但显然,位于我的 Python 脚本旁边的 jQuery JavaScript …
MWE 演示这个问题是这样的:
#include <QMainWindow>
#include <QApplication>
#include <QWidget>
#include <QWebEngineView>
#include <QGridLayout>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow w;
auto w1 = new QWidget();
w1->setLayout(new GridLayout());
auto view = new QWebEngineView();
view->load(QUrl("file://C:\\Users\\FruitfulApproach\\Desktop\\AbstractSpacecraft\\MathEnglishApp\\KaTeX_template.html"));
view->show();
w1->layout()->addWidget(view);
w.setCentralWidget(w1);
w.show();
return a.exec();
}
Run Code Online (Sandbox Code Playgroud)
KaTeX_template.html:
<!DOCTYPE html>
<html>
<head>
<title>MathJax TeX Test Page</title>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}});
</script>
<script type="text/javascript" async
src="https://example.com/mathjax/MathJax.js?config=TeX-AMS_CHTML">
</script>
</head>
<body>
When $a \ne 0$, there are two solutions to \(ax^2 + bx + c …Run Code Online (Sandbox Code Playgroud) qwebengineview ×13
python ×11
pyqt ×10
pyqt5 ×10
qt ×4
c++ ×2
python-3.x ×2
javascript ×1
pdf.js ×1
plotly ×1
proxy ×1
pyside2 ×1
python-3.6 ×1
qt5 ×1
qtwebengine ×1
webrtc ×1