程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

How to import Turtle library in Python

編輯:Python

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庫.


導入Turtle

從一個空白的 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)
復制代碼

My turtle window disappeared!

在這一點上,我們的小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


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved