0

我正在使用 python turtle 绘制一个五星级。但是我需要清除屏幕并在单击后重新绘制它。所以这个过程是这样的:

黑屏

  1. 点击鼠标
  2. 开始画星星
  3. 结束
  4. 点击鼠标
  5. 清屏重画

谢谢

import turtle
wn = turtle.Screen()

tess = turtle.Turtle()
tess.hideturtle()

tess.left(36)
tess.forward(100)
for a in range(4):
    tess.left(144)
    tess.forward(100)
4

1 回答 1

1

为了events像 mouse_click 一样工作,您必须在(无限)循环中运行海龟。现在,它将events相应地监听 & 进程。

要运行这样的循环,请执行wn.mainloop()

有关所有海龟的详细信息,请访问此处 - http://docs.python.org/3.1/library/turtle.html#turtle.clear

干得好。大部分解释都有相应的注释。

import turtle
wn = turtle.Screen()

tess = turtle.Turtle()

def draw(x, y): # x, y are mouse position arguments passed by onclick()

    tess.clear() # Clear out the drawing (if any)
    tess.reset() # Reset the turtle to original position
    tess.hideturtle()

    tess.left(36)
    tess.forward(100)
    for a in range(4):
        tess.left(144)
        tess.forward(100)
    tess.right(36) # to go to original place

draw(0, 0) # Draw the first time

wn.onclick(draw) # Register function draw to the event mouse_click
wn.onkey(wn.bye, "q") # Register function exit to event key_press "q"

wn.listen() # Begin listening to events like key_press & mouse_clicks
wn.mainloop()
于 2013-11-05T05:38:50.423 回答