问题描述
web_browser.py
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'web_browser.ui' # # Created by: PyQt5 UI code generator 5.15.2 # # 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(779,257) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setobjectName("centralwidget") self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget) self.verticalLayout.setContentsMargins(0,0) self.verticalLayout.setSpacing(0) self.verticalLayout.setobjectName("verticalLayout") MainWindow.setCentralWidget(self.centralwidget) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self,MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setwindowTitle(_translate("MainWindow","MainWindow")) 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_())
web_browser_main.py
from web_browser import * from PyQt5 import QtCore,QtWidgets from PyQt5.QtCore import QUrl from PyQt5.QtWebKitWidgets import QWebView,QWebPage import sys class TestbrowserCode: def __init__(self): self.app = QtWidgets.QApplication(sys.argv) self.mainWindow = QtWidgets.QMainWindow() self.main_ui = Ui_MainWindow() self.main_ui.setupUi(self.mainWindow) self.mainWindow.showMaximized() self.open_google() sys.exit(self.app.exec_()) def open_google(self): self.google_url = 'http://www.google.com' self.main_ui.chat_browser = QWebView() self.main_ui.chat_page = QWebPage() self.main_ui.chat_browser.setPage(self.main_ui.chat_page) self.main_ui.chat_browser.setUrl(QUrl(self.google_url)) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding,QtWidgets.QSizePolicy.Expanding) self.main_ui.chat_browser.setMinimumSize(QtCore.QSize(0,398)) self.main_ui.chat_browser.setMaximumSize(QtCore.QSize(16777215,398)) self.main_ui.chat_browser.setSizePolicy(sizePolicy) self.main_ui.chat_browser.setStyleSheet("background-color:white;border:1px solid #dadada;") self.main_ui.verticalLayout.addWidget(self.main_ui.chat_browser) self.main_ui.chat_browser.show() program = TestbrowserCode()
Χρήστος@Chris-pc MINGW64 /c/python/scripts/Papinhio player/notes $ python web_browser_main.py
(网站显示正确)
- 制作 .exe 文件 (full result here)
Traceback (most recent call last): File "C:/Python/Scripts/Papinhio player/notes/web_browser_main.py",line 4,in from PyQt5.QtWebKitWidgets import QWebView,QWebPage File "",line 991,in _find_and_load File "",line 975,in _find_and_load_unlocked File "",line 657,in _load_unlocked File "",line 556,in module_from_spec File "",line 1101,in create_module File "",line 219,in _call_with_frames_removed File "",line 973,in _find_and_load_unlocked ModuleNotFoundError: No module named 'PyQt5.QtWebKit' [17208] Failed to execute script web_browser_main
结果错误导入PyQt5.QtWebKitWidgets
msys2 mingw64 控制台中的所有内容。 如何编译上述文件?
解决方法
pyinstaller --onefile --hidden-import PyQt5.QtWebKit web_browser_main.py
上层代码包括缺少的模块QtWebKit。 我建议使用 --onedir 而不是 --OneFile 因为我认为输出 exe 更稳定。 (在像 web_browser_main 应用程序这样的应用程序中几秒钟后,我遇到了 Segmentfault 运行时错误)
现在工作!!!