[英]python turtle graphic doesn't draw
当我运行以下代码时,它只弹出一个窗口,不绘制任何图形。
我从参考文献中尝试了一些例子,但在所有情况下,它都发生了。 有人可以帮我解决问题吗?
import turtle
turtle.mainloop()
t = turtle.Turtle()
t.color('red')
t.pensize(10)
t.shape('turtle')
看这里:
https://docs.python.org/3.1/library/turtle.html#turtle.mainloop
启动事件循环 - 调用 Tkinter 的mainloop函数。 必须是海龟图形程序中的最后一条语句。 如果脚本以 -n 模式(无子进程)从 IDLE 内运行,则不得使用 - 用于海龟图形的交互使用。
所以你有第二行到你的程序结尾
同样没有主循环,我可以看到在 repl.it 中运行时绘制的红色海龟: https ://repl.it/@CarstenEggers/Raphael
它没有做任何事情的原因是因为你还没有真正告诉“画儿” t
到画什么。 您告诉它要做的所有事情只是设置说明:使用什么颜色,绘制什么尺寸等。
尝试运行此代码。 评论中的解释:
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`.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.