Turtle graphics for Python in Dynamo

Hi,

Dynamo is using IronPython. I was trying to import and start
Turtle graphics but got warning that no module named Turtle.

https://docs.python.org/2/library/turtle.html

I can see that ironpython support Turtle. Anyone can guide me how to get it to work?

this might help:
https://leemendelowitz.github.io/blog/how-does-python-find-packages.html

google around for sys.path, you need to add the /lib folder of the iron python install directory to your interpreter when it boots up.

3 Likes

Hi,

Thanks, this is good resource. I think I am close but missing something. Not sure why link is somehow changed and “\t” is missing from path.

TurtleDynamo1.dyn (3.3 KB)

\t is an escape character. Try to use a raw string for paths:

r"C:\Program Files\Anaconda2\Lib\lib-tk\turtle.py"

https://docs.python.org/2.0/ref/strings.html

2 Likes

@Einar_Raknes

thanks for help this works aslo I found that “\” works as well so two option

my_module_file = ‘C:\\Program Files\\Anaconda2\\Lib\\lib-tk\\turtle.py’
my_module_file = r’C:\Program Files\Anaconda2\Lib\lib-tk\turtle.py’

however this time

another warning,

No idea why it looks for Tkinter? where this name come from… and not turtle

It’s called “dependency”. Turtle depends on Tkinter to be able to draw its graphics. Here’s an excerpt from the Python Docs page:

The turtle module provides turtle graphics primitives, in both object-oriented
and procedure-oriented ways. Because it uses Tkinter for the underlying
graphics, it needs a version of Python installed with Tk support.

Basically you need to install that module as well to get Turtle to work.

Have a look at this: https://ironpython-test.readthedocs.io/en/latest/library/tkinter.html

1 Like

@Konrad_Sobon
thanks for help. I think I am step closer now. I have now error in line 39 that is in Tkinter.py file. Hmm not sure how to convinence IronPython to accept this. According to documentation this should work. Do I need any other library? Hmm

TurtleDynamo_v12_2.dyn (2.4 KB)

import clr
import sys
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
#The inputs to this node will be stored as a list in the IN variables.

dataEnteringNode = IN[0]
import imp

my_module_file = r'C:\Program Files\Anaconda2\Lib\lib-tk\Tkinter.py'
Tkinter = imp.load_source('Tkinter', my_module_file)
import Tkinter
from Tkinter import *

if dataEnteringNode == True:
	imp.find_module('turtle')
	Out = 'True'
else:
	Out = 'False'

#Assign your output to the OUT variable.
OUT = Out