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.