22

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
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Zach
  • 627
  • 1
  • 5
  • 11
  • might this help you? https://stackoverflow.com/questions/19309085/no-display-name-and-no-display-environment-variable-using-tkinter-through-ssh – Paolo Jan 14 '18 at 21:57
  • The situations are different, since I am not using ssh. Everything is on my own machine. I updated my post to better clarify. There might be some insight to be gained from that post (have read multiple which are similar) but I do not know enough to figure out how to apply the information in those posts to my situation. – Zach Jan 14 '18 at 23:55
  • 1
    Well, at the risk of asking the obvious, do you have an environment variable called `$DISPLAY`? – Silvio Mayolo Jan 15 '18 at 00:45
  • The error message comes when tcl/tk for linux tries to connect to an X server. My guess is that you must tell Ubuntu to do so or tell tcl/tk to not look for one. I have no idea either way. (The solution to previous questions was to tell matplotlib to not look for X server.) Say more about what you actually did. You should be running Python directly, not through IDLE. Perhaps you did the former, in spite of the tag, because I suspect that IDLE should fail when *it* calls tkinter.Tk(). – Terry Jan Reedy Jan 15 '18 at 06:39
  • I use Linux and has `DISPLAY=:0`. I can change this in command line `DISPLAY=:0` or `export DISPLAY=:0`. Or I can put this in file `.bashrc` which is executed everytime I login - so I don't have to do it manually. – furas Jan 15 '18 at 07:56
  • Thank you for the feedback. Updated the post. Was following dome instructions on getting started with tkinter: http://www.techinfected.net/2015/09/how-to-install-and-use-tkinter-in-ubuntu-debian-linux-mint.html. Tried furas's suggestion. I have been reading about X server, as I imagine Terry's comment is on the right track. – Zach Jan 15 '18 at 15:08

3 Answers3

32

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.

Akashdeep Singh
  • 388
  • 4
  • 14
Zach
  • 627
  • 1
  • 5
  • 11
  • had issue while running gitk command. Now working fine thanks for your answer – Syed Danial Jun 16 '21 at 08:26
  • I didn't get any option to set display on windows side. On linux ssh side I added your export comand to `.bashrc`. I get no error now, but I see no window. – Mote Zart Jul 20 '22 at 14:14
8

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!

Haaris Memon
  • 81
  • 1
  • 2
  • Yep. I can't type. Or apparently, don't know the difference between "export" and "execute". Drrrrr. – Zach Nov 17 '18 at 17:39
1

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

Then use CMD in windows and it will work fine.