Working with GUIs in WSL2

Aaro Alhainen
3 min readJun 26, 2021

--

I have been using Ubuntu as my daily driver for over six years and working with python and Tkinter was no issue. Lately, I started to have some networking issues and the only solution was to start using Windows since it was the only OS with fixed drivers. This move resulted in the situation where I was installing WSL2 and trying the full developer experience on Windows.

Today I continued to work on my long term project that is using Tkinter for GUI and noticed that I started to get this “_tkinter.TclError: no display name and no $DISPLAY environment variable” error which indicates that there is some issue with the display. Getting this error makes a lot of sense since the Ubuntu where I’m trying to run this Tkinter app from is running inside Windows and I bet that it has no access to the displays. Some investigations later I realized that the support for graphical programs in WSL is rather limited and hence developing anything with a GUI is not out of the box possible. So started to look for solutions and find one that works rather nicely. So, what I did?

  1. Finding an X server for windows

I choose to use the Xming based VcXsrc(https://sourceforge.net/projects/vcxsrv/) since I have used it a long time ago and had some good experience with it back then.

VcXsrv installation

2. Run the XLaunch with default settings but remember to check that you disable access control. (This step is to ease up the testing a bit)

Server initialization

3. After finishing, check that the server started correctly and in which display number it got

Checking the display id

You can check this by hovering the Xming icon in the taskbar. For me, my display id is 1.0

4. Access to the WSL side and register the previously created display in ubuntu

This can be done by running the following command with your display id:

export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk ‘{print $2}’):<your_display_id_here>

for me it looks like this:

export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk ‘{print $2}’):1.0

Alternatively, you can check your host machine IP by hand and use it like:

export DISPLAY=<host_machine_ip>:1.0

5. Test the display

If you don’t have anything to test with, you can install some x11 apps to Ubuntu by running the following command:

sudo apt-get install x11-apps

After this, you can run for example xcalc and note that now we have a client connected:

1 client is connected

And also the calculator showing up 🎉

Calculator running from Ubuntu on Windows

The next thing after getting everything to work is to add the display export export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk ‘{print $2}’):1.0 to the .bashrc so that it is registering automatically without any hassle in the future.

I hope this article helped you or at least got you a bit further with your journey in GUI development with Windows and WSL🙂

--

--

Aaro Alhainen
Aaro Alhainen

Responses (1)