如何在 Python QT Designer 中插入 Web 浏览器
How to insert a web browser in Python QT Designer
我已经在 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 浏览器小部件的小部件,因此我正在寻找一个小部件来实现它,方法是使用 class 或其他东西并将小部件添加到界面已在 Designer 中创建。
一个简单的解决方案是使用 QWebEngineView
,在我的例子中,我可以在 Qt Designer 中找到它:
但是如果你没有,也没关系,因为有一个插件可以推广。在 中,我指出了 QVideoWidget
是如何完成的,但在您的情况下,您应该只更改
Promoted class name: QWebEngineView
Header file: PyQt5.QtWebEngineWidgets
test.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>
main.py
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://whosebug.com/"))
window.show()
sys.exit(app.exec_())
我已经在 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 浏览器小部件的小部件,因此我正在寻找一个小部件来实现它,方法是使用 class 或其他东西并将小部件添加到界面已在 Designer 中创建。
一个简单的解决方案是使用 QWebEngineView
,在我的例子中,我可以在 Qt Designer 中找到它:
但是如果你没有,也没关系,因为有一个插件可以推广。在 QVideoWidget
是如何完成的,但在您的情况下,您应该只更改
Promoted class name: QWebEngineView
Header file: PyQt5.QtWebEngineWidgets
test.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>
main.py
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://whosebug.com/"))
window.show()
sys.exit(app.exec_())