PyQt5基础 Cannot find reference QtWebEngineWidgets

465 阅读2分钟
  •        Python : 3.8.13
  •          OS : Windows 21H1
  •       Conda : 4.12.0
  •     PyCharm : 2022.1 (Community Edition)

UI界面

pyqt-web-error-1.png

文件结构

C:\Users\admin\PycharmProjects\pythonProject1>tree /F
文件夹 PATH 列表
卷序列号为 B0D5-61C1
C:.
│  demo.py
│  main.py
│  untitled.py
│  untitled.ui
│
├─.idea
│  │  .gitignore
│  │  misc.xml
│  │  modules.xml
│  │  pythonProject1.iml
│  │  workspace.xml
│  │
│  └─inspectionProfiles
│          profiles_settings.xml
│
└─__pycache__
        untitled.cpython-38.pyc

代码

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(921, 732)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.webEngineView = QtWebEngineWidgets.QWebEngineView(self.centralwidget)
        self.webEngineView.setGeometry(QtCore.QRect(150, 170, 300, 200))
        self.webEngineView.setUrl(QtCore.QUrl("https://juejin.cn/user/4081803083395527"))
        self.webEngineView.setObjectName("webEngineView")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 921, 23))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        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

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
import untitled

app = QApplication(sys.argv)

window = QMainWindow()

ui = untitled.Ui_MainWindow()
ui.setupUi(window)
window.show()

sys.exit(app.exec_())

报错

D:\Develop\Anaconda3\envs\pyqtenv\python.exe C:/Users/admin/PycharmProjects/pythonProject1/main.py
Traceback (most recent call last):
  File "C:/Users/admin/PycharmProjects/pythonProject1/main.py", line 3, in <module>
    import untitled
  File "C:\Users\admin\PycharmProjects\pythonProject1\untitled.py", line 39, in <module>
    from PyQt5 import QtWebEngineWidgets
ImportError: cannot import name 'QtWebEngineWidgets' from 'PyQt5' (D:\Develop\Anaconda3\envs\pyqtenv\lib\site-packages\PyQt5\__init__.py)

Process finished with exit code 1

解决方法

(pyqtenv) C:\Users\admin>pip install PyQtWebEngine
Collecting PyQtWebEngine
  Downloading PyQtWebEngine-5.15.5-cp36-abi3-win_amd64.whl (181 kB)
     |████████████████████████████████| 181 kB 547 kB/s
Requirement already satisfied: PyQt5>=5.15.4 in d:\develop\anaconda3\envs\pyqtenv\lib\site-packages (from PyQtWebEngine) (5.15.4)
Collecting PyQtWebEngine-Qt5>=5.15.2
  Downloading PyQtWebEngine_Qt5-5.15.2-py3-none-win_amd64.whl (60.0 MB)
     |████████████████████████████████| 60.0 MB 74 kB/s
Requirement already satisfied: PyQt5-sip<13,>=12.8 in d:\develop\anaconda3\envs\pyqtenv\lib\site-packages (from PyQtWebEngine) (12.10.1)
Requirement already satisfied: PyQt5-Qt5>=5.15 in d:\develop\anaconda3\envs\pyqtenv\lib\site-packages (from PyQt5>=5.15.4->PyQtWebEngine) (5.15.2)
Installing collected packages: PyQtWebEngine-Qt5, PyQtWebEngine
Successfully installed PyQtWebEngine-5.15.5 PyQtWebEngine-Qt5-5.15.2

(pyqtenv) C:\Users\admin>pip list
Package           Version
----------------- ----------
certifi           2021.10.8
click             7.1.2
pip               21.2.2
PyQt5             5.15.4
pyqt5-plugins     5.15.4.2.2
PyQt5-Qt5         5.15.2
PyQt5-sip         12.10.1
pyqt5-tools       5.15.4.3.2
PyQtWebEngine     5.15.5
PyQtWebEngine-Qt5 5.15.2
python-dotenv     0.20.0
qt5-applications  5.15.2.2.2
qt5-tools         5.15.2.1.2
setuptools        61.2.0
wheel             0.37.1
wincertstore      0.2

效果展示

  • 错误提示消失,直接运行代码,成功!

pyqt-web-error-2.png

学习资料

学习推荐


Python具有开源、跨平台、解释型和交互式等特性,值得学习。
Python的设计哲学:优雅,明确,简单。提倡用一种方法,最好是只有一种方法来做一件事。
GUI可以选择Tkinter、PySide2、wxPython、PyGObject、wxWidgets等进行创作。
代码的书写要遵守规范,这样有助于沟通和理解。
每种语言都有独特的思想,初学者需要转变思维、踏实践行、坚持积累。