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

PyQt5 QtWebEngineWidgets加载网页报错:Failed to initialize CategoryMgr

问题解决思路

核心问题是PyQt5的QtWebEngine加载网页时,导入comtypes或pycaw会干扰系统文本服务框架(TSF)的初始化,导致致命错误。以下是可行的解决方向:

  • 延迟导入冲突库:别在脚本开头就导入comtypes/pycaw,等QtWebEngine初始化完成、网页加载完毕后再导入。比如绑定loadFinished信号,在槽函数里导入:
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEngineView
import sys

def after_page_load():
    # 网页加载完再导入
    import comtypes
    # 这里写用到comtypes的逻辑

if __name__ == "__main__":
    app = QApplication(sys.argv)
    web_view = QWebEngineView()
    web_view.loadFinished.connect(after_page_load)
    web_view.load("https://example.com")
    web_view.show()
    sys.exit(app.exec_())
  • 手动指定COM初始化方式:comtypes和QtWebEngine的COM初始化模式可能冲突,导入comtypes前手动设置初始化参数:
import comtypes
import comtypes.client
# 调整COM初始化模式,避免和QtWebEngine冲突
comtypes.CoInitializeEx(comtypes.COINIT_APARTMENTTHREADED | comtypes.COINIT_DISABLE_OLE1DDE)
comtypes.client._set_utils_module("comtypes.client.gen_utils")

这段代码要放在QtWebEngine相关代码执行前,或者配合延迟导入使用。

  • 降级Python版本:Python3.12.x和PyQt5、comtypes的兼容性可能存在问题,试试降级到Python3.11.x,不少用户反馈这个版本下不会触发该错误。

  • 换成PyQt6:PyQt6的QtWebEngine组件在COM处理上更适配新系统,迁移过去试试,替换导入语句后调整对应API(比如信号槽写法、类名变化):

from PyQt6.QtWidgets import QApplication
from PyQt6.QtWebEngineWidgets import QWebEngineView
  • 检查系统TSF服务:按下Win+R输入services.msc,找到「Touch Keyboard and Handwriting Panel Service」和「Text Input Management Service」,确保这两个服务处于运行状态,没启动的话手动启动。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 15:00:05