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


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


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

turtle.Screen().turtles()

此函数用于返回屏幕上的海龟列表。这不需要任何论点。

用法:

turtle.Screen().turtles()

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

范例1:



Python3


# import package
import turtle
  
# make screen object
# and set size
sc = turtle.Screen()
sc.setup(400,300)
  
# make turlte object
t1=turtle.Turtle(shape='square')
  
# do some motion with properties
t1.color("red")
t1.circle(50)
  
# make another turlte 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>]

范例2:

Python3


# 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)

输出:

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

相关用法


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