_tkinter.TclError: no display name and no $DISPLAY environment variable
Matplotlib chooses Xwindows backend by default.
You need to set matplotlib to not use the Xwindows backend.
Add this code to the start of your script (before importing pyplot) and try again:
import matplotlib
matplotlib.use('Agg')
Or add to .config/matplotlib/matplotlibrc
line backend: Agg
to use non-interactive backend.
echo "backend: Agg" > ~/.config/matplotlib/matplotlibrc
Or when connect to server use ssh -X remoteMachine
command to use Xwindows.
Also you may try to export display: export DISPLAY=mymachine.com:0.0
.
For more info: https://matplotlib.org/faq/howto_faq.html#matplotlib-in-a-web-application-server
Error in GoogleColab (Python)- TclError: no display name and no $DISPLAY environment variable
It is not possible to use tkinter
on Google Colaboratory
for alot of reasons. Firstly, even though if you manage to get it working by following here, as it mentions:
However, if you want to interact with the GUI, that's going to be hard, 'cuz Colab doesn't support interactive screens out of the box.
also from other answers there:
Servers generally don't even have a display. And even if they had, you wouldn't see it. You will have to run Python on your desktop or laptop to use tkinter.
I cannot close this question as a duplicate because the other question does not have a correct answer chosen.
TkInter error: no display name and no $DISPLAY environment variable
You're apparently trying to do this with repl.it's "Python", which doesn't support the display that tkinter needs. They do offer a separate "Tkinter" option, although it's quite far down the list of languages. Here's a shortcut: https://repl.it/languages/tkinter
There you don't get that error. In order to actually get the window shown, you'll have to also add this under your current code:
root.mainloop()
Demo
Ubuntu: _tkinter.TclError: no display name and no $DISPLAY environment variable
One cannot run active GUI's inside a bash terminal unless they download external software. The following tutorial is how I found out how to solve this problem: http://pjdecarlo.com/2016/06/xming-bash-on-ubuntu-on-windows-x11-window-system-running-from-windows-10-subsystem-for-linux.html
Download xming x server: A free display server for windows operating systems, simply allows you to display gui's and other fancy things from terminals: This is where I found it: https://xming.en.softonic.com/download , then run the server and it should appear in the lower right hand of the taskbar
Run the following command from bash/ubuntu:
brandon@DESKTOP-V5LTF5T:~$ export DISPLAY=localhost:0.0
, this sets the DISPLAY variable to the local host of the newly installed xming x xerver.Now run your GUI!
brandon@DESKTOP-V5LTF5T:~$ python3 MainApp.py
DietPI: _tkinter.TclError: no display name and no $DISPLAY environment variable
This took me all day to get working. (I also posted this same answer on a different SE site here: https://raspberrypi.stackexchange.com/a/118928/60683)
Note: This is for raspbian, with main user account using the default username ("pi")
Here are the steps in order:
Name your python file main.py
and drag your python file to the desktop.
Next, create a new plain text file and add the following contents:
#!/bin/bash
python3 /home/pi/Desktop/main.py
Save this text file as launch.command
on the desktop. This is shell script to launch your python file. By default it won't work, we have to chmod it (next step)
Run terminal and run the following command:
chmod u+x /home/pi/Desktop/launch.command
Now double clicking launch.command (and selecting execute
) will launch your python file.
At this point, we are ready to get it launching on boot. If you've done any other launch attempts prior to this please undo all of those file changes you had done.
Open terminal and run the following:
sudo nano /etc/systemd/system/myproject.service
We are just going to call it myproject for now, don't change any of this until after you get it working, then feel free to attempt a rename
In the window that pops up, copy paste the following exactly:
[Unit]
Description=Start Myproject
After=graphical.target
Wants=graphical.target
[Service]
User=pi
Group=pi
ExecStart=/bin/bash -c "export DISPLAY=:0; export XAUTHORITY=/home/pi/.Xauthority; /home/pi/Desktop/launch.command"
[Install]
WantedBy=graphical.target
Now press ctrl+x to exit-and-save, it will prompt you to save changes, type "y", the filename should already be entered as .../myproject.service, if it is then press enter and it will save and exit, if the file name is blank it messed up (this just happens sometimes), quit terminal and try all of this again, sorry.
Now in terminal enter:
sudo systemctl daemon-reload
Then
sudo systemctl enable myproject.service
Lastly
sudo reboot
On launch, before anything else shows (after the boot up splash screen shows of course, but before desktop shows) your GUI will now launch, congrats!
I recommend you launch it full screen, to do that edit your python file to use this:
root.attributes('-fullscreen', True)
NOTE, 'root' is likely not what you called your TK() init.... at the end of your code you have something like XXXX.mainloop()
, change 'root' to whatever XXXX is, stick this right after your init of TK()
Once you've reboot, you are likely stuck in the app (especially if you launched it full screen), things like alt+f4 or alt+f11 won't kill your program. To exit, press the windows/menu button on your keyboard, the access panel will appear you and can go to accessories>terminal to launch a new terminal window, then run the following:
sudo systemctl stop myproject.service
The program will be killed and you will be back to your standard desktop...
NOW, to prevent it from launching again on bootup, simply rename your launch.command
file to DISABLED_launch.command
and when you're ready to start launching on bootup again set the name back.
Go get a drink, you've earned it.
Also, you can obviously put these places other than desktop but MAKE SURE you always use the full path in any of these commands, don't use the '~/..' shortcut.
tkinter.TclError: no display name and no $DISPLAY environment variable python
Found the Answer:
I just need to run export DISPLAY=:0
in their ssh session and programs run will run on the remote display. A quick example:
paulsteven@smackcoders:~$ ssh afreeth@his_ipaddress
afreeth@smackcoders:~$ export DISPLAY=:0
afreeth@smackcoders:~$ firefox
Firefox is now running on afreeth's display.
Related Topics
Python: Requests.Exceptions.Connectionerror. Max Retries Exceeded With Url
Python Ssl.Sslerror: [Ssl: Certificate_Verify_Failed] Certificate Verify Failed (_Ssl.C:748)
Importerror: No Module Named Bs4 (Beautifulsoup)
Python Super :Typeerror: _Init_() Takes 2 Positional Arguments But 3 Were Given
Replace Single Quote to Double Quote Python Pandas Dataframe
Sys.Path Different in Jupyter and Python - How to Import Own Modules in Jupyter
Get Business Days Between Start and End Date Using Pandas
Check to See If Python Script Is Running
Running Multiple Commands Simultaneously from Python
How to Repeat Each Test Multiple Times in a Py.Test Run
If-Condition With Multiple Actions in Robot Framework
Get All Rows That Have Same Value in Pandas
Running Two Python Scripts With Bash File
How to Populate New Column Based on Values in Other Columns
How to Remove Commas and Dots of Individual Word in Two Dimensional List