7

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

7
  • 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.CommentedNov 19, 2015 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
    CommentedNov 19, 2015 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
    CommentedNov 19, 2015 at 21:36
  • That's because you're running it without x windows. Log in to raspbian desktop by startx and try doing it.CommentedNov 19, 2015 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
    CommentedNov 23, 2015 at 2:43

3 Answers 3

6

I solved it with these two commands:

export DISPLAY=0.0 xhost + 
3
  • where did you put these commands?CommentedMay 14, 2018 at 0:02
  • I run it on raspi terminal
    – Ömer Aba
    CommentedMay 21, 2018 at 14:01
  • DId it work? because it does not work on mine
    – Shalomi90
    CommentedJun 17, 2019 at 8:21
6

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() master.title("tester") master.geometry("300x100") #make a label for the window label1 = Tkinter.Label(master, text='Hellooooo') # Lay out label label1.pack() # Run forever! master.mainloop() 
1
  • FYI for python3 one must instead use import tkinter and besides this solution leads to an error: TclError: couldn't connect to display ":0.0"
    – doplano
    CommentedNov 28, 2023 at 6:51
-3

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

2
  • 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?CommentedAug 9, 2017 at 11:55
  • This is only applicable for matplotlib.
    – Aloha
    CommentedJan 18, 2021 at 0:12

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.