Python Turtle 時鐘程式

2021-10-16 10:32:56 字數 3905 閱讀 2978

:#抬起畫筆,向前移動step長的距離

turtle.penup(

) turtle.forward(step)

turtle.pendown(

)def

gethand

(name,length)

:#登錄檔針的形狀

turtle.reset(

)#注意重置

skip(

-length*

0.1)

turtle.begin_poly(

) turtle.forward(length*

1.1)

turtle.end_poly(

) handshape = turtle.get_poly(

) turtle.register_shape(name,handshape)

definit()

:#初始化指標,打字writer

global sechand,minhand,hurhand,writer

turtle.mode(

'logo'

) gethand(

'sechand'

,135

) gethand(

'minhand'

,125

) gethand(

'hurhand',90

) sechand = turtle.turtle(

) sechand.shape(

'sechand'

) minhand = turtle.turtle(

) minhand.shape(

'minhand'

) hurhand = turtle.turtle(

) hurhand.shape(

'hurhand'

)#設定指標尺寸

sechand.shapesize(1,

1,3)

minhand.shapesize(1,

1,5)

hurhand.shapesize(1,

1,9)

for hand in sechand,minhand,hurhand:

hand.color(

'dimgray'

)#建立打字手writer

writer = turtle.turtle(

) writer.hideturtle(

) writer.penup(

)def

drawclock

(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=

('微軟雅黑',14

))elif i ==30:

skip(25)

turtle.write(

int(6)

,、 align=

'center'

,font=

('微軟雅黑',14

))skip(-25

)elif

(i ==

25or i ==35)

: skip(20)

turtle.write(

int(i/5)

,、 align=

'center'

,font=

('微軟雅黑',14

))skip(-20

)else

: turtle.write(

int(i/5)

,、 align=

'center'

,font=

('微軟雅黑',14

))skip(

-radius-20)

#注意返回

else

: turtle.dot(5)

skip(

-radius)

turtle.right(6)

defweek

(date_today)

: week=

['星期一'

,'星期二'

,'星期三'

,'星期四'

,'星期五'

,'星期六'

,'星期日'

]return week[date_today.weekday()]

defdate

(date_today)

:return

'{}-{}-{}'

.format

(date_today.year,date_today.month,date_today.day)

deftictok()

: date_today = datetime.today(

) second = date_today.second + date_today.microsecond*

0.000001

minute = date_today.minute + second/

60.0

hour = date_today.hour + minute/

60.0

sechand.setheading(

6*second)

minhand.setheading(

6*minute)

hurhand.setheading(

30*hour)

turtle.tracer(

false

) writer.forward(65)

writer.write(week(date_today)

,、 align=

'center'

,font=

('微軟雅黑',14

))writer.back(

130)

writer.write(date(date_today)

,、 align=

'center'

,font=

('微軟雅黑',14

))turtle.tracer(

true

) writer.home(

)#注意返回

turtle.ontimer(tictok,

100)

defmain()

: turtle.tracer(

false

) init(

) drawclock(

160)

turtle.tracer(

true

) tictok(

) turtle.mainloop(

)if __name__ ==

'__main__'

: main(

)

Python Turtle繪製動態時鐘

coding utf 8 import turtle from datetime import 抬起畫筆,向前運動一段距離放下 def skip step turtle.penup turtle.forward step turtle.pendown def mkhand name,length 註...

Python Turtle庫繪製表盤時鐘

執行效果 源 1 coding utf 823 import turtle 4from datetime import 56 抬起畫筆,向前運動一段距離放下 7def skip step 8turtle.penup 9turtle.forward step 10turtle.pendown 1112...

Python Turtle庫 3 5時鐘程式

import turtle from datetime import def skip step turtle.penup turtle.forward step turtle.pendown def mkhand name,length 註冊turtle形狀,建立表針turtle turtle.r...