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)
Related
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()
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
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
I am trying to insert a .gif file as a background image in my turtle world for a separate turtle to travel on, but I can't get it to work. I'm new to python and any help would be great.
Here's the current code:
from turtle import *
from Tkinter import *
def test():
turtle.bgpic("warehouse1.gif")
fd(100)
goto(50, 100)
from turtle import * makes available all names in the turtle module so you could use bare bgpic() in this case:
#!/usr/bin/env python
from turtle import *
def test():
speed(1) # set the slowest speed to see the turtle movements
bgpic('warehouse1.gif')
fd(100)
goto(50, 100)
mainloop()
test()
Note: In general you should not use wildcard imports (*) outside a Python interactive shell. See Idioms and Anti-Idioms in Python.
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.