Python Turtle庫繪製表盤時鐘

2022-04-02 18:53:45 字數 4469 閱讀 9614

執行效果:

源**:

1

#coding=utf-823

import

turtle

4from datetime import *56

#抬起畫筆,向前運動一段距離放下

7def

skip(step):

8turtle.penup()

9turtle.forward(step)

10turtle.pendown()

1112

defmkhand(name, length):13#

註冊turtle形狀,建立表針turtle

14turtle.reset()

15 skip(-length * 0.1)16#

開始記錄多邊形的頂點。當前的烏龜位置是多邊形的第乙個頂點。

17turtle.begin_poly()

18 turtle.forward(length * 1.1)19#

停止記錄多邊形的頂點。當前的烏龜位置是多邊形的最後乙個頂點。將與第乙個頂點相連。

20turtle.end_poly()21#

返回最後記錄的多邊形。

22 handform =turtle.get_poly()

23turtle.register_shape(name, handform)

2425

definit():

26global

sechand, minhand, hurhand, printer27#

重置turtle指向北

28 turtle.mode("

logo")

29#建立三個表針turtle並初始化

30 mkhand("

sechand

", 135)

31 mkhand("

minhand

", 125)

32 mkhand("

hurhand

", 90)

33 sechand =turtle.turtle()

34 sechand.shape("

sechand")

35 minhand =turtle.turtle()

36 minhand.shape("

minhand")

37 hurhand =turtle.turtle()

38 hurhand.shape("

hurhand")

3940

for hand in

sechand, minhand, hurhand:

41 hand.shapesize(1, 1, 3)

42hand.speed(0)

4344

#建立輸出文字turtle

45 printer =turtle.turtle()46#

隱藏畫筆的turtle形狀

47printer.hideturtle()

48printer.penup()

4950

defsetupclock(radius):51#

建立表的外框

52turtle.reset()

53 turtle.pensize(7)

54for i in range(60):

55skip(radius)

56if i % 5 ==0:

57 turtle.forward(20)

58 skip(-radius - 20)

5960 skip(radius + 20)

61if i ==0:

62 turtle.write(int(12), align="

center

", font=("

courier

", 14, "

bold"))

63elif i == 30:

64 skip(25)

65 turtle.write(int(i/5), align="

center

", font=("

courier

", 14, "

bold"))

66 skip(-25)

67elif (i == 25 or i == 35):

68 skip(20)

69 turtle.write(int(i/5), align="

center

", font=("

courier

", 14, "

bold"))

70 skip(-20)

71else

:72 turtle.write(int(i/5), align="

center

", font=("

courier

", 14, "

bold"))

73 skip(-radius - 20)

74else

:75 turtle.dot(5)

76 skip(-radius)

77 turtle.right(6)

7879

defweek(t):

80 week = ["

星期一", "

星期二", "

星期三",81

"星期四

", "

星期五", "

星期六", "

星期日"]82

return

week[t.weekday()]

8384

defdate(t):

85 y =t.year

86 m =t.month

87 d =t.day

88return

"%s年%d月%d日

" %(y, m, d)

8990

deftick():91#

繪製表針的動態顯示

92 t =datetime.today()

93 second = t.second + t.microsecond * 0.000001

94 minute = t.minute + second / 60.0

95 hour = t.hour + minute / 60.0

96 sechand.setheading(6 *second)

97 minhand.setheading(6 *minute)

98 hurhand.setheading(30 *hour)

99100

turtle.tracer(false)

101 printer.forward(65)

102 printer.write(week(t), align="

center",

103 font=("

courier

", 14, "

bold"))

104 printer.back(130)

105 printer.write(date(t), align="

center",

106 font=("

courier

", 14, "

bold"))

107printer.home()

108turtle.tracer(true)

109110

#100ms後繼續呼叫tick

111 turtle.ontimer(tick, 100)

112113

defmain():

114#

開啟/關閉龜動畫,並為更新圖紙設定延遲。

115turtle.tracer(false)

116init()

117 setupclock(160)

118turtle.tracer(true)

119tick()

120turtle.mainloop()

121122

if__name__ == "

__main__":

123 main()

總結python turtle庫繪製七晶管日期

import turtle def drawline draw turtle.pendown if draw else turtle.penup turtle.fd 40 turtle.right 90 def drawdigit digit drawline true if digit in 2,...

python turtle 繪製創意梅花

author 魏振東 data 2019.10.25 func 繪製創意圖形 from turtle import from random import 畫雪花 defdrawsnow hideturtle pensize 2 for i in range 20 pencolor white pen...

Python turtle蟒蛇繪製案例

mooc教程 北理 蟒蛇例項 庫的使用 import 庫名 法二 from 庫名 import 法三 import 庫名 as 庫別名 推薦使用 import turtle turtle.setup width,height,startx,starty setup 設定窗體大小及位置 非必須 tur...