Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tkinter in Ubuntu inside Windows 10. Error: "no display name and no $DISPLAY environment variable"

Tags:

I have recently installed the Ubuntu app for Windows 10 so that I can use it for a class.

I am following some cryptic assignment directions to "try out tkinter".

I installed the package python3-tk in the Ubuntu terminal window following instructions located here: How to install and use Tkinter in ubuntu / Debian / Linux mint.

So then I execute

~$ python3

with the result

Python 3.5.2 (default, Aug 18 2017, 17:48:00)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.

then the following commands:

>>> import tkinter
>>> tkinter.Tk()

With the resulting error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.5/tkinter/__init__.py", line 1871, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, 
wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
like image 322
Zach Avatar asked Jan 14 '18 21:01

Zach


People also ask

How check display variable is set in Linux?

The most used command to displays the environment variables is printenv . If the name of the variable is passed as an argument to the command, only the value of that variable is displayed. If no argument is specified, printenv prints a list of all environment variables, one variable per line.


3 Answers

Solved:

Note: This is when using the Ubuntu App inside Windows 10

Install Xming X server for Windows.

During installation there is an option to set the display to 0.

Add the line export DISPLAY=:0; to .bashrc as suggested.

like image 179
Zach Avatar answered Oct 13 '22 21:10

Zach


I was having the same problem. I recently installed the 'Bash on Ubuntu on Windows' app from the store, and I was getting the following error when running my python script that tries to create a python UI:

_tkinter.TclError: no display name and no $DISPLAY environment variable

I followed @Zach instructions however my problem still remained. This is how I fixed my issue after following @Zach.

  1. I first installed Xming X Server for Windows.
  2. I then went into my home directory ~ on the Ubuntu terminal, and opened my .bashrc file by executing vim .bashrc.
  3. At the end of the file, I added the following line: export DISPLAY=:0;. Instead of 'execute', I used 'export'.

Make sure to close all terminal instances before trying to execute the python script.

And that worked for me!

like image 31
Haaris Memon Avatar answered Oct 13 '22 20:10

Haaris Memon


If you don't want to install Xming X Server For Windows

Then use CMD in windows and it will work fine.

like image 37
Mark Angelo Carcillar Avatar answered Oct 13 '22 19:10

Mark Angelo Carcillar