python - 如何在 Python QT Designer 中插入 Web 浏览器
问题描述
我在 QT Designer 5 中创建了一个简单的用户界面,并希望包含一个显示网页的小部件。我使用以下代码将 ui 文件与 python 一起使用:
from PyQt5 import uic, QtWidgets
import sys
app = QtWidgets.QApplication(sys.argv)
window = uic.loadUi("test.ui")
window.show()
sys.exit(app.exec_())
似乎没有小部件可以用来在 QT Designer 中插入 Web 浏览器小部件,因此我正在寻找一个小部件来通过使用类或其他东西并将小部件添加到已经在 Designer 中创建的界面来实现这一点。
解决方案
一个简单的解决方案是使用QWebEngineView
,在我的情况下,我可以在 Qt Designer 中找到它:
但是如果你没有它,没有问题,因为有一个小部件。在上一个答案中,我指出了它是如何完成的QVideoWidget
,但在你的情况下,你应该只改变
Promoted class name: QWebEngineView
Header file: PyQt5.QtWebEngineWidgets
测试.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QWebEngineView" name="widget" native="true"/>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>QWebEngineView</class>
<extends>QWidget</extends>
<header>PyQt5.QtWebEngineWidgets</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
主文件
import os
import sys
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets, uic
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
path_ui = os.path.join(os.path.dirname(__file__), "test.ui")
window = uic.loadUi(path_ui)
window.widget.load(QtCore.QUrl("https://stackoverflow.com/"))
window.show()
sys.exit(app.exec_())
推荐阅读
- flutter - Dart/Flutter : 日期范围内的日期列表
- python-3.x - 在 exp 错误中遇到溢出
- azure-machine-learning-service - 在推理配置中指定环境
- c# - 如何将网络字节转换为int?
- sql - 如何根据 sql 中的匹配数针对关键字数组搜索字符串并排序结果?
- c++ - 如何在我的 makefile 中包含目录及其所有子目录的标题?
- .net - RadioButtonList FindControl 总是找到自己,无论 find 参数如何
- python-3.x - 使用 scipy.integrate.quad 时收到 TypeError:“无法将复数转换为浮点数”
- sql - 如何使用分隔符解析数据
- python - Pandas 如何基于两列合并两个数据框并更新下一列?