Python Tkinter模塊實(shí)現(xiàn)時(shí)鐘功能應(yīng)用示例
本文實(shí)例講述了Python Tkinter模塊實(shí)現(xiàn)時(shí)鐘功能。分享給大家供大家參考,具體如下:
本機(jī)測(cè)試效果:

完整代碼:
# coding=utf-8
from Tkinter import *
import _tkinter
import math
import time
from threading import Thread
class Clock:
def __init__(self, master, x, y, width, height, radius):
'''
:param master: 父窗口
:param x: 時(shí)鐘中心點(diǎn)的x坐標(biāo)
:param y: 時(shí)鐘中心點(diǎn)的y坐標(biāo)
:param width: 畫(huà)布的寬度
:param height: 畫(huà)布的高度
:param radius: 時(shí)鐘鐘盤的半徑
'''
self.centerX = x
self.centerY = y
self.radius = radius
self.canvas = Canvas(master, width=width, height=height) # 畫(huà)布
self.canvas.pack()
self.canvas.create_oval(
x - radius,
y - radius,
x + radius,
y + radius) # 畫(huà)鐘框
self.id_lists = []
self.hourHandRadius = self.radius * 1.0 / 4 # 指針長(zhǎng)度
self.minHandRadius = self.radius * 2.0 / 3 # 分針長(zhǎng)度
self.secHandRadius = self.radius * 4.0 / 5 # 秒針長(zhǎng)度
self.timeVar = StringVar()
# self.timeVar.set('')
self.timeLabel = Label(self.canvas.master, textvariable=self.timeVar)
self.timeLabel.pack(side=BOTTOM)
#self.canvas.master.protocol('WM_DELETE_WINDOW', self.canvas.master.destroy)
def __del__(self):
self._deleteItems(self.id_lists)
# 繪制時(shí)鐘鐘盤
def drawClockDial(self):
# 繪制鐘盤上的數(shù)字1-12
r = self.radius - 15
for i in range(1, 13):
rad = 2 * math.pi / 12 * i
x = self.centerX + math.sin(rad) * r
y = self.centerY - math.cos(rad) * r
id = self.canvas.create_text(x, y, text=str(i))
self.id_lists.append(id)
# 繪制鐘盤上的刻度
r1 = self.radius - 5
r2 = self.radius
for i in range(1, 61):
rad = 2 * math.pi / 60 * i
x1, y1 = self._getPosByRadAndRadius(rad, r1)
x2, y2 = self._getPosByRadAndRadius(rad, r2)
id = self.canvas.create_line(x1, y1, x2, y2)
self.id_lists.append(id)
# 顯示時(shí)間
def showTime(self, tm):
hour = tm.tm_hour % 12
min = tm.tm_min
sec = tm.tm_sec
sec_rad = 2 * math.pi / 60 * sec
min_rad = 2 * math.pi / 60 * (min + sec / 60.0)
hour_rad = 2 * math.pi / 12 * (hour + min / 60.0)
timeStr = '當(dāng)前時(shí)間: %d-%02d-%02d %02d:%02d:%02d' % (
tm.tm_year, tm.tm_mon, tm.tm_mday, hour, min, sec)
self.timeVar.set(timeStr)
hour_id = self._drawLine(hour_rad, self.hourHandRadius, 6)
min_id = self._drawLine(min_rad, self.minHandRadius, 4)
sec_id = self._drawLine(sec_rad, self.secHandRadius, 3)
return (hour_id, min_id, sec_id)
def run(self):
def _run():
while True:
tm = time.localtime()
id_lists = self.showTime(tm)
self.canvas.master.update()
time.sleep(1)
self._deleteItems(id_lists)
thrd = Thread(target=_run) # 創(chuàng)建新的線程
thrd.run() # 啟動(dòng)線程
def _drawLine(self, rad, radius, width):
x, y = self._getPosByRadAndRadius(rad, radius)
id = self.canvas.create_line(
self.centerX, self.centerY, x, y, width=width)
return id
def _getPosByRadAndRadius(self, rad, radius):
x = self.centerX + radius * math.sin(rad)
y = self.centerY - radius * math.cos(rad)
return (x, y)
def _deleteItems(self, id_lists):
for id in id_lists:
try:
self.canvas.delete(id)
except BaseException:
pass
if __name__ == '__main__':
root = Tk()
root.title('www.dbjr.com.cn 時(shí)鐘')
clock = Clock(root, 200, 200, 400, 400, 150)
clock.drawClockDial()
clock.run()
root.mainloop()
待解決的bug:
關(guān)閉程序的時(shí)候,會(huì)出現(xiàn)如下的錯(cuò)誤:

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- Python實(shí)現(xiàn)模擬時(shí)鐘代碼推薦
- Python實(shí)現(xiàn)時(shí)鐘顯示效果思路詳解
- python使用turtle庫(kù)繪制時(shí)鐘
- python實(shí)現(xiàn)簡(jiǎn)易動(dòng)態(tài)時(shí)鐘
- python實(shí)現(xiàn)簡(jiǎn)易數(shù)碼時(shí)鐘
- python控制臺(tái)顯示時(shí)鐘的示例
- python+PyQT實(shí)現(xiàn)系統(tǒng)桌面時(shí)鐘
- Python+Pyqt實(shí)現(xiàn)簡(jiǎn)單GUI電子時(shí)鐘
- python基于Kivy寫(xiě)一個(gè)圖形桌面時(shí)鐘程序
- python實(shí)現(xiàn)好看的時(shí)鐘效果
相關(guān)文章
python發(fā)送郵件示例(支持中文郵件標(biāo)題)
python發(fā)送中文郵件示例,支持中文郵件標(biāo)題和中文郵件內(nèi)容。支持多附件。根據(jù)用戶名推測(cè)郵件服務(wù)器提供商2014-02-02
TensorFLow 數(shù)學(xué)運(yùn)算的示例代碼
這篇文章主要介紹了TensorFLow 數(shù)學(xué)運(yùn)算的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
pytorch DataLoader的num_workers參數(shù)與設(shè)置大小詳解
這篇文章主要介紹了pytorch DataLoader的num_workers參數(shù)與設(shè)置大小詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
已安裝tensorflow-gpu,但keras無(wú)法使用GPU加速的解決
今天小編就為大家分享一篇已安裝tensorflow-gpu,但keras無(wú)法使用GPU加速的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02

