Sup,

  • I am simply trying to use Tkinter (a Python GUI creator) to create a GUI on my Raspberry Pi.
  • To start, I only want a GUI to show up on my screen. That's it!
  • The code DOES work on my PC
  • The code does NOT work on my Raspberry Pi

The entire script looks as follows:
(works on my PC, bringing up an empty GUI)

import Tkinter

root = Tkinter.Tk()

root.mainloop()

Trying to run the program in terminal as follows:

sudo python GUI.py

Produces the following error:

Traceback (most recent call last):

File "GUI.py", line 4, in root = Tkinter.Tk()

File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1817, in init

self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)

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

Does anybody have any idea how I can resolve this issue? The error is produced under all of the following conditions:

  • Direct HDMI to computer monitor
  • DSI connection to touchscreen LCD
  • SSH on computer monitor

For reference, if I type the following into my terminal:

echo $DISPLAY


An empty string gets printed, so I believe therein lies the problem

The following commands don't work either, which might shed light on the problem:

sudo apt-get update

startx

  • Why do you need sudo ? Are you calling script from ssh ? If yes, try setting DISPLAY varible by export DISPLAY=:0 and then running it. Hope it helps. – dhruvvyas90 Nov 19 '15 at 4:31
  • I am indeed running from SSH but I see the exact same error when I directly hardwire the Pi HDMI into a computer monitor and use a USB keyboard. – dsazer25 Nov 19 '15 at 21:35
  • 1
    I like your idea and I tried it, but now I receive the error: _tkinter.TclError: couldn't connect to display ":0" – dsazer25 Nov 19 '15 at 21:36
  • That's because you're running it without x windows. Log in to raspbian desktop by startx and try doing it. – dhruvvyas90 Nov 19 '15 at 22:48
  • executing the command "startx" leads to an error as well. I started fresh on a brand new microSD card; everything worked fine until I started installing software packages, although I'm not sure which one led to the problem. If I figure it out I'll post which software package is the culprit! – dsazer25 Nov 23 '15 at 2:43

I solved it with these two commands:

export DISPLAY=0.0
xhost +
  • where did you put these commands? – marciokoko May 14 at 0:02
  • I run it on raspi terminal – O. Aba May 21 at 14:01

you have just to write: export MPLBACKEND="agg"

  • 2
    Where would the OP add this line? What does it do? Where can they find documentation about this? How is this better/different from the other answer? – Steve Robillard Aug 9 '17 at 11:55

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.