1. 程式人生 > 其它 >Python實用工具,turtle庫,Python實現簡易版時鐘

Python實用工具,turtle庫,Python實現簡易版時鐘

前言

Python函式庫眾多,而且在不斷更新,所以學習這些函式庫最有效的方法,就是閱讀Python官方文件。同時藉助Google和百度。

turtle庫中文文件:

https://docs.python.org/zh-cn/3/library/turtle.html

開發工具

Python版本:3.6.4

相關模組:

turtle庫等python自帶的模組。

環境搭建

安裝Python並新增到環境變數即可。

原理介紹

利用Turtle庫製作一個簡易時鐘可以分為三步。

第一步:初始化

第二步:建立時鐘

第三步,動態顯示時鐘

初始化需要定義時針分針秒針以及列印文字所需的turtle物件,共四個。其中時針分針秒針這三個turtle物件的定義方式如下:

createHand('second_hand', 150)
	createHand('minute_hand', 125)
	createHand('hour_hand', 85)
	# 秒, 分, 時
	second_hand = turtle.Turtle()
	second_hand.shape('second_hand')
	minute_hand = turtle.Turtle()
	minute_hand.shape('minute_hand')
	hour_hand = turtle.Turtle()
	hour_hand.shape('hour_hand')
	for hand in [second_hand, minute_hand, hour_hand]:
		hand.shapesize(1, 1, 3)
		hand.speed(0)

其中createHand函式用於建立錶針(定義形狀長度等),其程式碼實現如下:

'''建立錶針turtle'''
def createHand(name, length):
	turtle.reset()
	move(-length * 0.01)
	turtle.begin_poly()
	turtle.forward(length * 1.01)
	turtle.end_poly()
	hand = turtle.get_poly()
	turtle.register_shape(name, hand)

然後定義用於列印文字的turtle物件:

# 用於列印日期等文字
	printer = turtle.Turtle()
	printer.hideturtle()
	printer.penup()
	createClock(160)

即繪製時鐘。其程式碼實現如下:

'''建立時鐘'''
def createClock(radius):
	turtle.reset()
	turtle.pensize(7)
	for i in range(60):
		move(radius)
		if i % 5 == 0:
			turtle.forward(20)
			move(-radius-20)
		else:
			turtle.dot(5)
			move(-radius)
		turtle.right(6)

為了便於大家理解程式碼,錄了一小段這部分程式碼執行時的效果圖:

動態顯示時鐘的原始碼如下:

'''動態顯示錶針'''
def startTick(second_hand, minute_hand, hour_hand, printer):
	today = datetime.datetime.today()
	second = today.second + today.microsecond * 1e-6
	minute = today.minute + second / 60.
	hour = (today.hour + minute / 60) % 12
	# 設定朝向
	second_hand.setheading(6 * second)
	minute_hand.setheading(6 * minute)
	hour_hand.setheading(12 * hour)
	turtle.tracer(False)
	printer.forward(65)
	printer.write(getWeekday(today), align='center', font=("Courier", 14, "bold"))
	printer.forward(120)
	printer.write('12', align='center', font=("Courier", 14, "bold"))
	printer.back(250)
	printer.write(getDate(today), align='center', font=("Courier", 14, "bold"))
	printer.back(145)
	printer.write('6', align='center', font=("Courier", 14, "bold"))
	printer.home()
	printer.right(92.5)
	printer.forward(200)
	printer.write('3', align='center', font=("Courier", 14, "bold"))
	printer.left(2.5)
	printer.back(400)
	printer.write('9', align='center', font=("Courier", 14, "bold"))
	printer.home()
	turtle.tracer(True)
	# 100ms呼叫一次
	turtle.ontimer(lambda: startTick(second_hand, minute_hand, hour_hand, printer), 100)

即利用datetime庫獲取當前的日期與時間,將日期列印在鐘錶上下兩側,並根據時間調整錶針角度,並標明時鐘上的點所代表的數字。

注意:為了執行程式碼時直接呈現出時鐘,第一第二步中的程式碼執行時均設定tracker為False。僅在第三步中設定tracker為True。

文章到這裡就結束了,感謝你的觀看,關注我每天分享Python小工具系列,下篇文章分享簡易音樂播放器

為了感謝讀者們,我想把我最近收藏的一些程式設計乾貨分享給大家,回饋每一個讀者,希望能幫到你們。

乾貨主要有:

① 2000多本Python電子書(主流和經典的書籍應該都有了)

② Python標準庫資料(最全中文版)

③ 專案原始碼(四五十個有趣且經典的練手專案及原始碼)

④ Python基礎入門、爬蟲、web開發、大資料分析方面的視訊(適合小白學習)

⑤ Python學習路線圖(告別不入流的學習)

⑥ 兩天的Python爬蟲訓練營直播許可權

**All done~完整原始碼+乾貨詳見個人主頁簡介或者私信獲取