Error in GoogleColab (Python)- TclError: no display name and no $DISPLAY environment variable

Viewed 9798

Does anybody know how to fix the below error in Python in GoogleColab Notebook? I am running the below code:-

import matplotlib.pyplot as plt

from matplotlib import style
style.use('fivethirtyeight')

import tkinter as tk
from tkinter import filedialog
from tkinter import *
from PIL import ImageTk, Image

import numpy
import keras 
from keras.models import load_model

from sklearn.metrics import accuracy_score

model = load_model('training_model.h5')

classes = { 1: 'Speed Limit 20 km/h',
            2: 'Speed Limit 30 km/h',
            3: 'Speed Limit 50 km/h' }

#initialize GUI

top = tk.Tk()
top.geometry('800x600')
top.title('Traffic Sign Recognition Project Report')
top.configure(background='#EEE')

label=Label(top,background='#CDCDCD', font=('arial',15,'bold'))

sign_image = Label(top)

I get the error on the line -> top = tk.Tk()

1 Answers

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.

Related