You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyQt6应用无法显示HTML页面问题求助(Windows 10环境)

Windows 10下PyQt6 WebEngine空白/崩溃(退出码3221226505)解决指南

问题详情

  • 系统:Windows 10
  • 操作:尝试用PyQt6加载本地或在线HTML文件
  • 已安装依赖:pip install PyQt6 PyQt6-WebEngine
  • 异常:界面空白,运行后返回退出码3221226505
  • 运行输出:
[Running] python -u "c:\Users\PC\OneDrive\Desktop\main.py"

[Done] exited with code=3221226505 in 0.46 seconds
  • 测试代码:
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout
from PyQt6.QtWebEngineWidgets import QWebEngineView
from PyQt6.QtCore import QUrl

app = QApplication([])
window = QWidget()
layout = QVBoxLayout()

view = QWebEngineView()
layout.addWidget(view)

view.setUrl(QUrl("https://www.google.com/"))

window.setLayout(layout)
window.show()

app.exec()

可行解决办法

1. 匹配Python与PyQt6版本架构

退出码3221226505大多是32/64位架构不兼容导致:

  • 确认你的Python是64位版本(Windows下PyQt6-WebEngine对32位支持极差)
  • 卸载现有包后重装稳定版本:
    pip uninstall PyQt6 PyQt6-WebEngine -y
    pip install PyQt6==6.5.2 PyQt6-WebEngine==6.5.2
    

2. 禁用WebEngine沙箱

Windows部分环境会触发沙箱冲突,修改代码添加沙箱禁用逻辑:

from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout
from PyQt6.QtWebEngineWidgets import QWebEngineView
from PyQt6.QtCore import QUrl, Qt
from PyQt6.QtWebEngineCore import QWebEngineSettings

app = QApplication([])
# 配置高DPI与沙箱禁用
app.setAttribute(Qt.ApplicationAttribute.AA_EnableHighDpiScaling)
app.setAttribute(Qt.ApplicationAttribute.AA_UseHighDpiPixmaps)

window = QWidget()
layout = QVBoxLayout()

view = QWebEngineView()
view.settings().setAttribute(QWebEngineSettings.WebAttribute.NoSandbox, True)
layout.addWidget(view)

view.setUrl(QUrl("https://www.google.com/"))

window.setLayout(layout)
window.show()

app.exec()

3. 调整文件路径与权限

  • 若加载本地HTML,必须用QUrl.fromLocalFile()生成合法本地URL:
    view.setUrl(QUrl.fromLocalFile("C:/test/index.html"))
    
  • 不要把代码放在OneDrive等同步文件夹(同步机制可能导致文件访问异常),移到本地普通目录(如C:\dev\)测试。

4. 修复VC++运行库

PyQt6-WebEngine依赖微软VC++运行库,缺失会直接崩溃:

  • 下载安装对应架构的微软VC++可再发行组件包(64位Python就装x64版本),安装后重启电脑再运行代码。

内容的提问来源于stack exchange,提问作者Doge

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 23:27:45