Python Turtle Libraryis a very cool onePython庫,It does this by providing the user with a virtual canvas,enable them to draw various shapes.Make all shapes、The drawing tool where lines and colors appear on the canvas are called turtles,That's why this program is called turtle!We can try the turtle module.我們可以嘗試使用Python的Turtle模塊,But first it needs to be imported.在本教程中,我們將設置一個Python文件,Get yourself ready to useTurtle庫.
從一個空白的 Python 文件開始.It can have any name,我們可以只用 testing.py 來開始.在testing.py中,添加下面這行代碼.
from turtle import *
復制代碼
fromA command just means to import something from outside the current file.After that is the name of the module we want to import,也就是turtle.使用importKeywords allow us to access turtle All code for the module.Asterisk at the end of the line (*) 是一個通配符,它告訴 Python Import everything from this module.
當程序啟動時,Turtle A window is automatically set up for you.If you want to set a custom window size for your program,你可以使用 Screen() 和 .setup() 方法來實現.The code here will give us a width of 750、高度為500turtle window.
drawing_area = Screen()
drawing_area.setup(width=750, height=500)
復制代碼
在這一點上,我們的小TurtleThe entire code of the program looks like this.
from turtle import *
drawing_area = Screen()
drawing_area.setup(width=750, height=500)
復制代碼
然而,when we run the program,我們看到一個TurtleThe window flashed on the screen for a moment,然後就消失了.it's not so good!為了解決這個問題,我們可以使用 done() 方法.
from turtle import *
drawing_area = Screen()
drawing_area.setup(width=750, height=500)
done()
復制代碼
現在,當你運行這個程序時,You should see a beautiful blank canvas,Just waiting for your creativity to unleash