2013-11-05 100 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

希望你看看文檔&掌握'龜':) –