PyWebView与pythonnet兼容问题求助:解决空引用后遇下标错误
PyWebView运行报错的解决方法
测试代码
import webview webview.create_window('Hello world', 'https://pywebview.flowrl.com/hello') webview.start()
遇到的错误
第一个错误
执行上述代码时出现以下异常:
Unbehandelte Ausnahme: System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt. bei System.Windows.Forms.Control.get_CacheTextInternal() bei System.Windows.Forms.Control.get_Text() bei System.Windows.Forms.Control.set_Text(String value) --- Ende der Stapel�berwachung vom vorhergehenden Ort, an dem die Ausnahme ausgel�st wurde --- bei System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() bei Python.Runtime.PythonException.ThrowLastAsClrException() bei Python.Runtime.Dispatcher.TrueDispatch(Object[] args) bei Python.Runtime.Dispatcher.Dispatch(Object[] args) bei __System_Threading_ThreadStartDispatcher.Invoke() bei System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) bei System.Threading.ThreadHelper.ThreadStart()
第二个错误
安装pythonnet 3.0.0a2尝试解决后,出现新错误:
Traceback (most recent call last): File "C:\Users\User\Documents\Visual Studio Code\Python Projects\webviewTest.py", line 3, in <module> webview.start() File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\webview\__init__.py", line 122, in start guilib = initialize(gui) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\webview\guilib.py", line 101, in initialize if not try_import(guis): File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\webview\guilib.py", line 62, in try_import if import_func(): File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\webview\guilib.py", line 52, in import_winforms import webview.platforms.winforms as guilib File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\webview\platforms\winforms.py", line 31, in <module> import clr File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\clr.py", line 6, in <module> load() File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\pythonnet\__init__.py", line 42, in load func = _LOADER_ASSEMBLY["Python.Runtime.Loader.Initialize"] TypeError: 'Assembly' object is not subscriptable
解决步骤
1. 降级pythonnet到稳定版
pythonnet 3.0.0a2是预发布版本,API存在兼容性问题,换成稳定版即可解决第二个错误:
- 卸载现有版本:
pip uninstall -y pythonnet clr
- 安装兼容的稳定版本:
pip install pythonnet==2.5.2
2. 强制使用CEF渲染引擎
如果WinForms后端依旧报错,直接指定PyWebView使用CEF引擎,兼容性更强:
修改代码如下:
import webview webview.create_window('Hello world', 'https://pywebview.flowrl.com/hello') webview.start(gui='cef')
首次运行时PyWebView会自动下载CEF依赖,等待下载完成后即可正常启动窗口。
3. 匹配Python版本
确保Python版本在3.8-3.10之间,过新或过旧的Python版本可能和PyWebView、pythonnet存在兼容性冲突。
内容的提问来源于stack exchange,提问作者Max Payne
相关产品推荐
相关产品推荐

