canopy python turtle on mac 64bit - python

IMPORTANT i did not notice that there were two different places where i had to change the settings from Qt4 to SVG. I changed them both and the problem regarding the "no Turtle found" was solved. I need to thank Jonathan March, who turned me in the right direction with the link he suggested.
PROBLEM SOVED!!
I'm using Canopy 1.3.0.1715 (32bit) on a MacBook Pro 64bit OS 10.9.2.
When i try to use
from turtle import Turtle
Canopy says
name 'Turtle' is not defined
Here is my code, named draw.py (i want to draw a square):
from turtle import Turtle
t = Turtle()
def drawsquare(t, x, y, side):
t.up()
t.goto(x,y)
t.setheading(270)
t.down()
for count in range(4):
t.forward(side)
t.left(90)
I also created a file turtle.cfg like this
width = 300
height = 200
using_IDLE = True
colormode = 255
Please be as simple as you can, i just recently started using Python. Thanks everyone.

from turtle import Turtle
works for me on Canopy-32 bit on Mac64 (running in the Canopy python shell).
First thing to check: did you name some file turtle.py? If so, rename the file, delete the file turtle.pyc in the same directory if it exists, and try again. (If you name your file turtle.py, then python has no way to find the standard turtle module.)
Otherwise:
Where are you running this? In the Canopy python (ipython) shell?
Or did you start up Python some other way?
Wherever it is, what do you see when you type this?:
import sys, turtle
print sys.prefix
print turtle.__file__
Also, while this should not account for your import failure, be sure to read and follow the following:
https://support.enthought.com/entries/21793229-Using-Tkinter-Turtle-in-Canopy-s-IPython-panel

Related

How can I solve turtle addshape error in python?

I try to solve error for this code. I'm using mac os
file is in right directory and use gif file.
but I kept getting error. How can I solve this(please help me)
import turtle
import random
screen=turtle.Screen()
image1="/Users/jameslee/Downloads/front.gif"
image2="/Users/jameslee/Downloads/back.gif"
screen.addshape(image1)
screen.addshape(image2)
t1=turtle.Turtle()
coin=random.randint(0, 1)
if coin==0:
t1.shape(image1)
t1.stamp()
else :
t1.shape(image2)
t1.stamp()
Your code is fine. I've simplified it below for my testing purposes. The next issue is the *.gif files themselves. Either they are variants of GIF that tkinter doesn't recognize, or they aren't GIF files (e.g. something else renamed with a *.gif extension.) Since you say you're on Mac OSX, we can test this. Run /Applications/Utilities/Terminal.app, cd to the directory in question and run the Unix file command:
> cd /Users/jameslee/Downloads
> file front.gif
front.gif: GIF image data, version 89a, 50 x 50
>
Your output should be similar -- let us know what you get. Your code simplified:
from turtle import Screen, Turtle
from random import choice
image1 = "/Users/jameslee/Downloads/front.gif"
image2 = "/Users/jameslee/Downloads/back.gif"
screen = Screen()
screen.addshape(image1)
screen.addshape(image2)
turtle = Turtle()
turtle.shape(choice([image1, image2]))
turtle.stamp()
screen.exitonclick()

AttributeError: 'Turtle' object has no attribute 'shapesize' on line 14

I'm trying to make a turtle game on Repl.it and I don't know why this error keeps coming up. Here is my code:
import turtle
wn = turtle.Screen()
wn.bgcolor("white")
wn.setup(width=800, height=800)
wn.tracer(0)
# Set up the ground
ground = turtle.Turtle()
ground.color("white")
ground.shape("square")
ground.speed(0)
ground.shapesize(stretch_wid=200, stretch_len=20)
ground.speed(0)
ground.color("black")
ground.penup()
ground.goto(0, -400)
ground.direction = "stop"
It seems that the implementation of the turtle library on repl.it is somewhat limited and not all commands are available. You can either run it locally to use all commands or remove incompatible commands.
Source: https://repl.it/talk/ask/Turtle-python-resizing/7312
There is actually a way to do this.
Whenever you create a new repl instead of creating a "python with turtle" repl just create a normal python one. Then import the turtle module and it should work. When importing it on a basic python file it imports everything.
Whenever you do import turtle it is also a good idea to add from turtle import * that way you import everything.
I also had this problem
You can use Python with pygame instead of python with turtle because for some reason, it works

running python code on sublime and atom error

from turtle import *
for i in range(500): # this "for" loop will repeat these functions 500 times
forward(i)
left(91)
I want to run python code including usage of "turtle" class ,I tried to use sublime text editor after installing python environment from here : https://www.python.org/downloads/
it gives me that error
Atom:
Sublime:
I have installed atom-runner package and turtle package and still the error !
What you need to do is define what you want to move forward and turn. When you are using a module you first import it then use it's functions with the lines:
module_name.function(variables)
so you have to edit your code to make it look like this:
import turtle
for i in range(500):
turtle.forward(i)
turtle.left(91)
or
import turtle as t
and replace
'turtle'
with
't'
The problem is not in the code but the file.
You are importing your own file. You have imported turtle.py and the name of your file is turtle.py.
Never name your .py file the same as a module. Rename your file to a name that is not a module in your standard library.
The problem in sublime in sublime is that you have not have not configured it to run Python files correctly.
you are mostly good to go.
#1 always import turtle. [Looks good]
Code: import turtle
#2 Write out your handle.[Looks good]
Code: jojo = turtle.Turtle()
#3 Completion of your turtle should be closed properly, otherwise your program will close
on yourself. [I think this was missing.]
code: turtle.done()
example: This should work (atom editor)
import turtle
jojo = turtle.Turtle()
jojo.color("red")
jojo.pensize(5)
jojo.shape("turtle")
jojo.forward(100)
jojo.left(90)
jojo.forward(100)
jojo.left(90)
jojo.forward(100)
jojo.left(90)
jojo.forward(100)
turtle.done()
example: This will close on itself (atom editor)
import turtle
jojo = turtle.Turtle()
jojo.color("red")
jojo.pensize(5)
jojo.shape("turtle")
jojo.forward(100)
jojo.left(90)
jojo.forward(100)
jojo.left(90)
jojo.forward(100)
jojo.left(90)
jojo.forward(100)
#turtle.done()

importing pictures to python

I have two pictures "gif" and want to insert them as small pictures and add them to Python code. I want the picture to stay in an accurate location as well decrease it size. When i try to run this code the shell shows an error "screen isn't defined"
import turtle
import time
from tkinter import *
screen=turtle.Turtle()
screen=turtle.getscreen()
screen.register_shape("health.gif")
screen.penup()
screen.shape("health.gif")
screen.goto(x+50,y+150)
I don't know why you're getting that error... but you have a mixup with your variable names... you create a variable you call screen that is turtle.Turtle() but then you overwrite that variable by doing turtle.getscreen()
Doing:
import turtle
t=turtle.Turtle()
screen=t.getscreen()
screen.register_shape("health.gif")
t.penup()
t.shape("health.gif")
Draws a health.gif image (if that happens to be in your working directory)

Turtle module - Saving an image

I would like to figure out how to save a bitmap or vector graphics image after creating a drawing with python's turtle module. After a bit of googling I can't find an easy answer. I did find a module called canvas2svg, but I'm very new to python and I don't know how to install the module. Is there some built in way to save images of the turtle canvas? If not where do I put custom modules for python on an Ubuntu machine?
from tkinter import * # Python 3
#from Tkinter import * # Python 2
import turtle
turtle.forward(100)
ts = turtle.getscreen()
ts.getcanvas().postscript(file="duck.eps")
This will help you; I had the same problem, I Googled it, but solved it by reading the source of the turtle module.
The canvas (tkinter) object has the postscript function; you can use it.
The turtle module has "getscreen" which gives you the "turtle screen" which gives you the Tiknter canvas in which the turtle is drawing.
This will save you in encapsulated PostScript format, so you can use it in GIMP for sure but there are other viewers too. Or, you can Google how to make a .gif from this. You can use the free and open source Inkscape application to view .eps files as well, and then save them to vector or bitmap image files.
I wrote the svg-turtle package that supports the standard Turtle interface from Python, and writes an SVG file using the svgwrite module. Install it with pip install svg-turtle, and then call it like this:
from svg_turtle import SvgTurtle
def draw_spiral(t):
t.fillcolor('blue')
t.begin_fill()
for i in range(20):
d = 50 + i*i*1.5
t.pencolor(0, 0.05*i, 0)
t.width(i)
t.forward(d)
t.right(144)
t.end_fill()
def write_file(draw_func, filename, width, height):
t = SvgTurtle(width, height)
draw_func(t)
t.save_as(filename)
def main():
write_file(draw_spiral, 'example.svg', 500, 500)
print('Done.')
if __name__ == '__main__':
main()
The canvasvg package is another option. After you run some turtle code, it will convert all the items on the tkinter canvas into an SVG file. This requires tkinter support and a display, where svg-turtle doesn't.

Categories

Resources