When I run the following code it only pops up a window and doesn't draw any graphic.
I tried a few examples from references but in all cases, it happened. Can someone help me to fix the problem?
import turtle
turtle.mainloop()
t = turtle.Turtle()
t.color('red')
t.pensize(10)
t.shape('turtle')
See here:
https://docs.python.org/3.1/library/turtle.html#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.
So you have the second line to the end of your program
Also without mainloop i can see an red Turtle drawn when run in repl.it: https://repl.it/@CarstenEggers/Raphael
The reason it's not doing anything is because you haven't actually told the "draw-er" t
to draw anything. All the stuff you've told it to do is just set-up instructions: what color to use, what size to draw at, etc.
Try running this code instead. Explanations in comments:
import turtle
# turtle.mainloop() # Generally not necessary to run mainloop; can just delete
t = turtle.Turtle() # Creating a turtle to do drawing
t.color('red') # Telling it what color to draw
t.pensize(10) # Telling it what size to draw at
t.shape('turtle') # What shape turtle draw-er should be: "arrow", "turtle", etc
# Now let's draw something!
t.forward(50) # Tell turtle to draw in forward direction 50 pixels
t.left(80) # Tell turtle to turn in-place 80 degrees to left
t.forward(100) # Draw 100 pixels forward
t.right(90) # Turn in-place 90 degrees right
t.forward(170) # Draw 170 pixels forward
# Done drawing!
turtle.exitonclick() # Tell program to keep picture on screen, exit on click
# Note: See how that's `turtle` and not `t`? That's because we don't want to
# tell our draw-er `t` anything: `t` is just for drawing, it doesn't control
# the larger scope of starting and exiting. If we said `t.exitonclick()`, the
# program would just break at the end, because the draw-er does not know how
# to exit or anything.
# On the the other hand, the module `turtle` (where we get the draw-er and
# everything else from) does know how to handle starting and exiting, so that's
# why we make the call to the module itself instead of the draw-er `t`.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.