Skip to content

AssertionError creating TurtleScreen due to bad _blankimage() type #10

@sbliven

Description

@sbliven

I get the following stack trace calling turtle.TurleScreen(myCanvas):

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
Cell In[12], line 2
      1 get_ipython().run_line_magic('pdb', 'off')
----> 2 myTS=turtle.TurtleScreen(myCanvas)

File ~/miniforge3/envs/jupyterlab/lib/python3.14/site-packages/ipyturtle3/ipyturtle3.py:250, in TurtleScreen.__init__(self, cv, mode, colormode, delay)
    229 self.canvheight = h
    230 self.xscale = self.yscale = 1.0
    232 self._shapes = {
    233            "arrow" : Shape("polygon", ((-10,0), (10,0), (0,10))),
    234           "turtle" : Shape("polygon", ((0,16), (-2,14), (-1,10), (-4,7),
    235                       (-7,9), (-9,8), (-6,5), (-7,1), (-5,-3), (-8,-6),
    236                       (-6,-8), (-4,-5), (0,-7), (4,-5), (6,-8), (8,-6),
    237                       (5,-3), (7,1), (6,5), (9,8), (7,9), (4,7), (1,10),
    238                       (2,14))),
    239           "circle" : Shape("polygon", ((10,0), (9.51,3.09), (8.09,5.88),
    240                       (5.88,8.09), (3.09,9.51), (0,10), (-3.09,9.51),
    241                       (-5.88,8.09), (-8.09,5.88), (-9.51,3.09), (-10,0),
    242                       (-9.51,-3.09), (-8.09,-5.88), (-5.88,-8.09),
    243                       (-3.09,-9.51), (-0.00,-10.00), (3.09,-9.51),
    244                       (5.88,-8.09), (8.09,-5.88), (9.51,-3.09))),
    245           "square" : Shape("polygon", ((10,-10), (10,10), (-10,10),
    246                       (-10,-10))),
    247         "triangle" : Shape("polygon", ((10,-5.77), (0,11.55),
    248                       (-10,-5.77))),
    249           "classic": Shape("polygon", ((0,0),(-5,-9),(0,-7),(5,-9))),
--> 250            "blank" : Shape("image", self._blankimage())
    251           }
    253 self._bgpics = {"nopic" : ""}
    255 self._mode = mode

File ~/miniforge3/envs/jupyterlab/lib/python3.14/turtle.py:877, in Shape.__init__(self, type_, data)
    875         data = tuple(data)
    876 elif type_ == "image":
--> 877     assert(isinstance(data, TK.PhotoImage))
    878 elif type_ == "compound":
    879     data = []

AssertionError: 

It looks like _blankimage() returns a numpy array, while TK expects a PhotoImage. I was able to patch it and run the README example with this code:

# Monkey-patch TurtleScreen
import numpy as np
from PIL import Image
import tkinter as tk
import ipycanvas
def _blankimage_patched(self):
    arr=ipycanvas.Canvas.create_image_data(self.cv,1,1)
    root = tk.Tk()
    img = tk.PhotoImage(Image.fromarray(arr.astype(np.uint8)))
    return img
turtle.TurtleScreen._blankimage = _blankimage_patched

That's not quite enough for a PR (creating a new Tk instance is probably unnecessary) but does make the package work.

Versions

  • python 3.14.3
  • tk 8.6.13
  • ipyturtle3 0.1.4
  • jupyter_core 5.9.1
  • jupyter_client 8.8.0
  • jupyter_server 2.17.0
  • jupyterlab 4.5.6
  • ipywidgets 8.1.8
  • pillow 12.1.1
  • numpy 2.4.3
  • ipycanvas 0.14.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions