【问题标题】:W.r.t. GUI, what's the difference between Jupyter Notebook and Google Colaboratory?W.r.t. GUI,Jupyter Notebook 和 Google Colaboratory 有什么区别?
【发布时间】:2020-09-11 03:03:56
【问题描述】:

这个问题可能与this重复,但还没有给出解决方案,所以让我说清楚。

我刚刚尝试在 Google Colaboratory 上使用 Tkinter 创建一个 GUI,然后我得到了:

    ---------------------------------------------------------------------------
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

但是,在 Jupyter Notebook(通过 Anaconda)上运行的代码完全相同。 为什么会这样?他们之间有什么区别? Google Colaboratory 有什么解决方法吗? 我更喜欢 Google Colaboratory,因为可以使用他们的 GPU/TPU。

这是我尝试过的代码,取自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()

提前谢谢你。

【问题讨论】:

  • Google Colab 在不使用 GUI(Windows、XWin、X11)且没有监视器的服务器上运行代码,但 tkinter(和任何 GUI 框架)只能在连接监视器的情况下工作直接连接到计算机 - 在 Google Colab 中,这意味着显示器直接连接到服务器。 tkinter(和任何 GUI 框架)无法在服务器上运行,也无法在本地监视器上显示。没有解决方法。您只能尝试在 Google Colab 上运行 Flask 或 Django 等 Web 框架
  • @furas 请给出答案,以便我接受。我将 Flask 作为一种解决方法。我刚刚检查了 Flask 在 Google Colaboratory 上的工作,参考link。我找不到任何有效的 Django/Google Colab 示例,但 Flask 对我来说已经足够了。谢谢。

标签: python user-interface tkinter jupyter-notebook google-colaboratory


【解决方案1】:

Google Colab 在不使用 GUI(Windows、XWin、X11)且没有显示器的服务器上运行代码,但 tkinter(和任何 GUI 框架)只能与直接连接到计算机的显示器一起使用 - 在Google Colab 这意味着显示器直接连接到服务器。

tkinter(和任何 GUI 框架)无法在服务器上运行,也无法在本地监视器上显示。 Google Colab 没有解决方法。

您可以尝试运行 FlaskDjango 等 Web 框架

顺便说一句:如果您需要交互元素,Google Colabs 也有 widgets


编辑:

在其他 Linux 服务器上,您可以尝试通过 ssh 连接并使用 $DISPLAYtkinter windows 重定向到带有 X11 的本地 Linux,但我不知道 ssh 是否可以访问 Google Colab 和您需要在您的本地计算机上安装 Linux。 Web 框架可以更简单,并且可以与任何系统一起使用。

【讨论】:

    猜你喜欢
    • 2019-06-06
    • 2021-08-20
    • 2018-12-01
    • 1970-01-01
    • 2018-08-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-24
    相关资源
    最近更新 更多