I am trying to draw a square using turtle in python, however, it's not opening any screen.
There are no errors in the shell. I am using python 2.7.14 on windows 10
import turtle
def draw_square(some_turtle):
for i in range(1,5):
some_turtle.forward(100)
some_turtle.right(90)
def draw_art ():
window = turtle.Screen()
window.bgcolor("green")
fahim = turtle.Turtle()
fahim.shape("turtle")
fahim.color("yellow")
fahim.speed(2)
for i in range(1,37):
draw_square(fahim)
fahim.right(10)
window.exitonclick()
You've defined the functions, but you haven't called them.
Functions only run when they're called, and as you haven't called them, no animation will be displayed. This means that you need to call your functions o get the animation.
Just add draw_Art() to the end of your code and there should be an animation.
Related
Is there a simple way to change the turtle cursor to an open hand and back to normal cursor?
I have searched and did not find a thing.
I'm using Python turtle on Windows.
Any answer is appreciated!
There are few preset shapes for turtle such as, arrow, circle, etc. You can find them here https://docs.python.org/3.3/library/turtle.html?highlight=turtle#turtle.shape, or you could use the turtle.register_shape(image_name) function. Hope this helps!
Is there a simple way to change the cursor to an open hand and back to
normal cursor?
Yes, you first want the register_shape() method of the screen, passing it your GIF file name. Then you pass the same file name to the shape() method of turtle. (Newer implementations of turtle and tkinter accept more file types but traditionally this needs to be a GIF.)
Demonstrating with a guinea pig GIF found on iconsplace.com, the following code switches from the turtle cursor that comes with the Python library to a guinea pig cursor from that site when you click on the window:
from turtle import Screen, Turtle
IMAGE = "guinea-pig-icon-24.gif"
def change(x, y):
turtle.shape(IMAGE)
screen = Screen()
screen.register_shape(IMAGE)
turtle = Turtle('turtle')
screen.onclick(change)
screen.mainloop()
Switching back again is simple as calling the shape() method again.
I am trying to write a program to draw a flower, but no matter what I do it keeps throwing an "invalid syntax" error for the turtle name. I have taken out all of my other code, tried naming the turtle something different, yet nothing works. Any ideas?
import turtle
def draw_flower():
window = turtle.Screen()
window.bgcolor(#42dff4)
sam = turtle.Turtle()
sam.forward(50)
window.exitonclick()
draw_flower()
Besides quoting the color string, as noted in the comments, your lines of code are in the wrong order. For example, generally nothing should follow window.exitonclick():
window.exitonclick()
draw_flower()
Make it (or window.mainloop()) the last statement of your program as that's when your code ends and the Tk event handler loop begins. I.e. reverse the order of these two statements. The second problem is that the variable window is in the wrong scope:
def draw_flower():
window = turtle.Screen()
...
window.exitonclick()
Since it's defined locally in draw_flower(), it's not available to use globally. Here's a rework of your code addressing both issues:
import turtle
def draw_flower():
sam = turtle.Turtle()
sam.forward(50)
window = turtle.Screen()
window.bgcolor("#42dff4")
draw_flower()
window.exitonclick()
i have to draw the house of santa claus with tkinter, but when using the following code it just stops after the first line
First I import turtle and open the gui screen
import turtle as t
t.Screen()
then i use this code
>>> s = 100
>>> points = [(s,0), (s,s), (0,0), (s,s), (s/2.,2.*s), (0,s), (s,0)]
>>> for p in points:
t.goto(p)
t.mainloop()
but it stops after the first line, how do i fix this?
As is specified in the documentation for turtle.mainloop:
turtle.mainloop()
Starts event loop - calling Tkinter’s mainloop function. Must be the last statement in a turtle graphics program. Must not be used if a script is run from within IDLE in -n mode (No subprocess) - for interactive use of turtle graphics
Here however it is not the last statement: it is called for each point in points. So you have to rewrite your program to:
for p in points:
t.goto(p)
t.mainloop()
If you read the second boldface part of the specification, it is not even necessary to call it: your program is not interactive. So the following program will be sufficient:
import turtle as t
t.Screen()
for p in points:
t.goto(p)
import turtle
window = turtle.Screen()
pen = turtle.Turtle()
def star():
pen.forward(100);
turtle.onscreenclick(star())
window.mainloop()
I am very new to turtle,and python itself, I am just using some of the documentation that I found on the python website, but when I run this program it runs my function star without the screen even being clicked. The end goal of this program would be for me to click then it would run the function, then if I clicked again the screen would clear and the function would be run again.
Thanks for the help!
You need to pass reference to start function into onscreenclick, and make start to accept two arguments:
def star(x,y):
pen.forward(100);
turtle.onscreenclick(star)
I'm having an issue where a call to onscreenclick is not keeping the window open and listening when I am testing using Pycharm, however it works fine when I use an online interpreter like the one found at Skultp.org or Interactivepython.org
Here is the block of code in question:
import turtle
def position(x, y):
print x, y
wn = turtle.Screen()
wn.bgcolor("lightgreen")
tess = turtle.Turtle()
tess.color("blue")
tess.shape("turtle")
wn.onscreenclick(position)
When I run the code above in Pycharm, the interpreter immediately runs the function 'position' and prints 'None'. Following this, the program ends.
When I run the code in the online interpreters mentioned above, the program waits for clicks and prints out the x and y coordinates of each click.
I want the call to 'onscreenclick' to behave in Pycharm as it does on the online interpreters.
Just insert turtle.done() as your last statement.
import turtle
def position(x, y):
print x, y
wn = turtle.Screen()
wn.bgcolor("lightgreen")
tess = turtle.Turtle()
tess.color("blue")
tess.shape("turtle")
wn.onscreenclick(position)
turtle.done()