Python中的turtle.Screen().turtles()函数
turtle模块以面向对象和面向过程的方式提供Turtle图形基元。因为它使用 Tkinter 作为底层图形,它需要安装一个支持 Tk 的 Python 版本。
turtle.Screen().turtles()
该函数用于返回屏幕上的Turtle列表。这不需要任何参数。
语法 :
turtle.Screen().turtles()
下面是上述方法的实现和一些例子:
例子1 :
# import package
import turtle
# make screen object
# and set size
sc = turtle.Screen()
sc.setup(400,300)
# make turtle object
t1=turtle.Turtle(shape='square')
# do some motion with properties
t1.color("red")
t1.circle(50)
# make another turtle object
t2=turtle.Turtle(shape='circle')
# do some motion with properties
t2.color("green")
t2.circle(40)
# get all turtle objects on screen
print(sc.turtles())
输出 :
[<turtle.Turtle object at 0x000001E90622DAC8>,<turtle.Turtle object at 0x000001E90625CC88>]</turtle.Turtle></turtle.Turtle><turtle.Turtle object at 0x000001E90622DAC8> <turtle.Turtle object at 0x000001E90625CC88>
</turtle.Turtle></turtle.Turtle>
例子2 :
# import package
import turtle
# make screen object and set size
sc = turtle.Screen()
sc.setup(400, 300)
# make first turtle and do something
t1 = turtle.Turtle(shape='square')
t1.color("red")
t1.circle(50)
# make another turtle and do something
t2 = turtle.Turtle(shape='circle')
t2.color("green")
t2.circle(40)
# get all turtles object
turt = sc.turtles()
# use first turtle object
turt[0].circle(-40)
# use another turtle object
turt[1].circle(-50)
输出 :