Turtle庫是Python內置的圖形化模塊,屬于標準庫之一,位于Python安裝目錄的lib文件夾下,常用函數有以下幾種:
畫筆控制函數
- penup():抬起畫筆;
- pendown():落下畫筆;
- pensize(width):畫筆寬度;
- pencolor(color):畫筆顏色;
運動控制函數
- forward(d)/fd(d):直行d個像素;
- circle(r, extent = None):繪制半徑為r,角度為extent的弧形,圓心默認在海龜左側距離r的位置;
方向控制函數
- setheading(angle)/seth(angle):改變前進方向;
- left(angle):海龜左轉;
- right(angle):海龜右轉;
Turtle庫的使用
#coding=utf-8 #繪制蟒蛇 import turtle turtle.penup() turtle.pencolor("red") turtle.forward(-250) turtle.pendown() turtle.pensize(10) turtle.right(45) for i in range(4): turtle.circle(40, 80) turtle.circle(-40, 80) turtle.circle(40, 80 / 2) turtle.fd(40) turtle.circle(16, 180) turtle.fd(40 * 2 / 3) turtle.done()
結果
#coding=utf-8 # 繪制五角星 import turtle turtle.pensize(5) turtle.pencolor("red") turtle.forward(200) for i in range(4): turtle.right(144) turtle.fd(200) turtle.done()
結果
#繪制時鐘 # coding=utf-8 import turtle as tt from datetime import * # 當前日期屬于一周的第幾天 def Week(t): week = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"] return week[t.weekday()] # 獲取當前時間 def Date(t): y = t.year m = t.month d = t.day cur_hour = t.hour; cur_min = t.minute; cur_sec = t.second; return "%s-%d-%d %d:%02d:%02d" % (y, m, d, cur_hour, cur_min, cur_sec) # 移動畫筆,距離為distance def movePen(distance): tt.penup() tt.pensize(5) tt.pencolor("blue") tt.fd(distance) tt.pendown() # 繪制表針 def makeHands(name, length): # 清空窗口,重置turtule狀態為初始狀態 tt.reset() movePen(-length * 0.1) # 開始記錄多邊形的頂點 tt.begin_poly() tt.fd(length * 1.1) # 停止記錄多邊形的頂點 tt.end_poly() # 返回記錄的多邊形 handForm = tt.get_poly() tt.register_shape(name, handForm) # 初始化 def initial(): global secHand, minHand, hurHand, printer # 重置方向向北(上),正角度為順時針 tt.mode("logo") # 建立并初始化表針 makeHands("secHand", 180) makeHands("minHand", 150) makeHands("hurHand", 110) secHand = tt.Turtle() secHand.shape("secHand") minHand = tt.Turtle() minHand.shape("minHand") hurHand = tt.Turtle() hurHand.shape("hurHand") for hand in secHand, minHand, hurHand: hand.shapesize(1, 1, 4) hand.speed(0) # 輸出文字 printer = tt.Turtle() # 隱藏畫筆 printer.hideturtle() printer.penup() # 繪制表盤外框 def drawClock(R): # 清空窗口,重置turtule狀態為初始狀態 tt.reset() # 畫筆尺寸 tt.pensize(5) for i in range(60): movePen(R) if i % 5 == 0: tt.fd(20) movePen(-R - 20) movePen(R + 20) if i == 0: # 寫文本 tt.write(int(12), align="center", font=("Consolas", 14, "bold")) elif i == 30: movePen(25) tt.write(int(i / 5), align="center", font=("Consolas", 14, "bold")) movePen(-25) elif (i == 25 or i == 35): movePen(20) tt.write(int(i / 5), align="center", font=("Consolas", 14, "bold")) movePen(-20) else: tt.write(int(i / 5), align="center", font=("Consolas", 14, "bold")) movePen(-R - 20) else: # 繪制指定半徑和顏色的點 tt.dot(5, "red") movePen(-R) tt.right(6) # 表針的動態顯示 def handsMove(): t = datetime.today() second = t.second + t.microsecond * 0.000001 minute = t.minute + second / 60.0 hour = t.hour + minute / 60.0 secHand.seth(6 * second) minHand.seth(6 * minute) hurHand.seth(30 * hour) tt.tracer(False) printer.fd(65) tt.pencolor("green") printer.write(Week(t), align="center", font = ("黑體", 14)) printer.back(130) printer.write(Date(t), align="center", font = ("Consolas", 14)) # 設置當前畫筆位置為原點,方向朝東 printer.home() tt.tracer(True) # 經過100ms后繼續調用handsMove函數 tt.ontimer(handsMove, 100) # 調用定義的函數,打開和關閉動畫,為更新圖紙設置延遲; tt.tracer(False) initial() drawClock(200) tt.tracer(True) handsMove() tt.mainloop()
結果
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
