当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python turtle.clear()用法及代码示例


turtle 模块以面向对象和面向过程的方式提供 turtle 图形基元。由于它使用tkinter作为基础图形,因此需要安装有Tk支持的Python版本。

turtle .clear()

此函数用于从屏幕上删除海龟的图形。不要移动 turtle 的状态和位置,其他 turtle 的图纸也不受影响。不需要任何参数。

用法:turtle.clear()
参数:None
Returns:Nothing

下面是上述方法的实现和一些示例:

范例1:



# import package 
import turtle 
  
# motion 
turtle.forward(100) 
turtle.right(90) 
turtle.forward(100) 
turtle.right(90) 
turtle.forward(100) 
  
# clear the drawing 
# and remain turtle  
# as it is 
turtle.clear()

输出:

范例2:

# import package 
import turtle 
  
# make a turtle object 
# and do some drawing 
t1 = turtle.Turtle() 
t1.up() 
t1.setpos(-100, 50) 
t1.down() 
t1.circle(50) 
  
# make a turtle object 
# and do some drawing 
t2 = turtle.Turtle() 
t2.up() 
t2.setpos(50, 50) 
t2.down() 
t2.circle(50) 
  
# make a turtle object 
# and do some drawing 
t3 = turtle.Turtle() 
t3.up() 
t3.setpos(50, -100) 
t3.down() 
t3.circle(50) 
  
# make a turtle object 
# and do some drawing 
t4 = turtle.Turtle() 
t4.up() 
t4.setpos(-100, -100) 
t4.down() 
t4.circle(50) 
  
# here we clear the work done by turtle  
# objects:t1 and t3 only but turtle  
# shape remain as it is 
t1.clear() 
t3.clear()

输出:





注:本文由纯净天空筛选整理自deepanshu_rustagi大神的英文原创作品 Python – turtle.clear()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。