Python——初學(xué)圖形化程序設(shè)計(jì)
Turtle模塊繪制圖像與添加顏色將筆移到任何位置繪制奧林匹克環(huán)標(biāo)志
Turtle模塊
在Python中有很多編寫圖形程序的方法,一個(gè)簡單的啟動(dòng)圖形化程序設(shè)計(jì)的方法是使用Python內(nèi)嵌的Turtle模塊。Turtle模塊是Python內(nèi)嵌的繪制線、圓以及其他形狀(包括文本)的圖形模塊。
繪制圖像與添加顏色
導(dǎo)入Turtle模塊 import turtle 顯示當(dāng)前Turtle的當(dāng)前位置 turtle.showturtle() 繪制文本字符串 turtle.write(“Hello world!”) 向前移動(dòng)100個(gè)像素 turtle.forward(100) 右轉(zhuǎn)45度,將箭頭顏色改為紅色,并向前移動(dòng)50像素 turtle.right(45) turtle.color(“red”) turtle.forward(50) 
將筆移到任何位置
箭頭在Turtle圖形窗口的中心位置的坐標(biāo)是(0,0),可以使用goto(x,y)命令將turtle移動(dòng)到任何一個(gè)特定點(diǎn)(x,y) turtle,goto(0,50) 使用penup()和pendown()命令設(shè)置抬起放下筆控制移動(dòng)筆時(shí)是否繪制一條線 turtle.penup() turtle.goto(50,-50) turtle.pendown() 使用circle命令繪制一個(gè)圓 turtle.circle(50) 
繪制奧林匹克環(huán)標(biāo)志
import turtle
turtle.color("blue")
turtle.penup()
turtle.goto(-110,-25)
turtle.pendown()
turtle.circle(45)
turtle.color("black")
turtle.penup()
turtle.goto(0,-25)
turtle.pendown()
turtle.circle(45)
turtle.color("red")
turtle.penup()
turtle.goto(110,-25)
turtle.pendown()
turtle.circle(45)
turtle.color("yellow")
turtle.penup()
turtle.goto(-55,-75)
turtle.pendown()
turtle.circle(45)
turtle.color("green")
turtle.penup()
turtle.goto(55,-75)
turtle.pendown()
turtle.circle(45)

|