How to run GUI programs such as tkinter in Python environment on WSL2

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.)

Windows side support

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. スクリーンショット 2020-12-16 110417.png

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) スクリーンショット 2020-12-16 110612.png

WSL2 environment (Ubuntu) settings

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. スクリーンショット 2020-12-16 111230.png スクリーンショット 2020-12-16 111341.png

Recommended Posts

How to run GUI programs such as tkinter in Python environment on WSL2
How to install OpenCV on Cloud9 and run it in Python
How to access environment variables in Python
How to run Python on Windows without polluting the environment as much as possible (Scoop edition)
How to install python package in local environment as a general user
How to run MeCab on Ubuntu 18.04 LTS Python
How to use tkinter with python in pyenv
How to run Leap Motion in non-Apple Python
How to output "Ketsumaimo" as standard output in Python
How to run AutoGluon in Google Colab GPU environment
How to run python in virtual space (for MacOS)
How to run tests in bulk with Python unittest
How to build a Django (python) environment on docker
How to install git on Linux such as EC2
How to build a Python environment on amazon linux 2
How to update Python Tkinter to 8.6
How to run Notepad ++ Python
How to develop in Python
How to build a new python virtual environment on Ubuntu
What I got stuck around GUI in WSL python environment
Don't lose to Ruby! How to run Python (Django) on Heroku
How to use VS Code in venv environment on windows
A note on how to load a virtual environment in PyCharm
Building an environment to execute python programs on AWS EC2
Parallel processing of Python joblib does not work in uWSGI environment. How to process in parallel on uWSGI?
How to manage Python minor version (build virtual environment) on Windows (without Pyenv or WSL)
[Python] How to do PCA in Python
Run MuJoCo on WSL (2) (also GUI)
How to collect images in Python
How to use SQLite in Python
How to run matplotlib on heroku
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to run TensorFlow 1.0 code in 2.0
How to handle Japanese in Python
GUI creation in python using tkinter 2
How to use python put in pyenv on macOS with PyCall
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS
Run the output code on the local web server as "A, pretending to be B" in python
[Introduction to Python] How to use class in Python?
Create a shortcut to run a Python file in VScode on your terminal
How to debug a Python program by remotely connecting to a Docker container in WSL2 environment with VS Code
Method to build Python environment in Xcode 6
How to dynamically define variables in Python
How to do R chartr () in Python
[Kivy] How to install Kivy on Windows [Python]
[Itertools.permutations] How to put permutations in Python
How to work with BigQuery in Python
Run AzureKinect in Python on Christmas Eve.
Steps to install Python environment on Ubuntu
How to prepare Python development environment [Mac]
How to erase Python 2.x on Mac.
How to display multiplication table in python
How to extract polygon area in Python
Memorandum on how to use gremlin python
How to check opencv version in python
[Python / Tkinter] How to pass arguments to command
How to switch python versions in cloud9
How to adjust image contrast in Python