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.
Related
from turtle import *
s = turtle()
why I am not getting any output? Error is occurring, saying turtle is not defined? It's python 3.7
s = turtle() statement is meant to create object of class turtle. But in module turtle there is no class by this name. That's why you're getting error NameError: name 'turtle' is not defined.
Turtle module is meant for simple drawings. You don't need to create any object (of any class) to start. Just call any drawing function straight away (and read module docs), for example:
from turtle import *
circle(50)
done()
More complex example taken from module docs:
from turtle import *
color('red', 'yellow')
begin_fill()
while True:
forward(200)
left(170)
if abs(pos()) < 1:
break
end_fill()
done()
will visualize this picture:
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
I was wondering what the easiest way to execute two or more commands at the same time in python. For example:
from turtle import *
turtle_one=Turtle()
turtle_two=Turtle()
turtle_two.left(180)
#The lines to be executed at the same time are below.
turtle_one.forward(100)
turtle_two.forward(100)
You can effectively do this using the timer event that comes with the turtle module:
from turtle import Turtle, Screen
turtle_one = Turtle(shape="turtle")
turtle_one.setheading(30)
turtle_two = Turtle(shape="turtle")
turtle_two.setheading(210)
# The lines to be executed at the same time are below.
def move1():
turtle_one.forward(5)
if turtle_one.xcor() < 100:
screen.ontimer(move1, 50)
def move2():
turtle_two.forward(10)
if turtle_two.xcor() > -100:
screen.ontimer(move2, 100)
screen = Screen()
move1()
move2()
screen.exitonclick()
With respect to threads, as suggested by others, read up on the issues discussed in posts like Multi threading in Tkinter GUI as Python's turtle module is built on Tkinter and that recent post notes:
a lot of GUI toolkits are not thread-safe, and tkinter is not an
exception
Try using the threading module.
from turtle import *
from threading import Thread
turtle_one=Turtle()
turtle_two=Turtle()
turtle_two.left(180)
Thread(target=turtle_one.forward, args=[100]).start()
Thread(target=turtle_two.forward, args=[100]).start()
This starts the turtle_one/two.forward function in the background, with 100 as an argument.
To make it easier, make a run_in_background function...
def run_in_background(func, *args):
Thread(target=func, args=args).start()
run_in_background(turtle_one.forward, 100)
run_in_background(turtle_two.forward, 100)
I'm trying to use Turtle, but for some reason, it won't let me import it. I've tried from turtle import * (and that works) but if I try print(dir(turtle)) or to use any functions, I get an error saying turtle is not defined.
from turtle import Turtle doesn't work but print(dir(Turtle)) after using from turtle import * does work. However, prefixing commands with Turtle. ie Turtle.color("red") doesn't work.
Also, the demonstrations in the turtledemo folder work. I'd really appreciate any help
This imports turtle into the main namespace and is used as follows:
import turtle
turtle.something()
Thus, you now have an identifier in your main namespace, named turtle.
This imports all visible identifiers from turtle into the main namespace
and is used differently:
from turtle import *
something()
In this scenario, turtle is not in the main namespace. Its contents
are. Thus, dir(turtle) will fail, because that identifier isn't there.
Are you sure that from turtle import Turtle doesn't work? It worked
for me.
If you import all from turtle then you have to use the Turtle object directly, like this:
from turtle import *
print(dir(Turtle))
and not like this:
from turtle import *
print(dir(turtle))
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)