python海龟图形不';t画
当我运行下面的代码时,它只弹出一个窗口,不绘制任何图形 我试过参考文献中的一些例子,但在所有情况下,它都发生了。有人能帮我解决这个问题吗python海龟图形不';t画,python,turtle-graphics,Python,Turtle Graphics,当我运行下面的代码时,它只弹出一个窗口,不绘制任何图形 我试过参考文献中的一些例子,但在所有情况下,它都发生了。有人能帮我解决这个问题吗 import turtle turtle.mainloop() t = turtle.Turtle() t.color('red') t.pensize(10) t.shape('turtle') 请看这里: 启动事件循环-调用Tkinter的mainloop函数必须是海龟图形程序中的最后一条语句。如果脚本是在-n模式(无子进程)下从空闲运行的,则不得用于
import turtle
turtle.mainloop()
t = turtle.Turtle()
t.color('red')
t.pensize(10)
t.shape('turtle')
请看这里:
启动事件循环-调用Tkinter的mainloop函数必须是海龟图形程序中的最后一条语句。如果脚本是在-n模式(无子进程)下从空闲运行的,则不得用于交互式使用turtle图形
所以你的节目还有第二行
在没有mainloop的情况下,我还可以看到在repl.it中运行时绘制的红海龟:
它什么都不做的原因是因为你实际上没有告诉“绘图者”
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`.
您是在空闲状态下运行还是在独立状态下运行?我在空闲状态下和pycharm状态下都运行了这段代码,它们都给出了相同的结果(使用和不使用mainloop())。Linux还是Windows?您是否已将mainloop设置为末尾?