WSL2 和 PySide6

问题描述

我正在尝试在 WSL2 上运行一个简单的 PySide6 应用

$ python3 -m venv venv
$ . ./venv/bin/activate
$ pip install PySide6
$ sudo apt install libopengl0 libegl1

我的代码:

import sys
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QApplication,QLabel
                                                     
if __name__ == "__main__":
    app = QApplication(sys.argv)
    label = QLabel("Hello World",alignment=Qt.AlignCenter)
    label.show()
    sys.exit(app.exec_())

运行时出现错误

$ export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}'):0\n
$ python main.py
qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.

Available platform plugins are: eglfs,linuxfb,minimal,minimalegl,offscreen,vnc,wayland-egl,wayland,wayland-xcomposite-egl,wayland-xcomposite-glx,xcb.

[1]    2557 abort      python main.py

是否可以使用 WSL2 运行 PySide 应用程序?如果是,如何?

解决方法

1,X 服务器 https://techcommunity.microsoft.com/t5/windows-dev-appconsult/running-wsl-gui-apps-on-windows-10/ba-p/1493242

i) 安装 https://sourceforge.net/projects/vcxsrv/

ii) export DISPLAY="`grep nameserver /etc/resolv.conf | sed 's/nameserver //'`:0"

iii) 运行 xev 来测试 image

iv) 在 /etc/bash.bashrc 文件的末尾包含命令 ii:

2,sudo apt install pyside2-tools libopengl-dev

3、运行python3 xxx.py 它有效但仍然存在一些问题。输出:

QStandardPaths: XDG_RUNTIME_DIR not set,defaulting to '/tmp/runtime-dev'
libGL error: No matching fbConfigs or visuals found
libGL error: failed to load driver: swrast

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...