當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。