I usually create an Ubuntu environment with WSL2 for Python, and then build and use a Pyenv + Pipenv environment on it, but I needed to run a GUI program such as tkinter, so how did I set it up? I summarized.
By the way, when I try to run a GUI program (tkinter in this case) in the WSL2 environment, I get an error saying `` `_tkinter.TclError: no display name and no $ DISPLAY environment variable```.
(Aside: In the case of WSL (WSL1), it seems that it can be handled only by installing Xming Server (without setting additional parameters) introduced below and changing the environment variables in the WSL environment such as Ubuntu, but in the case of WSL2, It seems that you have to go through complicated steps because the specifications are different.)
To run the GUI program, install something called Xming Server on the Windows side to enable GUI screen output.
I used Xming Server in my environment, but VcXsrv may be easier to use. (Functions are the same)
After installation, XLaunch for Xming Server, and launch the app for VcXsrv will bring up something like a settings screen. If you keep pressing Next without changing anything, the following screen will appear, so enter `` `-ac``` in Additional parameters for Xming and keep pressing Next to set To complete.
Then, I think that the Xming Server application is running on the taskbar (the location of the background application on the right side), so hover the mouse over it and check the written character string. Check the numbers such as : 0.0
as shown in the image below. (I will use it later)
This time I will use Ubuntu.
You need to set an environment variable called DISPLAY to run the GUI program.
It depends on the environment, but this time I will set the environment variable in .bash_profile
.
.bash_profile
export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}'):0.0
This specifies Xming Server in an environment variable called DISPLAY, which can usually be set in a format like `` `DISPLAY =: 0.0```, but due to a change in the WSL2 network specifications (on localhost). Since it is necessary to specify the IP address on the Windows side (because it will not be possible to connect to), it is a long command, but it is necessary to set it like this.
After completing these settings, the GUI program will start running.
Recommended Posts