Is there any way to use Tkinter with Google Colaboratory?

前端 未结 2 1633
长情又很酷
长情又很酷 2021-01-20 18:10

I have a code where I\'m trying out different Tkinter widgets, but Colab sends back an error saying that there\'s no display name or variable. The exact error message looks

相关标签:
2条回答
  • 2021-01-20 18:47

    No.

    From the intro to google colab:

    Colab notebooks execute code on Google's cloud servers

    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.

    Additionally, the colab environment is a form of IPython notebook, which is not really a standard Python environment. I would not recommend trying to run tkinter programs from an IPython notebook even if you have IPython running locally.

    0 讨论(0)
  • 2021-01-20 18:52

    As Run gym-gazebo on Google Colaboratory says, you need to run a framebuffer server (that will emulate a graphical screen) and create a DISPLAY envvar pointing to it.

    !apt-get install -y xvfb # Install X Virtual Frame Buffer
    import os
    os.system('Xvfb :1 -screen 0 1600x1200x16  &')    # create virtual display with size 1600x1200 and 16 bit color. Color can be changed to 24 or 8
    os.environ['DISPLAY']=':1.0'    # tell X clients to use our virtual DISPLAY :1.0
    

    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.

    0 讨论(0)
提交回复
热议问题