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
3 Answers 3
I solved it with these two commands:
export DISPLAY=0.0
xhost +
-
where did you put these commands?marciokoko– marciokoko2018年05月14日 00:02:07 +00:00Commented May 14, 2018 at 0:02
-
I run it on raspi terminalÖmer Aba– Ömer Aba2018年05月21日 14:01:20 +00:00Commented May 21, 2018 at 14:01
-
DId it work? because it does not work on mineShalomi90– Shalomi902019年06月17日 08:21:13 +00:00Commented Jun 17, 2019 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()
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()
-
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– doplano2023年11月28日 06:51:26 +00:00Commented Nov 28, 2023 at 6:51
you have just to write: export MPLBACKEND="agg"
-
2Where 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– Steve Robillard2017年08月09日 11:55:41 +00:00Commented Aug 9, 2017 at 11:55
-
This is only applicable for
matplotlib
.Aloha– Aloha2021年01月18日 00:12:49 +00:00Commented Jan 18, 2021 at 0:12
sudo
? Are you calling script from ssh ? If yes, try settingDISPLAY
varible byexport DISPLAY=:0
and then running it. Hope it helps.startx
and try doing it.