Fixing tkinter and Google Colab error – TclError: no display name and no $DISPLAY environment variable

Tkinter and Google Colab error – TclError: no display name and no $DISPLAY environment variable is an error which occurs because Colab runs on the cloud and can’t open a window while Tkinter creates a GUI (window).

I will explain why this error takes place and how to fix it, while also trying to add other solutions that could solve the error.

Exploring the tkinter and Google Colab error – TclError: no display name and no $DISPLAY environment variable

This is an error which occurs because Colab runs on the cloud and can’t open a window while Tkinter creates a GUI (window).

Do not mix between errors. Make sure the error message looks like the error message bellow after double checking.

                                                                       #
TclError: no display name and no $DISPLAY environment variable
                                                                       #

Bellow I will present multiple solutions some have worked for me and others have worked for other developers.

Solution 1 : Remove python3-tk and tkinter from the Colab notebook

We have already established the fact that you are getting this error since Google Colab runs on a server while Tkinter creates a window GUI.

The first workaround is to stop using the GUI, doing that is easy you can just delete the lines python3-tk and tkinter from the Colab notebook you are working with.

If this has fixed your issue congratulations. Try the method bellow if this one did not work.

Solution 2 : Modify your code to work with the new Colab GUI

If using the GUI is necessary for you. And you want to run your project on Colab then you should be ready to some modifications.

You can modify your code to work with the new Colab GUI which was added recently.

For example, you can use anvil. Here is a link to a Tutorial called Putting a web front-end on a Google Colab notebook.

https://anvil.works/learn/tutorials/google-colab-to-web-app

You can also check out how to quickly add a graphical user interface in Colab here

https://morioh.com/p/ca5508ab4cfb

Another method is to use a virtual display in Colab, try the method bellow if that is what you want to try.

Solution 3 : create a virtual display in Colab

To create a virtual display in google Colab you should try the following.

First, you need to install the X Virtual Frame Buffer , you can do that by adding these two lines to your code

                                                                       #
!apt-get install -y xvfb 
import os
                                                                       #

After that you should create a virtual display where the size is 1200×800 and 24 bit color, you can change the size to any size you want and you can do the same to the color this is just an example, the color can also be 16 bit or 8 bit. Add the line to your code

                                                                       #
os.system('Xvfb :1 -screen 0 1200x800x24  &')
                                                                       #

Finally, add this line to the code.

                                                                       #
os.environ['DISPLAY']=':1.0'
                                                                       #

I hope these methods have solved the problem for you, good luck with your Python projects.

Summing-up : 

We arrived at the end of this quest to solve the annoying error : tkinter and Google Colab error TclError no display name and no $DISPLAY environment variable , I hope me sharing my experience with you helped, I hope the other solutions helped, If you like this website support us on Kofi and keep browsing.

Thank you for reading, keep coding and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/