Cách định vị văn bản trong python rùa

Mô-đun Python Turtle Trong cửa sổ con rùa, bạn có thể thấy một đầu mũi tên ở giữa cửa sổ và nó hướng về bên phải. Đây là vị trí mặc định. (0,0) là vị trí mặc định của con rùa ở giữa cửa sổ

tu. write(chuỗi, di chuyển, căn chỉnh, (tên phông chữ, cỡ chữ, loại phông chữ)) ghi chuỗi đã cho vào vị trí hiện tại của con rùa. Nó được định vị như được biểu thị bởi căn chỉnh, có thể nhận các giá trị (chuỗi) "trái", "giữa" hoặc "phải". Văn bản được in theo tên phông chữ được đặt tên, với kích thước điểm phông chữ, theo kiểu phông chữ

Âm mưu sử dụng Rùa. Để tận dụng các phương thức và chức năng của rùa, chúng ta cần nhập rùa. "rùa" đi kèm với gói Python tiêu chuẩn và không cần cài đặt bên ngoài. Lộ trình thực hiện chương trình con rùa theo 4 bước. Nhập mô-đun rùa; . Vẽ xung quanh bằng phương pháp rùa

Con rùa Python ban đầu được đặt ở giữa cửa sổ đồ họa. Để hiển thị con rùa trong cửa sổ, bạn có thể sử dụng lệnh rùa. lệnh showturtle(), để ẩn rùa, bạn có thể sử dụng lệnh rùa. lệnh hideturtle()

Lưu ý phần if height < 0, kiểm tra xem chiều cao của thanh có âm không. Trong trường hợp đó, con rùa di chuyển trở lại (tur_pen. chuyển tiếp (-15)) để khi ghi giá trị, văn bản không chồng lên thanh

Bạn có thể sử dụng một con rùa để viết văn bản. con rùa. viết ('Xin chào. '. Đặt màu cho rùa để tạo văn bản có màu. con rùa. rùa màu ('hồng đậm'). viết ('Xin chào. '. Bạn cũng có thể thay đổi phông chữ và căn lề của văn bản

Bạn có thể thử từng lệnh như vậy. >>>. >>> t. đúng(90) >>> t. chuyển tiếp (100) >>> t. trái(90) >>> t. lùi (100) Khi bạn chạy các lệnh này, con rùa sẽ rẽ phải 90 độ, tiến 100 đơn vị, rẽ trái 90 độ và lùi 100 đơn vị

di chuyển – Đúng/Sai ; . write("messi fan", font=("Arial", 16, "normal")) để thay đổi cỡ chữ thành 16 (mặc định là 8)

Cách mô phỏng làm mờ nhãn văn bản từ rùa trăn mà không làm gián đoạn các tem khác. Bạn phải tự làm điều này bằng cách xóa và vẽ lại tem/văn bản bằng màu nhạt hơn. Ví dụ sau đây tạo ra hai con rùa, một con có tên là Alex di chuyển xung quanh và một bản sao sâu khác của alex có tên là alex_text

Hướng dẫn rùa trăn này bao gồm việc sử dụng các lần nhấn phím của người dùng và các sự kiện để di chuyển một đối tượng rùa xung quanh màn hình. Rùa Python rất tốt cho đồ họa 2d trong python

Bạn có thể sử dụng con rùa. lệnh goto (x, y) để di chuyển con rùa từ vị trí hiện tại của nó đến một vị trí cụ thể trong cửa sổ đồ họa. >>> con rùa. goto(100,100) Nếu chuồng của rùa hạ xuống, một đường sẽ được vẽ khi rùa di chuyển. Mặt phẳng tọa độ

con rùa. hàm textinput() trong Python. Mô-đun rùa cung cấp các nguyên mẫu đồ họa rùa, theo cả hai cách hướng đối tượng và hướng thủ tục. Bởi vì nó sử dụng Tkinter cho đồ họa cơ bản, nên nó cần cài đặt phiên bản Python có hỗ trợ Tk

Đặt màu cho rùa để tạo văn bản có màu. con rùa. rùa màu ( 'hồng đậm' ). viết ( 'Xin chào. ' Bạn cũng có thể thay đổi phông chữ và căn chỉnh của văn bản

Rùa () Không có. Tạo và trả về một đối tượng rùa mới. chuyển tiếp () số tiền. Di chuyển con rùa về phía trước theo số tiền được chỉ định. lùi () lượng. Di chuyển con rùa về phía sau một lượng đã chỉ định

Mô-đun con rùa cho phép chúng tôi phát hiện khi người dùng nhấn một số phím nhất định trên bàn phím hoặc di chuyển/nhấn chuột. Bất cứ khi nào người dùng thực hiện một hành động như vậy, nó được gọi là một sự kiện. Chúng ta có thể lắng nghe các sự kiện và kích hoạt các chức năng để chạy nếu chúng ta "nghe thấy" sự kiện

Hàm input() lấy đầu vào từ bảng điều khiển của bạn. Nó đọc đầu vào và chuyển đổi nó thành một chuỗi

Kivy là một công cụ GUI độc lập với nền tảng trong Python. Vì nó có thể chạy trên Android, IOS, linux và Windows, v.v. Về cơ bản, nó được sử dụng để phát triển ứng dụng Android, nhưng điều đó không có nghĩa là nó không thể được sử dụng trên các ứng dụng Máy tính để bàn

Đồ họa con rùa là một cách phổ biến để giới thiệu lập trình cho trẻ em. Nó là một phần của ngôn ngữ lập trình Logo ban đầu được phát triển bởi Wally Feurzeig, Seymour Papert và Cynthia Solomon vào năm 1967

Hãy tưởng tượng một con rùa robot bắt đầu từ (0, 0) trong mặt phẳng x-y. Sau một

>>> turtle.setheading(90)
>>> turtle.heading()
90.0
9, hãy cho nó lệnh
>>> turtle.heading()
90.0
>>> turtle.position()
(0.00,-10.00)
>>> turtle.home()
>>> turtle.position()
(0.00,0.00)
>>> turtle.heading()
0.0
0, và nó di chuyển (trên màn hình. ) 15 pixel theo hướng mà nó hướng tới, vẽ một đường khi nó di chuyển. Đưa cho nó lệnh
>>> turtle.heading()
90.0
>>> turtle.position()
(0.00,-10.00)
>>> turtle.home()
>>> turtle.position()
(0.00,0.00)
>>> turtle.heading()
0.0
1, và nó xoay tại chỗ 25 độ theo chiều kim đồng hồ

rùa sao

Rùa có thể vẽ các hình phức tạp bằng các chương trình lặp lại các bước di chuyển đơn giản

Cách định vị văn bản trong python rùa

from turtle import *
color('red', 'yellow')
begin_fill()
while True:
    forward(200)
    left(170)
    if abs(pos()) < 1:
        break
end_fill()
done()

Bằng cách kết hợp các lệnh này với nhau và các lệnh tương tự, có thể dễ dàng vẽ các hình và ảnh phức tạp.

Mô-đun này là sự triển khai lại mở rộng của mô-đun cùng tên từ bản phân phối tiêu chuẩn Python cho đến phiên bản Python 2. 5

Nó cố gắng giữ những ưu điểm của mô-đun rùa cũ và tương thích (gần như) 100% với nó. Điều này có nghĩa là ngay từ đầu phải cho phép người lập trình học sử dụng tất cả các lệnh, lớp và phương thức một cách tương tác khi sử dụng mô-đun từ bên trong IDLE chạy với công tắc

>>> turtle.heading()
90.0
>>> turtle.position()
(0.00,-10.00)
>>> turtle.home()
>>> turtle.position()
(0.00,0.00)
>>> turtle.heading()
0.0
3

Mô-đun rùa cung cấp các nguyên mẫu đồ họa rùa, theo cả hai cách hướng đối tượng và hướng thủ tục. Bởi vì nó sử dụng cho đồ họa cơ bản, nên nó cần cài đặt phiên bản Python có hỗ trợ Tk

Giao diện hướng đối tượng về cơ bản sử dụng hai+hai lớp

  1. Lớp định nghĩa cửa sổ đồ họa là sân chơi cho rùa vẽ. Hàm tạo của nó cần một đối số

    >>> turtle.heading()
    90.0
    >>> turtle.position()
    (0.00,-10.00)
    >>> turtle.home()
    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.heading()
    0.0
    
    6 hoặc as. Nó nên được sử dụng khi được sử dụng như một phần của một số ứng dụng

    Hàm trả về một đối tượng đơn lẻ của một lớp con. Chức năng này nên được sử dụng khi được sử dụng như một công cụ độc lập để làm đồ họa. Là một đối tượng đơn lẻ, không thể kế thừa từ lớp của nó

    Tất cả các phương thức của TurtleScreen/Screen cũng tồn tại dưới dạng hàm, tôi. e. như một phần của giao diện hướng thủ tục

  2. (bí danh. ) định nghĩa các đối tượng Rùa vẽ trên một. Hàm tạo của nó cần một Canvas, ScrolledCanvas hoặc TurtleScreen làm đối số, vì vậy các đối tượng RawTurtle biết nơi để vẽ

    Bắt nguồn từ RawTurtle là phân lớp (bí danh.

    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.forward(25)
    >>> turtle.position()
    (25.00,0.00)
    >>> turtle.forward(-75)
    >>> turtle.position()
    (-50.00,0.00)
    
    06), dựa trên “thực thể” được tạo tự động, nếu chưa có

    Tất cả các phương thức của RawTurtle/Turtle cũng tồn tại dưới dạng hàm, tôi. e. một phần của giao diện hướng thủ tục

Giao diện thủ tục cung cấp các chức năng bắt nguồn từ các phương thức của các lớp và. Chúng có cùng tên với các phương thức tương ứng. Một đối tượng màn hình được tạo tự động bất cứ khi nào một hàm bắt nguồn từ phương thức Màn hình được gọi. Một đối tượng rùa (chưa được đặt tên) được tạo tự động bất cứ khi nào bất kỳ hàm nào bắt nguồn từ phương thức Rùa được gọi

Để sử dụng nhiều con rùa trên một màn hình, người ta phải sử dụng giao diện hướng đối tượng

Ghi chú

Trong tài liệu sau đây, danh sách đối số cho các hàm được đưa ra. Tất nhiên, các phương thức có đối số đầu tiên bổ sung self được bỏ qua ở đây

Tổng quan về các phương pháp Con rùa và Màn hình có sẵn

phương pháp rùa

Chuyển động của rùa Di chuyển và vẽ

.

. .

.

.

. .

.

Cho biết trạng thái của Rùa

.

Cài đặt và đo lường

Điều khiển bút Trạng thái vẽ

. .

. .

.

Kiểm soát màu sắc

đổ đầy

Kiểm soát bản vẽ nhiều hơn

Trạng thái rùaKhả năng hiển thị

.

.

Vẻ bề ngoài

.

Sử dụng các sự kiện

Phương pháp rùa đặc biệt

.

Phương pháp của TurtleScreen/Screen

Kiểm soát cửa sổ

kiểm soát hoạt hình

Sử dụng các sự kiện màn hình

.

.

.

Cài đặt và phương pháp đặc biệt

.

Phương thức nhập liệu

Các phương pháp dành riêng cho Màn hình

Các phương thức của RawTurtle/Turtle và các hàm tương ứng

Hầu hết các ví dụ trong phần này đề cập đến một cá thể Rùa có tên là

>>> turtle.setheading(90)
>>> turtle.heading()
90.0
8

chuyển động rùa

rùa. về phía trước(khoảng cách)rùa. fd(khoảng cách) Tham số

khoảng cách – một số (số nguyên hoặc số thực)

Di chuyển rùa về phía trước theo khoảng cách xác định, theo hướng rùa đang đi

>>> turtle.position()
(0.00,0.00)
>>> turtle.forward(25)
>>> turtle.position()
(25.00,0.00)
>>> turtle.forward(-75)
>>> turtle.position()
(-50.00,0.00)

rùa. lùi lại(khoảng cách)rùa. bk(khoảng cách)rùa. lùi(khoảng cách) Tham số

khoảng cách - một con số

Di chuyển rùa lùi một đoạn, ngược với hướng rùa đi. Không thay đổi tiêu đề của rùa

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)

rùa. phải(góc)rùa. rt(góc) Tham số

góc – một số (số nguyên hoặc số thực)

Xoay rùa sang phải theo đơn vị góc. (Các đơn vị theo độ mặc định, nhưng có thể được thiết lập thông qua chức năng và. ) Định hướng góc phụ thuộc vào chế độ con rùa, xem

>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0

rùa. trái(góc)rùa. lt(góc) Tham số

góc – một số (số nguyên hoặc số thực)

Xoay rùa sang trái theo đơn vị góc. (Các đơn vị theo độ mặc định, nhưng có thể được thiết lập thông qua chức năng và. ) Định hướng góc phụ thuộc vào chế độ con rùa, xem

>>> turtle.heading()
22.0
>>> turtle.left(45)
>>> turtle.heading()
67.0

rùa. goto(x , y . =None)turtle.đặt vị trí(x , y . =None)turtle.vị trí đặt(x , y=None)Parameters
  • x – một số hoặc một cặp/vectơ số

  • y – một số hoặc

    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.backward(30)
    >>> turtle.position()
    (-30.00,0.00)
    
    29

Nếu y là

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
29, x phải là một cặp tọa độ hoặc một (e. g. như được trả về bởi )

Di chuyển rùa đến một vị trí tuyệt đối. Nếu bút xuống, vẽ đường. Không thay đổi hướng của rùa

 >>> tp = turtle.pos()
 >>> tp
 (0.00,0.00)
 >>> turtle.setpos(60,30)
 >>> turtle.pos()
 (60.00,30.00)
 >>> turtle.setpos((20,80))
 >>> turtle.pos()
 (20.00,80.00)
 >>> turtle.setpos(tp)
 >>> turtle.pos()
 (0.00,0.00)

rùa. setx(x) Tham số

x – một số (số nguyên hoặc số thực)

Đặt tọa độ đầu tiên của rùa thành x, giữ nguyên tọa độ thứ hai

>>> turtle.position()
(0.00,240.00)
>>> turtle.setx(10)
>>> turtle.position()
(10.00,240.00)

rùa. sety(y) Tham số

y – một số (số nguyên hoặc số thực)

Đặt tọa độ thứ hai của rùa thành y, giữ nguyên tọa độ đầu tiên

>>> turtle.position()
(0.00,40.00)
>>> turtle.sety(-10)
>>> turtle.position()
(0.00,-10.00)

rùa. tiêu đề(to_angle)rùa. seth(to_angle) Tham số

to_angle – một số (số nguyên hoặc số thực)

Đặt hướng của rùa thành to_angle. Dưới đây là một số hướng phổ biến theo độ

chế độ căn bản

chế độ logo

0 - đông

0 - bắc

90 - bắc

90 - đông

180 - tây

180 - nam

270 - nam

270 - tây

>>> turtle.setheading(90)
>>> turtle.heading()
90.0

rùa. nhà()

Di chuyển con rùa đến điểm gốc – tọa độ (0,0) – và đặt tiêu đề của nó thành hướng bắt đầu (tùy thuộc vào chế độ, xem phần )

>>> turtle.heading()
90.0
>>> turtle.position()
(0.00,-10.00)
>>> turtle.home()
>>> turtle.position()
(0.00,0.00)
>>> turtle.heading()
0.0

rùa. hình tròn(bán kính , phạm vi=None, steps=None)Parameters
  • bán kính - một số

  • phạm vi – một số (hoặc

    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.backward(30)
    >>> turtle.position()
    (-30.00,0.00)
    
    29)

  • các bước – một số nguyên (hoặc

    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.backward(30)
    >>> turtle.position()
    (-30.00,0.00)
    
    29)

Vẽ đường tròn có bán kính cho trước. Trung tâm là các đơn vị bán kính bên trái của con rùa; . Nếu phạm vi không được đưa ra, hãy vẽ toàn bộ vòng tròn. Nếu phạm vi không phải là một vòng tròn đầy đủ, một điểm cuối của vòng cung là vị trí bút hiện tại. Vẽ cung theo hướng ngược chiều kim đồng hồ nếu bán kính dương, ngược lại theo chiều kim đồng hồ. Cuối cùng, hướng của rùa được thay đổi theo mức độ

Vì hình tròn được xấp xỉ bằng một đa giác đều nội tiếp, các bước xác định số bước sẽ sử dụng. Nếu không được cung cấp, nó sẽ được tính toán tự động. Có thể được sử dụng để vẽ đa giác thông thường

>>> turtle.position()
(0.00,0.00)
>>> turtle.forward(25)
>>> turtle.position()
(25.00,0.00)
>>> turtle.forward(-75)
>>> turtle.position()
(-50.00,0.00)
0

rùa. chấm(kích thước=Không, *color)Parameters
  • size – một số nguyên >= 1 (nếu có)

  • màu – một chuỗi màu hoặc một bộ màu số

Vẽ một chấm tròn có kích thước đường kính, sử dụng màu. Nếu kích thước không được cung cấp, thì tối đa pensize+4 và 2*pensize được sử dụng

>>> turtle.position()
(0.00,0.00)
>>> turtle.forward(25)
>>> turtle.position()
(25.00,0.00)
>>> turtle.forward(-75)
>>> turtle.position()
(-50.00,0.00)
1

rùa. đóng dấu()

Đóng dấu một bản sao của hình con rùa lên canvas ở vị trí con rùa hiện tại. Trả lại một tem_id cho tem đó, có thể được sử dụng để xóa nó bằng cách gọi

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
36

>>> turtle.position()
(0.00,0.00)
>>> turtle.forward(25)
>>> turtle.position()
(25.00,0.00)
>>> turtle.forward(-75)
>>> turtle.position()
(-50.00,0.00)
2

rùa. dấu xóa(dấu) Tham số

temid - một số nguyên, phải là giá trị trả về của cuộc gọi trước đó

Xóa tem với tem đã cho

>>> turtle.position()
(0.00,0.00)
>>> turtle.forward(25)
>>> turtle.position()
(25.00,0.00)
>>> turtle.forward(-75)
>>> turtle.position()
(-50.00,0.00)
3

rùa. dấu xóa(n=Không)Parameters

n – một số nguyên (hoặc

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
29)

Xóa tất cả hoặc n tem đầu tiên/cuối cùng của rùa. Nếu n là

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
29, xóa tất cả tem, nếu n > 0 xóa n tem đầu tiên, ngược lại nếu n < 0 xóa n tem cuối cùng

>>> turtle.position()
(0.00,0.00)
>>> turtle.forward(25)
>>> turtle.position()
(25.00,0.00)
>>> turtle.forward(-75)
>>> turtle.position()
(-50.00,0.00)
4

rùa. hoàn tác()

Hoàn tác (nhiều lần) (các) hành động rùa cuối cùng. Số lượng hành động hoàn tác khả dụng được xác định bởi kích thước của bộ đệm hoàn tác

>>> turtle.position()
(0.00,0.00)
>>> turtle.forward(25)
>>> turtle.position()
(25.00,0.00)
>>> turtle.forward(-75)
>>> turtle.position()
(-50.00,0.00)
5

rùa. tốc độ(tốc độ=Không)Parameters

tốc độ – một số nguyên trong phạm vi 0. 10 hoặc dây tốc độ (xem bên dưới)

Đặt tốc độ của rùa thành một giá trị nguyên trong phạm vi 0. 10. Nếu không có đối số nào được đưa ra, hãy trả về tốc độ hiện tại

Nếu đầu vào là một số lớn hơn 10 hoặc nhỏ hơn 0. 5, tốc độ được đặt thành 0. Dây tốc độ được ánh xạ tới các giá trị tốc độ như sau

  • “nhanh nhất”. 0

  • "nhanh". 10

  • "Bình thường". 6

  • "chậm". 3

  • “chậm nhất”. 1

Tốc độ từ 1 đến 10 thực thi hoạt ảnh vẽ đường thẳng và xoay rùa ngày càng nhanh hơn

Chú ý. tốc độ = 0 có nghĩa là không có hoạt ảnh nào diễn ra. tiến/lùi khiến rùa nhảy và tương tự trái/phải khiến rùa quay ngay lập tức

>>> turtle.position()
(0.00,0.00)
>>> turtle.forward(25)
>>> turtle.position()
(25.00,0.00)
>>> turtle.forward(-75)
>>> turtle.position()
(-50.00,0.00)
6

Cho biết trạng thái của Rùa

rùa. vị trí()rùa. tư thế()

Trả về vị trí hiện tại của rùa (x,y) (dưới dạng vectơ)

>>> turtle.position()
(0.00,0.00)
>>> turtle.forward(25)
>>> turtle.position()
(25.00,0.00)
>>> turtle.forward(-75)
>>> turtle.position()
(-50.00,0.00)
7

rùa. về phía(x , y=None)Parameters
  • x – một số hoặc một cặp/vec-tơ số hoặc một cá thể rùa

  • y – một số nếu x là một số, ngược lại

    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.backward(30)
    >>> turtle.position()
    (-30.00,0.00)
    
    29

Trả lại góc giữa đường từ vị trí con rùa đến vị trí được chỉ định bởi (x,y), vectơ hoặc con rùa khác. Điều này phụ thuộc vào hướng bắt đầu của rùa, tùy thuộc vào chế độ - “tiêu chuẩn”/”thế giới” hoặc “logo”

>>> turtle.position()
(0.00,0.00)
>>> turtle.forward(25)
>>> turtle.position()
(25.00,0.00)
>>> turtle.forward(-75)
>>> turtle.position()
(-50.00,0.00)
8

rùa. xcor()

Trả về tọa độ x của con rùa

>>> turtle.position()
(0.00,0.00)
>>> turtle.forward(25)
>>> turtle.position()
(25.00,0.00)
>>> turtle.forward(-75)
>>> turtle.position()
(-50.00,0.00)
9

rùa. ycor()

Trả về tọa độ y của rùa

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
0

rùa. tiêu đề()

Trả về tiêu đề hiện tại của rùa (giá trị tùy thuộc vào chế độ rùa, xem phần )

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
1

rùa. khoảng cách(x , y=None)Parameters
  • x – một số hoặc một cặp/vec-tơ số hoặc một cá thể rùa

  • y – một số nếu x là một số, ngược lại

    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.backward(30)
    >>> turtle.position()
    (-30.00,0.00)
    
    29

Trả về khoảng cách từ con rùa đến (x,y), vectơ đã cho hoặc con rùa đã cho khác, theo đơn vị bước rùa

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
2

Cài đặt để đo lường

rùa. độ(vòng tròn=360. 0) Tham số

hình tròn – một số

Đặt đơn vị đo góc i. e. đặt số "độ" cho một vòng tròn đầy đủ. Giá trị mặc định là 360 độ

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
3

rùa. radian()

Đặt đơn vị đo góc thành radian. Tương đương với

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
44

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
4

điều khiển bút

Trạng thái bản vẽ

rùa. xuống dốc()rùa. pd()rùa. xuống()

Kéo bút xuống – vẽ khi di chuyển

rùa. bút chì()rùa. pu()rùa. lên()

Kéo bút lên – không vẽ khi di chuyển

rùa. pensize(chiều rộng=Không có . )turtle.chiều rộng(chiều rộng=Không có)Parameters

chiều rộng – một số dương

Đặt độ dày của đường thành chiều rộng hoặc trả lại. Nếu chế độ thay đổi kích thước được đặt thành “tự động” và hình con rùa là một đa giác, thì đa giác đó được vẽ với cùng độ dày nét vẽ. Nếu không có đối số nào được đưa ra, thì pensize hiện tại được trả về

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
5

rùa. bút(bút=Không có, **pendict)Parameters
  • bút – một từ điển với một số hoặc tất cả các phím được liệt kê bên dưới

  • pendict – một hoặc nhiều đối số từ khóa với các khóa được liệt kê bên dưới làm từ khóa

Trả lại hoặc đặt các thuộc tính của bút trong “từ điển bút” với các cặp khóa/giá trị sau

  • "cho xem". Đúng sai

  • "đặt bút xuống". Đúng sai

  • “màu bút”. chuỗi màu hoặc bộ màu

  • "tô màu". chuỗi màu hoặc bộ màu

  • “bù”. số dương

  • "tốc độ". số trong phạm vi 0. 10

  • "chế độ thay đổi kích thước". “tự động” hoặc “người dùng” hoặc “noresize”

  • "máy kéo dài". (số dương, số dương)

  • "đề cương". số dương

  • “nghiêng”. con số

Từ điển này có thể được sử dụng làm đối số cho lệnh gọi tiếp theo để khôi phục trạng thái bút cũ. Ngoài ra, một hoặc nhiều thuộc tính này có thể được cung cấp dưới dạng đối số từ khóa. Điều này có thể được sử dụng để đặt một số thuộc tính bút trong một câu lệnh

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
6

rùa. ngừng hoạt động()

Trả lại

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
46 nếu bút xuống,
>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
47 nếu bút lên

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
7

Kiểm soát màu sắc

rùa. bút màu(*args)

Trả lại hoặc đặt màu bút

Bốn định dạng đầu vào được cho phép

>>> turtle.position()
(0.00,0.00)
>>> turtle.forward(25)
>>> turtle.position()
(25.00,0.00)
>>> turtle.forward(-75)
>>> turtle.position()
(-50.00,0.00)
54

Trả về pencolor hiện tại dưới dạng chuỗi đặc tả màu hoặc dưới dạng bộ (xem ví dụ). Có thể được sử dụng làm đầu vào cho lệnh gọi color/pencolor/fillcolor khác

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
49

Đặt pencolor thành chuỗi màu, là chuỗi đặc tả màu Tk, chẳng hạn như

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
50,
>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
51 hoặc
>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
52

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
53

Đặt pencolor thành màu RGB được biểu thị bằng bộ của r, g và b. Mỗi r, g và b phải nằm trong phạm vi 0. colormode, trong đó colormode là 1. 0 hoặc 255 (xem )

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
55

Đặt pencolor thành màu RGB được biểu thị bằng r, g và b. Mỗi r, g và b phải nằm trong phạm vi 0. chế độ màu

Nếu hình con rùa là một đa giác, thì đường viền của đa giác đó được vẽ bằng bút màu mới được đặt

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
8

rùa. màu tô(*args)

Trả lại hoặc đặt màu tô

Bốn định dạng đầu vào được cho phép

>>> turtle.position()
(0.00,0.00)
>>> turtle.forward(25)
>>> turtle.position()
(25.00,0.00)
>>> turtle.forward(-75)
>>> turtle.position()
(-50.00,0.00)
55

Trả về màu tô hiện tại dưới dạng chuỗi đặc tả màu, có thể ở định dạng tuple (xem ví dụ). Có thể được sử dụng làm đầu vào cho lệnh gọi color/pencolor/fillcolor khác

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
57

Đặt màu tô thành chuỗi màu, là chuỗi đặc tả màu Tk, chẳng hạn như

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
50,
>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
51 hoặc
>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
52

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
61

Đặt màu tô thành màu RGB được biểu thị bằng bộ của r, g và b. Mỗi r, g và b phải nằm trong phạm vi 0. colormode, trong đó colormode là 1. 0 hoặc 255 (xem )

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
63

Đặt màu tô thành màu RGB được biểu thị bằng r, g và b. Mỗi r, g và b phải nằm trong phạm vi 0. chế độ màu

Nếu hình con rùa là một đa giác, phần bên trong của đa giác đó được vẽ bằng màu tô mới được đặt

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
9

rùa. màu(*args)

Trả lại hoặc đặt pencolor và fillcolor

Một số định dạng đầu vào được cho phép. Họ sử dụng 0 đến 3 đối số như sau

>>> turtle.position()
(0.00,0.00)
>>> turtle.forward(25)
>>> turtle.position()
(25.00,0.00)
>>> turtle.forward(-75)
>>> turtle.position()
(-50.00,0.00)
53

Trả về màu bút hiện tại và màu tô hiện tại dưới dạng một cặp chuỗi hoặc bộ đặc tả màu khi được trả về bởi và

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
67,
>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
68,
>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
69

Các đầu vào như trong , đặt cả hai, màu tô và màu bút, thành giá trị đã cho

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
71,
>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
72

Tương đương với

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
73 và
>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
74 và tương tự nếu định dạng đầu vào khác được sử dụng

Nếu hình con rùa là một đa giác, đường viền và phần bên trong của đa giác đó được vẽ bằng các màu mới được đặt

>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
0

Xem thêm. phương pháp màn hình

đổ đầy

rùa. điền()

Trả về trạng thái điền (

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
46 nếu điền,
>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
47 khác)

>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
1

rùa. begin_fill()

Được gọi ngay trước khi vẽ một hình cần tô

rùa. end_fill()

Tô màu cho hình được vẽ sau lần gọi cuối cùng đến

Việc các vùng chồng lấp cho đa giác tự giao nhau hoặc nhiều hình có được lấp đầy hay không tùy thuộc vào đồ họa của hệ điều hành, loại chồng lấp và số lượng chồng lấp. Ví dụ: ngôi sao Rùa ở trên có thể toàn màu vàng hoặc có một số vùng màu trắng

>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
2

Kiểm soát bản vẽ nhiều hơn

rùa. đặt lại()

Xóa hình vẽ của rùa khỏi màn hình, căn giữa lại rùa và đặt các biến về giá trị mặc định

>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
3

rùa. xóa()

Xóa hình vẽ của rùa khỏi màn hình. Đừng di chuyển rùa. Trạng thái và vị trí của rùa cũng như hình vẽ của các loài rùa khác không bị ảnh hưởng

rùa. viết(arg , di chuyển=False, align='left', font=('Arial', 8, 'normal'))Parameters
  • arg – đối tượng được ghi vào TurtleScreen

  • di chuyển – Đúng/Sai

  • căn chỉnh – một trong các chuỗi “trái”, “trung tâm” hoặc phải”

  • phông chữ – một bộ ba (tên phông chữ, cỡ chữ, loại phông chữ)

Viết văn bản - biểu diễn chuỗi của arg - tại vị trí con rùa hiện tại theo căn chỉnh (“trái”, “giữa” hoặc “phải”) và với phông chữ đã cho. Nếu di chuyển là đúng, bút sẽ được di chuyển đến góc dưới cùng bên phải của văn bản. Theo mặc định, di chuyển là

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
47

>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
4

bang con rùa

Hiển thị

rùa. con rùa ẩn nấp()con rùa. ht()

Làm cho con rùa vô hình. Bạn nên làm điều này khi đang thực hiện một số bản vẽ phức tạp, bởi vì việc ẩn con rùa sẽ tăng tốc độ vẽ một cách đáng kể

>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
5

rùa. rùa chiếu()rùa. st()

Làm cho con rùa có thể nhìn thấy

>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
6

rùa. hiển thị()

Quay trở lại

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
46 nếu Rùa được hiển thị,
>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
47 nếu nó bị ẩn

>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
7

Vẻ bề ngoài

rùa. hình dạng(tên=Không có)Parameters

tên - một chuỗi là một shapename hợp lệ

Đặt hình dạng con rùa thành hình dạng với tên đã cho hoặc, nếu tên không được đặt, hãy trả về tên của hình dạng hiện tại. Hình dạng có tên phải tồn tại trong từ điển hình dạng của TurtleScreen. Ban đầu có các hình đa giác sau. “mũi tên”, “con rùa”, “hình tròn”, “hình vuông”, “hình tam giác”, “cổ điển”. Để tìm hiểu về cách xử lý các hình, hãy xem Phương pháp màn hình

>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
8

rùa. resizemode(rmode=Không có)Parameters

rmode – một trong các chuỗi “auto”, “user”, “noresize”

Đặt resizemode thành một trong các giá trị. “tự động”, “người dùng”, “noresize”. Nếu rmode không được cung cấp, hãy trả về resizemode hiện tại. Các chế độ thay đổi kích thước khác nhau có các hiệu ứng sau

  • "tự động". điều chỉnh sự xuất hiện của con rùa tương ứng với giá trị của pensize

  • "người dùng". điều chỉnh sự xuất hiện của con rùa theo các giá trị của hệ số kéo dài và chiều rộng đường viền (đường viền), được đặt bởi

  • “noresize”. không có sự thích nghi nào về ngoại hình của con rùa diễn ra

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
84 được gọi bởi khi được sử dụng với các đối số

>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
9

rùa. kích thước hình dạng(stretch_wid=Không có . , stretch_len=None, outline=None)turtle.kích thước rùa(stretch_wid=Không có, stretch_len=None, outline=None)Parameters
  • stretch_wid – số dương

  • stretch_len – số dương

  • đại cương – số dương

Trả lại hoặc đặt các thuộc tính x/y-stretchfactors và/hoặc phác thảo của bút. Đặt resizemode thành “người dùng”. Khi và chỉ khi chế độ thay đổi kích thước được đặt thành "người dùng", con rùa sẽ được hiển thị kéo dài theo các yếu tố kéo dài của nó. stretch_wid là hệ số giãn vuông góc với hướng của nó, stretch_len là hệ số giãn theo hướng của hướng của nó, đường viền xác định chiều rộng của đường viền của hình dạng

>>> turtle.heading()
22.0
>>> turtle.left(45)
>>> turtle.heading()
67.0
0

rùa. máy cắt(máy cắt=Không có)Parameters

cắt - số (tùy chọn)

Đặt hoặc trả lại shearfactor hiện tại. Cắt hình rùa theo lực cắt shearfactor đã cho, là tiếp tuyến của góc cắt. Không thay đổi tiêu đề của rùa (hướng di chuyển). Nếu cắt không được đưa ra. trả lại shearfactor hiện tại, tôi. e. tiếp tuyến của góc cắt, theo đó các đường song song với tiêu đề của con rùa bị cắt

>>> turtle.heading()
22.0
>>> turtle.left(45)
>>> turtle.heading()
67.0
1

rùa. độ nghiêng(góc) Tham số

góc – một số

Xoay hình con rùa theo góc so với góc nghiêng hiện tại của nó, nhưng không thay đổi hướng của con rùa (hướng di chuyển)

>>> turtle.heading()
22.0
>>> turtle.left(45)
>>> turtle.heading()
67.0
2

rùa. settiltangle(góc) Tham số

góc – một số

Xoay hình con rùa để chỉ theo hướng được chỉ định theo góc, bất kể góc nghiêng hiện tại của nó là gì. Không thay đổi tiêu đề của rùa (hướng di chuyển)

>>> turtle.heading()
22.0
>>> turtle.left(45)
>>> turtle.heading()
67.0
3

Không dùng nữa kể từ phiên bản 3. 1

rùa. nghiêng(góc=Không có)Parameters

góc – một số (tùy chọn)

Đặt hoặc trả về góc nghiêng hiện tại. Nếu góc được đưa ra, hãy xoay hình con rùa để chỉ theo hướng được chỉ định bởi góc, bất kể góc nghiêng hiện tại của nó là gì. Không thay đổi tiêu đề của rùa (hướng di chuyển). Nếu góc không được đưa ra. trả lại góc nghiêng hiện tại, tôi. e. góc giữa hướng của hình con rùa và hướng của con rùa (hướng di chuyển của nó)

>>> turtle.heading()
22.0
>>> turtle.left(45)
>>> turtle.heading()
67.0
4

rùa. shapetransform(t11=Không, t12=None, t21=None, t22=None)Parameters
  • t11 – một số (tùy chọn)

  • t12 – một số (tùy chọn)

  • t21 – một số (tùy chọn)

  • t12 – một số (tùy chọn)

Đặt hoặc trả về ma trận biến đổi hiện tại của hình con rùa

If none of the matrix elements are given, return the transformation matrix as a tuple of 4 elements. Mặt khác, đặt các phần tử đã cho và biến đổi hình con rùa theo ma trận bao gồm hàng đầu tiên t11, t12 và hàng thứ hai t21, t22. Định thức t11 * t22 - t12 * t21 không được bằng 0, nếu không sẽ xảy ra lỗi. Modify stretchfactor, shearfactor and tiltangle according to the given matrix

>>> turtle.heading()
22.0
>>> turtle.left(45)
>>> turtle.heading()
67.0
5

rùa. get_shapepoly()

Return the current shape polygon as tuple of coordinate pairs. This can be used to define a new shape or components of a compound shape

>>> turtle.heading()
22.0
>>> turtle.left(45)
>>> turtle.heading()
67.0
6

Sử dụng các sự kiện

turtle. onclick(fun , btn=1 , add=None) Parameters
  • fun – a function with two arguments which will be called with the coordinates of the clicked point on the canvas

  • btn – number of the mouse-button, defaults to 1 (left mouse button)

  • add –

    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.backward(30)
    >>> turtle.position()
    (-30.00,0.00)
    
    46 or
    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.backward(30)
    >>> turtle.position()
    (-30.00,0.00)
    
    47 – if
    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.backward(30)
    >>> turtle.position()
    (-30.00,0.00)
    
    46, a new binding will be added, otherwise it will replace a former binding

Bind fun to mouse-click events on this turtle. If fun is

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
29, existing bindings are removed. Example for the anonymous turtle, i. e. the procedural way

>>> turtle.heading()
22.0
>>> turtle.left(45)
>>> turtle.heading()
67.0
7

turtle. onrelease(fun , btn=1 , add=None) Parameters
  • fun – a function with two arguments which will be called with the coordinates of the clicked point on the canvas

  • btn – number of the mouse-button, defaults to 1 (left mouse button)

  • add –

    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.backward(30)
    >>> turtle.position()
    (-30.00,0.00)
    
    46 or
    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.backward(30)
    >>> turtle.position()
    (-30.00,0.00)
    
    47 – if
    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.backward(30)
    >>> turtle.position()
    (-30.00,0.00)
    
    46, a new binding will be added, otherwise it will replace a former binding

Bind fun to mouse-button-release events on this turtle. If fun is

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
29, existing bindings are removed

>>> turtle.heading()
22.0
>>> turtle.left(45)
>>> turtle.heading()
67.0
8

turtle. ondrag(fun , btn=1 , add=None) Parameters
  • fun – a function with two arguments which will be called with the coordinates of the clicked point on the canvas

  • btn – number of the mouse-button, defaults to 1 (left mouse button)

  • add –

    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.backward(30)
    >>> turtle.position()
    (-30.00,0.00)
    
    46 or
    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.backward(30)
    >>> turtle.position()
    (-30.00,0.00)
    
    47 – if
    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.backward(30)
    >>> turtle.position()
    (-30.00,0.00)
    
    46, a new binding will be added, otherwise it will replace a former binding

Bind fun to mouse-move events on this turtle. If fun is

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
29, existing bindings are removed

Remark. Every sequence of mouse-move-events on a turtle is preceded by a mouse-click event on that turtle

>>> turtle.heading()
22.0
>>> turtle.left(45)
>>> turtle.heading()
67.0
9

Subsequently, clicking and dragging the Turtle will move it across the screen thereby producing handdrawings (if pen is down)

Phương pháp rùa đặc biệt

turtle. begin_poly()

Start recording the vertices of a polygon. Current turtle position is first vertex of polygon

turtle. end_poly()

Stop recording the vertices of a polygon. Current turtle position is last vertex of polygon. This will be connected with the first vertex

turtle. get_poly()

Return the last recorded polygon

 >>> tp = turtle.pos()
 >>> tp
 (0.00,0.00)
 >>> turtle.setpos(60,30)
 >>> turtle.pos()
 (60.00,30.00)
 >>> turtle.setpos((20,80))
 >>> turtle.pos()
 (20.00,80.00)
 >>> turtle.setpos(tp)
 >>> turtle.pos()
 (0.00,0.00)
0

turtle. clone()

Create and return a clone of the turtle with same position, heading and turtle properties

 >>> tp = turtle.pos()
 >>> tp
 (0.00,0.00)
 >>> turtle.setpos(60,30)
 >>> turtle.pos()
 (60.00,30.00)
 >>> turtle.setpos((20,80))
 >>> turtle.pos()
 (20.00,80.00)
 >>> turtle.setpos(tp)
 >>> turtle.pos()
 (0.00,0.00)
1

turtle. getturtle()turtle. getpen()

Return the Turtle object itself. Only reasonable use. as a function to return the “anonymous turtle”

 >>> tp = turtle.pos()
 >>> tp
 (0.00,0.00)
 >>> turtle.setpos(60,30)
 >>> turtle.pos()
 (60.00,30.00)
 >>> turtle.setpos((20,80))
 >>> turtle.pos()
 (20.00,80.00)
 >>> turtle.setpos(tp)
 >>> turtle.pos()
 (0.00,0.00)
2

turtle. getscreen()

Return the object the turtle is drawing on. TurtleScreen methods can then be called for that object

 >>> tp = turtle.pos()
 >>> tp
 (0.00,0.00)
 >>> turtle.setpos(60,30)
 >>> turtle.pos()
 (60.00,30.00)
 >>> turtle.setpos((20,80))
 >>> turtle.pos()
 (20.00,80.00)
 >>> turtle.setpos(tp)
 >>> turtle.pos()
 (0.00,0.00)
3

turtle. setundobuffer(size) Parameters

size – an integer or

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
29

Set or disable undobuffer. If size is an integer, an empty undobuffer of given size is installed. size gives the maximum number of turtle actions that can be undone by the method/function. If size is

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
29, the undobuffer is disabled

 >>> tp = turtle.pos()
 >>> tp
 (0.00,0.00)
 >>> turtle.setpos(60,30)
 >>> turtle.pos()
 (60.00,30.00)
 >>> turtle.setpos((20,80))
 >>> turtle.pos()
 (20.00,80.00)
 >>> turtle.setpos(tp)
 >>> turtle.pos()
 (0.00,0.00)
4

turtle. undobufferentries()

Return number of entries in the undobuffer

 >>> tp = turtle.pos()
 >>> tp
 (0.00,0.00)
 >>> turtle.setpos(60,30)
 >>> turtle.pos()
 (60.00,30.00)
 >>> turtle.setpos((20,80))
 >>> turtle.pos()
 (20.00,80.00)
 >>> turtle.setpos(tp)
 >>> turtle.pos()
 (0.00,0.00)
5

Compound shapes

To use compound turtle shapes, which consist of several polygons of different color, you must use the helper class explicitly as described below

  1. Create an empty Shape object of type “compound”

  2. Add as many components to this object as desired, using the

    >>> turtle.heading()
    22.0
    >>> turtle.right(45)
    >>> turtle.heading()
    337.0
    
    03 method

    For example

     >>> tp = turtle.pos()
     >>> tp
     (0.00,0.00)
     >>> turtle.setpos(60,30)
     >>> turtle.pos()
     (60.00,30.00)
     >>> turtle.setpos((20,80))
     >>> turtle.pos()
     (20.00,80.00)
     >>> turtle.setpos(tp)
     >>> turtle.pos()
     (0.00,0.00)
    
    6

  3. Now add the Shape to the Screen’s shapelist and use it

     >>> tp = turtle.pos()
     >>> tp
     (0.00,0.00)
     >>> turtle.setpos(60,30)
     >>> turtle.pos()
     (60.00,30.00)
     >>> turtle.setpos((20,80))
     >>> turtle.pos()
     (20.00,80.00)
     >>> turtle.setpos(tp)
     >>> turtle.pos()
     (0.00,0.00)
    
    7

Ghi chú

The class is used internally by the method in different ways. Người lập trình ứng dụng chỉ phải xử lý lớp Hình dạng khi sử dụng các hình dạng phức hợp như được hiển thị ở trên

Methods of TurtleScreen/Screen and corresponding functions

Most of the examples in this section refer to a TurtleScreen instance called

>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
06

Kiểm soát cửa sổ

turtle. bgcolor(*args) Parameters

args – a color string or three numbers in the range 0. colormode or a 3-tuple of such numbers

Set or return background color of the TurtleScreen

 >>> tp = turtle.pos()
 >>> tp
 (0.00,0.00)
 >>> turtle.setpos(60,30)
 >>> turtle.pos()
 (60.00,30.00)
 >>> turtle.setpos((20,80))
 >>> turtle.pos()
 (20.00,80.00)
 >>> turtle.setpos(tp)
 >>> turtle.pos()
 (0.00,0.00)
8

turtle. bgpic(picname=None) Parameters

picname – a string, name of a gif-file or

>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
07, or
>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
29

Set background image or return name of current backgroundimage. If picname is a filename, set the corresponding image as background. If picname is

>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
07, delete background image, if present. If picname is
>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
29, return the filename of the current backgroundimage

 >>> tp = turtle.pos()
 >>> tp
 (0.00,0.00)
 >>> turtle.setpos(60,30)
 >>> turtle.pos()
 (60.00,30.00)
 >>> turtle.setpos((20,80))
 >>> turtle.pos()
 (20.00,80.00)
 >>> turtle.setpos(tp)
 >>> turtle.pos()
 (0.00,0.00)
9

rùa. xóa()

Ghi chú

This TurtleScreen method is available as a global function only under the name

>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
11. The global function
>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
12 is a different one derived from the Turtle method
>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
12

turtle. clearscreen()

Delete all drawings and all turtles from the TurtleScreen. Đặt lại TurtleScreen hiện đang trống về trạng thái ban đầu. nền trắng, không có hình nền, không có ràng buộc sự kiện và theo dõi trên

rùa. đặt lại()

Ghi chú

Phương thức TurtleScreen này chỉ khả dụng dưới dạng hàm toàn cầu dưới tên

>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
14. Hàm toàn cục
>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
15 là một hàm khác bắt nguồn từ phương thức Rùa
>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
15

rùa. đặt lại màn hình()

Đặt lại tất cả Rùa trên Màn hình về trạng thái ban đầu

rùa. kích thước màn hình(canvwidth=Không có, canvheight=None, bg=None)Parameters
  • canvwidth – số nguyên dương, chiều rộng mới của canvas tính bằng pixel

  • canvheight – số nguyên dương, chiều cao mới của canvas tính bằng pixel

  • bg – chuỗi màu hoặc bộ màu, màu nền mới

If no arguments are given, return current (canvaswidth, canvasheight). Else resize the canvas the turtles are drawing on. Do not alter the drawing window. To observe hidden parts of the canvas, use the scrollbars. With this method, one can make visible those parts of a drawing which were outside the canvas before

>>> turtle.position()
(0.00,240.00)
>>> turtle.setx(10)
>>> turtle.position()
(10.00,240.00)
0

e. g. to search for an erroneously escaped turtle ;-)

turtle. setworldcoordinates(llx , lly , urx , ury) Parameters
  • llx – a number, x-coordinate of lower left corner of canvas

  • lly – a number, y-coordinate of lower left corner of canvas

  • urx – a number, x-coordinate of upper right corner of canvas

  • ury – a number, y-coordinate of upper right corner of canvas

Set up user-defined coordinate system and switch to mode “world” if necessary. This performs a

>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
17. If mode “world” is already active, all drawings are redrawn according to the new coordinates

ATTENTION. in user-defined coordinate systems angles may appear distorted

>>> turtle.position()
(0.00,240.00)
>>> turtle.setx(10)
>>> turtle.position()
(10.00,240.00)
1

kiểm soát hoạt hình

turtle. độ trễ(độ trễ=Không có)Parameters

delay – positive integer

Set or return the drawing delay in milliseconds. (This is approximately the time interval between two consecutive canvas updates. ) The longer the drawing delay, the slower the animation

Optional argument

>>> turtle.position()
(0.00,240.00)
>>> turtle.setx(10)
>>> turtle.position()
(10.00,240.00)
2

turtle. tracer(n=None , delay=None) Parameters
  • n – nonnegative integer

  • delay – nonnegative integer

Turn turtle animation on/off and set delay for update drawings. If n is given, only each n-th regular screen update is really performed. (Can be used to accelerate the drawing of complex graphics. ) When called without arguments, returns the currently stored value of n. Second argument sets delay value (see )

>>> turtle.position()
(0.00,240.00)
>>> turtle.setx(10)
>>> turtle.position()
(10.00,240.00)
3

turtle. update()

Perform a TurtleScreen update. To be used when tracer is turned off

See also the RawTurtle/Turtle method

Sử dụng các sự kiện màn hình

turtle. listen(xdummy=None , ydummy=None)

Set focus on TurtleScreen (in order to collect key-events). Dummy arguments are provided in order to be able to pass to the onclick method

turtle. onkey(fun , key)turtle. onkeyrelease(fun , key) Parameters
  • fun – a function with no arguments or

    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.backward(30)
    >>> turtle.position()
    (-30.00,0.00)
    
    29

  • key – a string. key (e. g. “a”) or key-symbol (e. g. “space”)

Bind fun to key-release event of key. If fun is

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
29, event bindings are removed. Remark. in order to be able to register key-events, TurtleScreen must have the focus. (See method . )

>>> turtle.position()
(0.00,240.00)
>>> turtle.setx(10)
>>> turtle.position()
(10.00,240.00)
4

turtle. onkeypress(fun , key=None) Parameters
  • fun – a function with no arguments or

    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.backward(30)
    >>> turtle.position()
    (-30.00,0.00)
    
    29

  • key – a string. key (e. g. “a”) or key-symbol (e. g. “space”)

Bind fun to key-press event of key if key is given, or to any key-press-event if no key is given. Remark. in order to be able to register key-events, TurtleScreen must have focus. (See method . )

>>> turtle.position()
(0.00,240.00)
>>> turtle.setx(10)
>>> turtle.position()
(10.00,240.00)
5

turtle. onclick(fun , btn=1 , add=None)turtle. onscreenclick(fun , btn=1 , add=None) Parameters
  • fun – a function with two arguments which will be called with the coordinates of the clicked point on the canvas

  • btn – number of the mouse-button, defaults to 1 (left mouse button)

  • add –

    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.backward(30)
    >>> turtle.position()
    (-30.00,0.00)
    
    46 or
    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.backward(30)
    >>> turtle.position()
    (-30.00,0.00)
    
    47 – if
    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.backward(30)
    >>> turtle.position()
    (-30.00,0.00)
    
    46, a new binding will be added, otherwise it will replace a former binding

Bind fun to mouse-click events on this screen. If fun is

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
29, existing bindings are removed

Example for a TurtleScreen instance named

>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
06 and a Turtle instance named
>>> turtle.setheading(90)
>>> turtle.heading()
90.0
8

>>> turtle.position()
(0.00,240.00)
>>> turtle.setx(10)
>>> turtle.position()
(10.00,240.00)
6

Ghi chú

This TurtleScreen method is available as a global function only under the name

>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
32. The global function
>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
33 is another one derived from the Turtle method
>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
33

rùa. ontimer(fun , t=0) Parameters
  • fun – a function with no arguments

  • t – a number >= 0

Install a timer that calls fun after t milliseconds

>>> turtle.position()
(0.00,240.00)
>>> turtle.setx(10)
>>> turtle.position()
(10.00,240.00)
7

turtle. mainloop()turtle. done()

Starts event loop - calling Tkinter’s mainloop function. Must be the last statement in a turtle graphics program. Must not be used if a script is run from within IDLE in -n mode (No subprocess) - for interactive use of turtle graphics

>>> turtle.position()
(0.00,240.00)
>>> turtle.setx(10)
>>> turtle.position()
(10.00,240.00)
8

Phương thức nhập liệu

turtle. textinput(title , prompt) Parameters
  • title – string

  • prompt – string

Bật lên một cửa sổ hộp thoại để nhập chuỗi. Parameter title is the title of the dialog window, prompt is a text mostly describing what information to input. Return the string input. If the dialog is canceled, return

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
29

>>> turtle.position()
(0.00,240.00)
>>> turtle.setx(10)
>>> turtle.position()
(10.00,240.00)
9

rùa. numinput(title , prompt , default=None , minval=None , maxval=None) Parameters
  • title – string

  • prompt – string

  • default – number (optional)

  • minval – number (optional)

  • maxval – number (optional)

Pop up a dialog window for input of a number. title is the title of the dialog window, prompt is a text mostly describing what numerical information to input. default. default value, minval. minimum value for input, maxval. maximum value for input. The number input must be in the range minval . maxval if these are given. If not, a hint is issued and the dialog remains open for correction. Return the number input. If the dialog is canceled, return

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
29

>>> turtle.position()
(0.00,40.00)
>>> turtle.sety(-10)
>>> turtle.position()
(0.00,-10.00)
0

Cài đặt và phương pháp đặc biệt

turtle. mode(mode=None) Parameters

mode – one of the strings “standard”, “logo” or “world”

Set turtle mode (“standard”, “logo” or “world”) and perform reset. If mode is not given, current mode is returned

Mode “standard” is compatible with old . Mode “logo” is compatible with most Logo turtle graphics. Chế độ “thế giới” sử dụng “tọa độ thế giới” do người dùng xác định. Attention. in this mode angles appear distorted if

>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
38 unit-ratio doesn’t equal 1

Mode

Initial turtle heading

positive angles

“standard”

to the right (east)

counterclockwise

“logo”

upward (north)

clockwise

>>> turtle.position()
(0.00,40.00)
>>> turtle.sety(-10)
>>> turtle.position()
(0.00,-10.00)
1

turtle. colormode(cmode=None) Parameters

cmode – one of the values 1. 0 or 255

Return the colormode or set it to 1. 0 or 255. Subsequently r, g, b values of color triples have to be in the range 0. *cmode*

>>> turtle.position()
(0.00,40.00)
>>> turtle.sety(-10)
>>> turtle.position()
(0.00,-10.00)
2

turtle. getcanvas()

Return the Canvas of this TurtleScreen. Useful for insiders who know what to do with a Tkinter Canvas

>>> turtle.position()
(0.00,40.00)
>>> turtle.sety(-10)
>>> turtle.position()
(0.00,-10.00)
3

turtle. getshapes()

Return a list of names of all currently available turtle shapes

>>> turtle.position()
(0.00,40.00)
>>> turtle.sety(-10)
>>> turtle.position()
(0.00,-10.00)
4

turtle. register_shape(name , shape=None)turtle. addshape(name , shape=None)

There are three different ways to call this function

  1. name is the name of a gif-file and shape is

    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.backward(30)
    >>> turtle.position()
    (-30.00,0.00)
    
    29. Install the corresponding image shape

    >>> turtle.position()
    (0.00,40.00)
    >>> turtle.sety(-10)
    >>> turtle.position()
    (0.00,-10.00)
    
    5

    Ghi chú

    Image shapes do not rotate when turning the turtle, so they do not display the heading of the turtle

  2. name is an arbitrary string and shape is a tuple of pairs of coordinates. Install the corresponding polygon shape

    >>> turtle.position()
    (0.00,40.00)
    >>> turtle.sety(-10)
    >>> turtle.position()
    (0.00,-10.00)
    
    6

  3. name is an arbitrary string and shape is a (compound) object. Install the corresponding compound shape

Add a turtle shape to TurtleScreen’s shapelist. Only thusly registered shapes can be used by issuing the command

>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
41

turtle. turtles()

Return the list of turtles on the screen

>>> turtle.position()
(0.00,40.00)
>>> turtle.sety(-10)
>>> turtle.position()
(0.00,-10.00)
7

turtle. window_height()

Return the height of the turtle window

>>> turtle.position()
(0.00,40.00)
>>> turtle.sety(-10)
>>> turtle.position()
(0.00,-10.00)
8

turtle. window_width()

Trả về chiều rộng của cửa sổ con rùa

>>> turtle.position()
(0.00,40.00)
>>> turtle.sety(-10)
>>> turtle.position()
(0.00,-10.00)
9

Methods specific to Screen, not inherited from TurtleScreen

turtle. bye()

Shut the turtlegraphics window

turtle. exitonclick()

Bind

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
18 method to mouse clicks on the Screen

If the value “using_IDLE” in the configuration dictionary is

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
47 (default value), also enter mainloop. Remark. If IDLE with the
>>> turtle.heading()
90.0
>>> turtle.position()
(0.00,-10.00)
>>> turtle.home()
>>> turtle.position()
(0.00,0.00)
>>> turtle.heading()
0.0
3 switch (no subprocess) is used, this value should be set to
>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
46 in
>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
46. In this case IDLE’s own mainloop is active also for the client script

turtle. setup(width=_CFG['width'] , height=_CFG['height'] , startx=_CFG['leftright'] , starty=_CFG['topbottom'])

Set the size and position of the main window. Default values of arguments are stored in the configuration dictionary and can be changed via a

>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
46 file

Parameters
  • width – if an integer, a size in pixels, if a float, a fraction of the screen; default is 50% of screen

  • height – if an integer, the height in pixels, if a float, a fraction of the screen; default is 75% of screen

  • startx – if positive, starting position in pixels from the left edge of the screen, if negative from the right edge, if

    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.backward(30)
    >>> turtle.position()
    (-30.00,0.00)
    
    29, center window horizontally

  • starty – if positive, starting position in pixels from the top edge of the screen, if negative from the bottom edge, if

    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.backward(30)
    >>> turtle.position()
    (-30.00,0.00)
    
    29, center window vertically

>>> turtle.setheading(90)
>>> turtle.heading()
90.0
0

turtle. title(titlestring) Parameters

titlestring – a string that is shown in the titlebar of the turtle graphics window

Set title of turtle window to titlestring

>>> turtle.setheading(90)
>>> turtle.heading()
90.0
1

lớp học công cộng

lớp rùa. RawTurtle(canvas)class turtle. RawPen(canvas) Parameters

canvas – a

>>> turtle.heading()
90.0
>>> turtle.position()
(0.00,-10.00)
>>> turtle.home()
>>> turtle.position()
(0.00,0.00)
>>> turtle.heading()
0.0
6, a or a

Create a turtle. The turtle has all methods described above as “methods of Turtle/RawTurtle”

class turtle. Turtle

Subclass of RawTurtle, has the same interface but draws on a default object created automatically when needed for the first time

class turtle. TurtleScreen(cv) Parameters

cv – a

>>> turtle.heading()
90.0
>>> turtle.position()
(0.00,-10.00)
>>> turtle.home()
>>> turtle.position()
(0.00,0.00)
>>> turtle.heading()
0.0
6

Provides screen oriented methods like

>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
55 etc. that are described above

class turtle. Screen

Subclass of TurtleScreen, with

class turtle. ScrolledCanvas(master) Parameters

master – some Tkinter widget to contain the ScrolledCanvas, i. e. a Tkinter-canvas with scrollbars added

Used by class Screen, which thus automatically provides a ScrolledCanvas as playground for the turtles

class turtle. Shape(type_ , data) Parameters

type_ – one of the strings “polygon”, “image”, “compound”

Hình dạng mô hình cấu trúc dữ liệu. The pair

>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
56 must follow this specification

type_

data

“polygon”

a polygon-tuple, i. e. a tuple of pairs of coordinates

“image”

an image (in this form only used internally. )

“compound”

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
29 (a compound shape has to be constructed using the method)

addcomponent(poly , fill , outline=None) Parameters
  • poly – a polygon, i. e. a tuple of pairs of numbers

  • fill – a color the poly will be filled with

  • outline – a color for the poly’s outline (if given)

Example

>>> turtle.setheading(90)
>>> turtle.heading()
90.0
2

See

class turtle. Vec2D(x , y)

A two-dimensional vector class, used as a helper class for implementing turtle graphics. May be useful for turtle graphics programs too. Derived from tuple, so a vector is a tuple

Provides (for a, b vectors, k number)

  • >>> turtle.heading()
    22.0
    >>> turtle.right(45)
    >>> turtle.heading()
    337.0
    
    59 vector addition

  • >>> turtle.heading()
    22.0
    >>> turtle.right(45)
    >>> turtle.heading()
    337.0
    
    60 vector subtraction

  • >>> turtle.heading()
    22.0
    >>> turtle.right(45)
    >>> turtle.heading()
    337.0
    
    61 inner product

  • >>> turtle.heading()
    22.0
    >>> turtle.right(45)
    >>> turtle.heading()
    337.0
    
    62 and
    >>> turtle.heading()
    22.0
    >>> turtle.right(45)
    >>> turtle.heading()
    337.0
    
    63 multiplication with scalar

  • >>> turtle.heading()
    22.0
    >>> turtle.right(45)
    >>> turtle.heading()
    337.0
    
    64 absolute value of a

  • >>> turtle.heading()
    22.0
    >>> turtle.right(45)
    >>> turtle.heading()
    337.0
    
    65 rotation

Help and configuration

How to use help

The public methods of the Screen and Turtle classes are documented extensively via docstrings. So these can be used as online-help via the Python help facilities

  • When using IDLE, tooltips show the signatures and first lines of the docstrings of typed in function-/method calls

  • Calling on methods or functions displays the docstrings

    >>> turtle.setheading(90)
    >>> turtle.heading()
    90.0
    
    3

  • The docstrings of the functions which are derived from methods have a modified form

    >>> turtle.setheading(90)
    >>> turtle.heading()
    90.0
    
    4

These modified docstrings are created automatically together with the function definitions that are derived from the methods at import time

Translation of docstrings into different languages

There is a utility to create a dictionary the keys of which are the method names and the values of which are the docstrings of the public methods of the classes Screen and Turtle

turtle. write_docstringdict(filename='turtle_docstringdict') Parameters

filename – a string, used as filename

Create and write docstring-dictionary to a Python script with the given filename. This function has to be called explicitly (it is not used by the turtle graphics classes). The docstring dictionary will be written to the Python script

>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
67. It is intended to serve as a template for translation of the docstrings into different languages

If you (or your students) want to use with online help in your native language, you have to translate the docstrings and save the resulting file as e. g.

>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
69

If you have an appropriate entry in your

>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
46 file this dictionary will be read in at import time and will replace the original English docstrings

At the time of this writing there are docstring dictionaries in German and in Italian. (Requests please to glingl @ aon . at. )

How to configure Screen and Turtles

The built-in default configuration mimics the appearance and behaviour of the old turtle module in order to retain best possible compatibility with it

If you want to use a different configuration which better reflects the features of this module or which better fits to your needs, e. g. for use in a classroom, you can prepare a configuration file

>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
46 which will be read at import time and modify the configuration according to its settings

The built in configuration would correspond to the following turtle. cfg

>>> turtle.setheading(90)
>>> turtle.heading()
90.0
5

Short explanation of selected entries

  • The first four lines correspond to the arguments of the

    >>> turtle.heading()
    22.0
    >>> turtle.right(45)
    >>> turtle.heading()
    337.0
    
    72 method

  • Line 5 and 6 correspond to the arguments of the method

    >>> turtle.heading()
    22.0
    >>> turtle.right(45)
    >>> turtle.heading()
    337.0
    
    73

  • shape can be any of the built-in shapes, e. g. arrow, turtle, etc. For more info try

    >>> turtle.heading()
    22.0
    >>> turtle.right(45)
    >>> turtle.heading()
    337.0
    
    74

  • If you want to use no fillcolor (i. e. make the turtle transparent), you have to write

    >>> turtle.heading()
    22.0
    >>> turtle.right(45)
    >>> turtle.heading()
    337.0
    
    75 (but all nonempty strings must not have quotes in the cfg-file)

  • If you want to reflect the turtle its state, you have to use

    >>> turtle.heading()
    22.0
    >>> turtle.right(45)
    >>> turtle.heading()
    337.0
    
    76

  • If you set e. g.

    >>> turtle.heading()
    22.0
    >>> turtle.right(45)
    >>> turtle.heading()
    337.0
    
    77 the docstringdict
    >>> turtle.heading()
    22.0
    >>> turtle.right(45)
    >>> turtle.heading()
    337.0
    
    78 will be loaded at import time (if present on the import path, e. g. in the same directory as )

  • The entries exampleturtle and examplescreen define the names of these objects as they occur in the docstrings. The transformation of method-docstrings to function-docstrings will delete these names from the docstrings

  • using_IDLE. Set this to

    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.backward(30)
    >>> turtle.position()
    (-30.00,0.00)
    
    46 if you regularly work with IDLE and its
    >>> turtle.heading()
    90.0
    >>> turtle.position()
    (0.00,-10.00)
    >>> turtle.home()
    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.heading()
    0.0
    
    3 switch (“no subprocess”). This will prevent to enter the mainloop

There can be a

>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
46 file in the directory where is stored and an additional one in the current working directory. The latter will override the settings of the first one

The

>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
85 directory contains a
>>> turtle.heading()
22.0
>>> turtle.right(45)
>>> turtle.heading()
337.0
46 file. You can study it as an example and see its effects when running the demos (preferably not from within the demo-viewer)

— Demo scripts

Gói này bao gồm một bộ tập lệnh demo. These scripts can be run and viewed using the supplied demo viewer as follows

>>> turtle.setheading(90)
>>> turtle.heading()
90.0
6

Alternatively, you can run the demo scripts individually. Ví dụ,

>>> turtle.setheading(90)
>>> turtle.heading()
90.0
7

The package directory contains

  • Trình xem demo

    >>> turtle.heading()
    22.0
    >>> turtle.right(45)
    >>> turtle.heading()
    337.0
    
    90 có thể được sử dụng để xem mã nguồn của các tập lệnh và chạy chúng cùng một lúc

  • Multiple scripts demonstrating different features of the module. Các ví dụ có thể được truy cập thông qua menu Ví dụ. Chúng cũng có thể được chạy độc lập

  • Tệp

    >>> turtle.heading()
    22.0
    >>> turtle.right(45)
    >>> turtle.heading()
    337.0
    
    46 dùng làm ví dụ về cách viết và sử dụng các tệp đó

Các kịch bản demo là

Tên

Sự miêu tả

Đặc trưng

thiết kế phụ

complex classical turtle graphics pattern

>>> turtle.position()
(0.00,0.00)
>>> turtle.forward(25)
>>> turtle.position()
(25.00,0.00)
>>> turtle.forward(-75)
>>> turtle.position()
(-50.00,0.00)
96, delay,
>>> turtle.position()
(0.00,0.00)
>>> turtle.forward(25)
>>> turtle.position()
(25.00,0.00)
>>> turtle.forward(-75)
>>> turtle.position()
(-50.00,0.00)
97

chaos

graphs Verhulst dynamics, shows that computer’s computations can generate results sometimes against the common sense expectations

world coordinates

clock

analog clock showing time of your computer

turtles as clock’s hands, ontimer

colormixer

thử nghiệm với r, g, b

>>> turtle.position()
(0.00,0.00)
>>> turtle.forward(25)
>>> turtle.position()
(25.00,0.00)
>>> turtle.forward(-75)
>>> turtle.position()
(-50.00,0.00)
79

forest

3 breadth-first trees

randomization

fractalcurves

Hilbert & Koch curves

recursion

lindenmayer

ethnomathematics (indian kolams)

L-System

minimal_hanoi

Towers of Hanoi

Rectangular Turtles as Hanoi discs (shape, shapesize)

nim

play the classical nim game with three heaps of sticks against the computer

turtles as nimsticks, event driven (mouse, keyboard)

paint

super minimalistic drawing program

>>> turtle.position()
(0.00,0.00)
>>> turtle.forward(25)
>>> turtle.position()
(25.00,0.00)
>>> turtle.forward(-75)
>>> turtle.position()
(-50.00,0.00)
77

peace

elementary

turtle. appearance and animation

penrose

aperiodic tiling with kites and darts

>>> turtle.position()
(0.00,0.00)
>>> turtle.forward(25)
>>> turtle.position()
(25.00,0.00)
>>> turtle.forward(-75)
>>> turtle.position()
(-50.00,0.00)
29

planet_and_moon

simulation of gravitational system

compound shapes,

>>> turtle.position()
(0.00,0.00)
>>> turtle.backward(30)
>>> turtle.position()
(-30.00,0.00)
31

round_dance

dancing turtles rotating pairwise in opposite direction

compound shapes, clone shapesize, tilt, get_shapepoly, update

sorting_animate

visual demonstration of different sorting methods

simple alignment, randomization

tree

a (graphical) breadth first tree (using generators)

>>> turtle.position()
(0.00,0.00)
>>> turtle.forward(25)
>>> turtle.position()
(25.00,0.00)
>>> turtle.forward(-75)
>>> turtle.position()
(-50.00,0.00)
83

two_canvases

simple design

turtles on two canvases

wikipedia

a pattern from the wikipedia article on turtle graphics

>>> turtle.position()
(0.00,0.00)
>>> turtle.forward(25)
>>> turtle.position()
(25.00,0.00)
>>> turtle.forward(-75)
>>> turtle.position()
(-50.00,0.00)
83,
>>> turtle.position()
(0.00,0.00)
>>> turtle.forward(25)
>>> turtle.position()
(25.00,0.00)
>>> turtle.forward(-75)
>>> turtle.position()
(-50.00,0.00)
32

yinyang

another elementary example

>>> turtle.position()
(0.00,0.00)
>>> turtle.forward(25)
>>> turtle.position()
(25.00,0.00)
>>> turtle.forward(-75)
>>> turtle.position()
(-50.00,0.00)
27

Have fun

Những thay đổi kể từ Python 2. 6

  • The methods

    >>> turtle.heading()
    22.0
    >>> turtle.left(45)
    >>> turtle.heading()
    67.0
    
    03,
    >>> turtle.heading()
    22.0
    >>> turtle.left(45)
    >>> turtle.heading()
    67.0
    
    04 and
    >>> turtle.heading()
    22.0
    >>> turtle.left(45)
    >>> turtle.heading()
    67.0
    
    05 have been eliminated. Methods with these names and functionality are now available only as methods of
    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.forward(25)
    >>> turtle.position()
    (25.00,0.00)
    >>> turtle.forward(-75)
    >>> turtle.position()
    (-50.00,0.00)
    
    07. The functions derived from these remain available. (In fact already in Python 2. 6 these methods were merely duplications of the corresponding
    >>> turtle.heading()
    90.0
    >>> turtle.position()
    (0.00,-10.00)
    >>> turtle.home()
    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.heading()
    0.0
    
    5/
    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.forward(25)
    >>> turtle.position()
    (25.00,0.00)
    >>> turtle.forward(-75)
    >>> turtle.position()
    (-50.00,0.00)
    
    07-methods. )

  • The method

    >>> turtle.heading()
    22.0
    >>> turtle.left(45)
    >>> turtle.heading()
    67.0
    
    09 has been eliminated. The behaviour of
    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.forward(25)
    >>> turtle.position()
    (25.00,0.00)
    >>> turtle.forward(-75)
    >>> turtle.position()
    (-50.00,0.00)
    
    57 and
    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.forward(25)
    >>> turtle.position()
    (25.00,0.00)
    >>> turtle.forward(-75)
    >>> turtle.position()
    (-50.00,0.00)
    
    58 have changed slightly. now every filling-process must be completed with an
    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.forward(25)
    >>> turtle.position()
    (25.00,0.00)
    >>> turtle.forward(-75)
    >>> turtle.position()
    (-50.00,0.00)
    
    58 call

  • A method

    >>> turtle.heading()
    22.0
    >>> turtle.left(45)
    >>> turtle.heading()
    67.0
    
    13 has been added. It returns a boolean value.
    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.backward(30)
    >>> turtle.position()
    (-30.00,0.00)
    
    46 if a filling process is under way,
    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.backward(30)
    >>> turtle.position()
    (-30.00,0.00)
    
    47 otherwise. This behaviour corresponds to a
    >>> turtle.heading()
    22.0
    >>> turtle.left(45)
    >>> turtle.heading()
    67.0
    
    16 call without arguments in Python 2. 6

Changes since Python 3. 0

  • The methods

    >>> turtle.heading()
    22.0
    >>> turtle.left(45)
    >>> turtle.heading()
    67.0
    
    17,
    >>> turtle.heading()
    22.0
    >>> turtle.left(45)
    >>> turtle.heading()
    67.0
    
    18 and
    >>> turtle.heading()
    22.0
    >>> turtle.left(45)
    >>> turtle.heading()
    67.0
    
    19 have been added. Thus the full range of regular linear transforms is now available for transforming turtle shapes.
    >>> turtle.heading()
    22.0
    >>> turtle.left(45)
    >>> turtle.heading()
    67.0
    
    20 đã được tăng cường chức năng. it now can be used to get or set the tiltangle.
    >>> turtle.heading()
    22.0
    >>> turtle.left(45)
    >>> turtle.heading()
    67.0
    
    21 has been deprecated

  • The method

    >>> turtle.heading()
    22.0
    >>> turtle.left(45)
    >>> turtle.heading()
    67.0
    
    22 has been added as a complement to
    >>> turtle.heading()
    22.0
    >>> turtle.left(45)
    >>> turtle.heading()
    67.0
    
    23 which in fact binds actions to the keyrelease event. Accordingly the latter has got an alias.
    >>> turtle.heading()
    22.0
    >>> turtle.left(45)
    >>> turtle.heading()
    67.0
    
    24

  • Phương pháp

    >>> turtle.heading()
    22.0
    >>> turtle.left(45)
    >>> turtle.heading()
    67.0
    
    25 đã được thêm vào. Vì vậy, khi chỉ làm việc với các đối tượng Màn hình và Rùa, người ta không được nhập thêm
    >>> turtle.position()
    (0.00,0.00)
    >>> turtle.backward(30)
    >>> turtle.position()
    (-30.00,0.00)
    
    05 nữa

  • Hai phương thức nhập liệu đã được thêm vào

    >>> turtle.heading()
    22.0
    >>> turtle.left(45)
    >>> turtle.heading()
    67.0
    
    27 và
    >>> turtle.heading()
    22.0
    >>> turtle.left(45)
    >>> turtle.heading()
    67.0
    
    28. Các hộp thoại đầu vào bật lên này và trả về các chuỗi và số tương ứng