0

This question might be a duplicate of this, but no solution has been given yet, so let me make it clear.

I've just tried to create a GUI with Tkinter on Google Colaboratory, then I got:

    ---------------------------------------------------------------------------
TclError                                  Traceback (most recent call last)
<ipython-input-38-3d592263964a> in <module>()
     21         print("hi there, everyone!")
     22 
---> 23 root = tk.Tk()
     24 app = Application(master=root)
     25 app.mainloop()

/usr/lib/python3.6/tkinter/__init__.py in __init__(self, screenName, baseName, className, useTk, sync, use)
   2021                 baseName = baseName + ext
   2022         interactive = 0
-> 2023         self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
   2024         if useTk:
   2025             self._loadtk()

TclError: no display name and no $DISPLAY environment variable

However, exactly the same code works on Jupyter Notebook (through Anaconda). Why does this happen? What's the difference between them? Is there any workaround on Google Colaboratory? I prefer Google Colaboratory because their GPU/TPU could be used.

Here's the code I tried, taken from A Simple Hello World Program:

import tkinter as tk

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.create_widgets()

    def create_widgets(self):
        self.hi_there = tk.Button(self)
        self.hi_there["text"] = "Hello World\n(click me)"
        self.hi_there["command"] = self.say_hi
        self.hi_there.pack(side="top")

        self.quit = tk.Button(self, text="QUIT", fg="red",
                              command=self.master.destroy)
        self.quit.pack(side="bottom")

    def say_hi(self):
        print("hi there, everyone!")

root = tk.Tk()
app = Application(master=root)
app.mainloop()

Thank you in advance.

IanHacker
  • 541
  • 9
  • 27
  • 1
    Google Colab runs code on server which doesn't use GUI (Windows, XWin, X11) and it doesn't have monitor but `tkinter` (and any GUI framework) can works only with monitor connected directly to computer - in Google Colab this means monitor connected directly to server. `tkinter` (and any GUI framework) can't work on servers and can't display on your local monitor. There is no workaround. You may only try to run Web Frameworks like Flask or Django on Google Colab – furas May 25 '20 at 02:15
  • @furas Please make it an answer, so that I can accept it. I take Flask as a workaround. I've just checked that Flask works on Google Colaboratory, referring to [link](https://stackoverflow.com/questions/54465816/how-to-use-flask-in-google-colaboratory-python-notebook). I coundn't find any working Django/Google Colab example, but Flask is enough for me. Thank you. – IanHacker May 25 '20 at 08:52

1 Answers1

1

Google Colab runs code on server which doesn't use GUI (Windows, XWin, X11) and it doesn't have monitor but tkinter (and any GUI framework) can works only with monitor connected directly to computer - in Google Colab this means monitor connected directly to server.

tkinter (and any GUI framework) can't work on servers and can't display on your local monitor. There is no workaround for Google Colab.

You may try to run Web Frameworks like Flask or Django

BTW: Google Colabs has also widgets if you need interactive elements.


EDIT:

On other Linux servers you could try to connect by ssh and use $DISPLAY to redirect tkinter windows to local Linux with X11 but I don't know if Google Colab can be accessed by ssh and you would need Linux on your local computer. Web Frameworks can be easier and it can works with any system.

furas
  • 134,197
  • 12
  • 106
  • 148