• 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()


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:


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


  • 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. 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. 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 '18 at 0:02
  • I run it on raspi terminal
    – O. Aba
    May 21 '18 at 14:01
  • DId it work? because it does not work on mine
    – Shalomi90
    Jun 17 '19 at 8:21

Here is my solution. This works for SSH terminal, or crontab:

import Tkinter
import sys
import os

if os.environ.get('DISPLAY','') == '':
    print('no display found. Using :0.0')
    os.environ.__setitem__('DISPLAY', ':0.0')

#create main window
master = Tkinter.Tk()

#make a label for the window
label1 = Tkinter.Label(master, text='Hellooooo')
# Lay out label

# Run forever!

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? Aug 9 '17 at 11:55
  • This is only applicable for matplotlib.
    – Aloha
    Jan 18 '21 at 0:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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