4

I am working with a student that uses Google colab. I tried introducing her to turtle graphics. We got this error: TclError: no display name and no $DISPLAY environment variable. When I try to look up the error all the solutions are very specific to matplotlib. That worked for my student without making any adjustment. I am looking for a solution for this that works more generally or at least works with turtle and tkinter.

Student is using a Chrome book. Google colab is what she uses at school, solving the problem in that environment would be best if possible. Did try to create a Turtle object, but this produced the same error. I did a search on the error all the post I could find talked bout this problem with matplotlib. The solution in that case was to override what I think is a rendering option by invoking .use('Agg'). I did not see an obvious equivalent for turtle. I also tried using matplotlib, to see if we got the error that I saw in the postings. We tried a simple matplotlib example and it worked without any changes. The graph output appeared as expected.

import turtle turtle.forward(100)

I expect the turtle graphics to be drawn in the results.

What I actually got were these errors:

TclError Traceback (most recent call last) in () ----> 1 turtle.forward(100)

5 frames /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

James Grimaldi
  • 41
  • 1
  • 1
  • 2

4 Answers4

11

Turtle for Google Colab notebooks Installation for Google Colab: Create an empty code cell and type:

!pip3 install ColabTurtle

Run the code cell.

Usage In any code cell, import like following:

from ColabTurtle.Turtle import *

As Colab stores the declared variables in the runtime, call this before using:

initializeTurtle()
Dharman
  • 30,962
  • 25
  • 85
  • 135
Rodrigo Campos
  • 111
  • 1
  • 3
8

Create an empty code cell, type the following pip command and run it:

!pip3 install ColabTurtle

I recommend you use your turtle like this (in a separate code piece):

import ColabTurtle.Turtle as lia
lia.initializeTurtle(initial_speed=5) 
lia.color('blue')
lia.forward(100)
lia.right(45)
lia.color('red')
lia.forward(50)

You may in fact use direct calls (skip all the lia object ref), but it's not a good idea IMHO since you want your students to get used to using instances. This simplified (less recommended way) would look like this:

import ColabTurtle
forward(100)
right(90)
forward(100)

You are not habituating students to using objects and they cannot see the tool tips (e.g. available methods and properties of the object).

Guy
  • 1,232
  • 10
  • 21
2

I prefer using https://repl.it/ to teach Turtle (there's a specific option to do just that).

jimmytt
  • 236
  • 2
  • 13
0

Turtle use Tk as a Window display. But Colab server is on the internet, it cannot open a new window on your machine and send display there. So, you cannot (easily) use Turtle on Colab.

If you really want to, there a hard way where you use virtual display, and capture screen to display. But I think it's too hard.

If you want to teach turtle to them on the browser, there's one implemented in JavaScript here.

https://rawgit.com/wrschneider99/js-turtle/master/turtle.html

korakot
  • 37,818
  • 16
  • 123
  • 144