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

avatar
Borislav Hadzhiev

Last updated: Apr 13, 2024
3 min

banner

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

The Matplotlib error "_tkinter.TclError: no display name and no $DISPLAY environment variable" occurs because of the default Matplotlib backend

To solve the error set, the backend to Agg (Anti-Grain Geometry engine) or set the DISPLAY environment variable.

The first thing you should try is setting your Matplotlib backend to Agg.

main.py
import matplotlib matplotlib.use('Agg') import numpy as np import matplotlib.pyplot as plt x = np.random.randn(90) y = np.random.randn(90) print(x) plt.scatter(x, y, s=80, edgecolors='blue', facecolors='none') plt.savefig('my-img.png', dpi=150)

Make sure to add the following 2 lines above the line that imports matplotlib.pyplot (import matplotlib.pyplot as plt).

If you set the backend to Agg after importing pyplot, the error will persist.

set matplotlib backend to agg

The AGG static backend is used for png images. It produces high-quality images using the Anti-Grain Geometry engine.

The renderer is capable of writing to a file as shown in the code sample.

renderer can write to file

You can also set the backend via the MPLBACKEND environment variable.

Issue the following command in your terminal.

shell
export MPLBACKEND=Agg

set mplbackend environment variable to agg

If you ever need to unset the environment variable, issue the following command.

shell
unset MPLBACKEND

The command unsets the MPLBACKEND environment variable.

# Setting the backend to Agg in your matplotlibrc config file

You can also set the Matplotlib backend to Agg in your matplotlibrc file.

shell
echo "backend: Agg" > ~/.config/matplotlib/matplotlibrc

set matplotlib backend to agg in matplotlibrc

Alternatively, you can open the ~/.config/matplotlib/matplotlibrc with your preferred text editor and add the line backend: Agg.

shell
# Using `gedit` sudo gedit ~/.config/matplotlib/matplotlibrc # Using `nano` sudo nano ~/.config/matplotlib/matplotlibrc

Once you open your matplotlibrc config file, add the following line.

~/.config/matplotlib/matplotlibrc
backend: Agg

# Set the DISPLAY environment variable to :0.0

Another thing you can try is to set the DISPLAY environment variable to :0.0.

First, get your default value of the DISPLAY variable.

shell
echo $DISPLAY

Now, set the DISPLAY variable to :0.0.

shell
export DISPLAY=:0.0

set display variable to 0 0

Every X server has a display name of the form hostname:displaynumber.screennumber.

This information is used by applications to determine how they should connect to the server and which screen they should use by default.

The :0.0 value relates to the first screen that is attached to your first display on localhost.

This often helps when you try to run a Tkinter application on a Raspberry Pi.

If that didn't work revert your DISPLAY variable to the previous value that you got from running echo $DISPLAY.

Note that rendering images and graphs on a server is not possible because servers don't have a display.

# Try using the -X option

If you get the error when connecting to a remote machine, try to use the -X option.

shell
ssh -X your_user@your_hostname # or ssh -X your_hostname

The X Windows system is a complete, cross-platform client-server system for managing GUIs (graphic user interfaces) on single computers and on networks of computers.

This often helps if you're trying to render images, plots, etc.

# Trying to use Tkinter on Google Collab

You will also get the error if you try to use Tkinter on Google Colab.

You aren't able to use Tkinter on Google Colab because Colab doesn't support interactive screens out of the box.

Servers don't have a display, so trying to render interactive GUIs, images and graphs is not an option.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.