1. 在 Windows 上安装 X11

VcXsvr 配置

2. 设置 Windows 防火墙

设置防火墙

3. 设置 WSL2

打开 WSL2,在 ~/.bashrc(如果用的其他 shell,如 zsh,使用它们自己的配置文件)中加入以下内容:

export DISPLAY=`grep -oP "(?<=nameserver ).+" /etc/resolv.conf`:0.0

然后刷新 shell 的配置,如使用命令 source ~/.bashrc

4. 测试

测试 python 程序

import matplotlib.pyplot as plt
import numpy as np

t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2*np.pi*t)
plt.plot(t, s)

plt.title('About as simple as it gets, folks')
plt.show()

5. 运行结果

如果没有安装 Python、Matplotlib 等库,可以使用以下命令安装 x11-apps 进行测试(Ubuntu 用户):


参考:

  1. Windows 10, WSL2 显示 GUI 窗口