最近學習python,接觸到turtle標準庫,後面又看到利用python內建庫turtle庫畫乙個實時鐘錶。話不多說,直接上**叭!
import turtle
from datetime import *
def skip
(step)
: turtle.
penup()
turtle.
forward
(step)
turtle.
pendown()
def mkhand
(name, length)
: # 註冊turtle形狀,建立表針turtle
turtle.
reset()
skip
(-length *
0.1)
# 開始記錄多邊形的頂點。當前的烏龜位置是多邊形的第乙個頂點。
turtle.
begin_poly()
turtle.
forward
(length *
1.1)
# 停止記錄多邊形的頂點。當前的烏龜位置是多邊形的最後乙個頂點。將與第乙個頂點相連。
turtle.
end_poly()
# 返回最後記錄的多邊形。
handform = turtle.
get_poly()
turtle.
register_shape
(name, handform)
def init()
: global sechand, minhand, hurhand, printer
# 重置turtle指向北
turtle.
mode
("logo"
) # 建立三個表針turtle並初始化
mkhand
("sechand"
,135
)mkhand
("minhand"
,125
)mkhand
("hurhand",90
) sechand = turtle.
turtle()
sechand.
shape
("sechand"
) minhand = turtle.
turtle()
minhand.
shape
("minhand"
) hurhand = turtle.
turtle()
hurhand.
shape
("hurhand"
)for hand in sechand, minhand, hurhand:
hand.
shapesize(1
,1,3
) hand.
speed(0
) # 建立輸出文字turtle
printer = turtle.
turtle()
# 隱藏畫筆的turtle形狀
printer.
hideturtle()
printer.
penup()
def setupclock
(radius)
: # 建立表的外框
turtle.
reset()
turtle.
pensize(7
)for i in range(60
):skip
(radius)
if i %5==
0:turtle.
forward(20
)skip
(-radius -20)
skip
(radius +20)
if i ==0:
turtle.
write
(int(12
), align=
"center"
, font=
("courier",14
,"bold"))
elif i ==30:
skip(25
) turtle.
write
(int
(i /5)
, align=
"center"
, font=
("courier",14
,"bold"))
skip(-
25)elif (i ==
25or i ==35)
:skip(20
) turtle.
write
(int
(i /5)
, align=
"center"
, font=
("courier",14
,"bold"))
skip(-
20)else
: turtle.
write
(int
(i /5)
, align=
"center"
, font=
("courier",14
,"bold"))
skip
(-radius -20)
else
: turtle.
dot(5)
skip
(-radius)
turtle.
right(6
)def week
(t):
week =
["星期一"
,"星期二"
,"星期三"
,"星期四"
,"星期五"
,"星期六"
,"星期日"
]return week[t.
weekday()
]def date
(t):
y = t.year
m = t.month
d = t.day
return
"%s年%d月%d日"
%(y, m, d)
def tick()
: # 繪製表針的動態顯示
t = datetime.
today()
second = t.second + t.microsecond *
0.000001
minute = t.minute + second /
60.0
hour = t.hour + minute /
60.0
sechand.
setheading(6
* second)
minhand.
setheading(6
* minute)
hurhand.
setheading(30
* hour)
turtle.
tracer
(false)
printer.
forward(65
) printer.
write
(week
(t), align=
"center"
, font=
("courier",14
,"bold"))
printer.
back
(130
) printer.
write
(date
(t), align=
"center"
, font=
("courier",14
,"bold"))
printer.
home()
turtle.
tracer
(true)
# 100ms後繼續呼叫tick
turtle.
ontimer
(tick,
100)
def main()
: # 開啟/關閉龜動畫,並為更新圖紙設定延遲。
python turtle庫的使用
turtle庫是turtle繪圖體系的python實現。誕生於1969年,主要用於程式設計入門,是python語言標準庫之一,入門級繪相簿。import turtle from turtlr import import turtle as t turtle.setup width,height,st...
python turtle庫的認識
os庫 作業系統介面 math庫 數學庫 常見的數學算式 等等 參見我另外一篇csdn 庫就會產生很多方法 介面api 先學會理解使用,再去想為什麼 import turtle 引入模組 庫 turtle t turtle.pen pen 函式。自動建立乙個畫布 turtle.setup width...
Python turtle庫知識小結
turtle庫是python語言中乙個很流行的繪製影象的函式庫 使用之前需要導入庫 import turtle turtle.setup width,height,startx,starty setup 設定窗體的位置和大小 相對於桌面的起始點的座標以及視窗的寬度高度,若不寫視窗的起始點,則預設在桌...