2013-11-05 61 views
0

我正在使用python turtle绘制五星。但是我需要清除屏幕并在点击它后重绘它。 所以像这样的过程:绘制后的Python龟,点击鼠标,清除屏幕并重新绘制

空白屏幕

  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) 

回答

1

对于0123_像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() 
+0

希望你看看文档&掌握'龟':) –