Turtle圖形庫
Turtle庫是Python內(nèi)置的圖形化模塊,屬于標(biāo)準(zhǔn)庫之一,位于Python安裝目錄的lib文件夾下,常用函數(shù)有以下幾種:
-
畫筆控制函數(shù)
-
penup()
:抬起畫筆; -
pendown()
:落下畫筆; -
pensize(width)
:畫筆寬度; -
pencolor(color)
:畫筆顏色;
-
-
運(yùn)動(dòng)控制函數(shù)
-
forward(d)/fd(d)
:直行d個(gè)像素; -
circle(r, extent = None)
:繪制半徑為r,角度為extent的弧形,圓心默認(rèn)在海龜左側(cè)距離r的位置;
-
-
方向控制函數(shù)
-
setheading(angle)/seth(angle)
:改變前進(jìn)方向; -
left(angle)
:海龜左轉(zhuǎn); -
right(angle)
:海龜右轉(zhuǎn);
-
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()
結(jié)果
#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()
結(jié)果
#繪制時(shí)鐘
# coding=utf-8
import turtle as tt
from datetime import *
# 當(dāng)前日期屬于一周的第幾天
def Week(t):
week = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"]
return week[t.weekday()]
# 獲取當(dāng)前時(shí)間
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)
# 移動(dòng)畫筆,距離為distance
def movePen(distance):
tt.penup()
tt.pensize(5)
tt.pencolor("blue")
tt.fd(distance)
tt.pendown()
# 繪制表針
def makeHands(name, length):
# 清空窗口,重置turtule狀態(tài)為初始狀態(tài)
tt.reset()
movePen(-length * 0.1)
# 開始記錄多邊形的頂點(diǎn)
tt.begin_poly()
tt.fd(length * 1.1)
# 停止記錄多邊形的頂點(diǎn)
tt.end_poly()
# 返回記錄的多邊形
handForm = tt.get_poly()
tt.register_shape(name, handForm)
# 初始化
def initial():
global secHand, minHand, hurHand, printer
# 重置方向向北(上),正角度為順時(shí)針
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狀態(tài)為初始狀態(tài)
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:
# 繪制指定半徑和顏色的點(diǎn)
tt.dot(5, "red")
movePen(-R)
tt.right(6)
# 表針的動(dòng)態(tài)顯示
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))
# 設(shè)置當(dāng)前畫筆位置為原點(diǎn),方向朝東
printer.home()
tt.tracer(True)
# 經(jīng)過100ms后繼續(xù)調(diào)用handsMove函數(shù)
tt.ontimer(handsMove, 100)
# 調(diào)用定義的函數(shù),打開和關(guān)閉動(dòng)畫,為更新圖紙?jiān)O(shè)置延遲;
tt.tracer(False)
initial()
drawClock(200)
tt.tracer(True)
handsMove()
tt.mainloop()
結(jié)果
歡迎關(guān)注微信公眾號(hào):村雨1943;創(chuàng)作不易,未經(jīng)同意,轉(zhuǎn)載請(qǐng)注明出處~
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

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