import Tkinter
import math
import time
一些变量
CentreNum = 120
RadiusLen = 90
ShortLen = 77
SecLen = 70
MinLen = 60
HourLen = 40
#TrsC是用于普通数字转换成角度的
TrsC = math.pi / 180.0
PointA = math.cos(30 * TrsC)
PointB = math.sin(30 * TrsC)
class P_CLOCK_CLASS:
"""Clock class"""
#Initialize the Canvas component
def __init__(self):
#self.imgsource = Tkinter.PhotoImage(file = "c:/Python27/green.gif")
self.canvas = Tkinter.Canvas(self.p_root, width = 230, height = 318)
self.canvas.pack()
#self.canvas.create_image(50, 50, image = self.imgsource, anchor = Tkinter.NW)
#Display the current time on Canvas
def DispCurrtTime(self):
#get the current time, and the time value is a string
Time = time.ctime()
self.canvas.create_text(120, 270, text = 'Chinese Time:' + Time, fill = "lightblue")
#Paint the Clock Frame and Clock Moment
def DrawBox(self):
#self.canvas.create_image(0, 0, image = self.imgsource, anchor = Tkinter.NW)
#画一个圆圈表盘
self.canvas.create_oval(30, 30, 210, 210, width = 1, outline = "gray", fill = "white")
#时间刻度
self.canvas.create_line(120, 30, 120, 43, width = 3, fill = "gray")
self.canvas.create_line(120, 210, 120, 197, width = 3, fill = "gray")
self.canvas.create_line(30, 120, 43, 120, width = 3, fill = "gray")
self.canvas.create_line(210, 120, 197, 120, width = 3, fill = "gray")
self.canvas.create_line(CentreNum + RadiusLen * PointA, CentreNum - RadiusLen * PointB, CentreNum + ShortLen * PointA, CentreNum - ShortLen * PointB, fill = "gray")
self.canvas.create_line(CentreNum + RadiusLen * PointA, CentreNum + RadiusLen * PointB, CentreNum + ShortLen * PointA, CentreNum + ShortLen * PointB, fill = "gray")
self.canvas.create_line(CentreNum + RadiusLen * PointB, CentreNum - RadiusLen * PointA, CentreNum + ShortLen * PointB, CentreNum - ShortLen * PointA, fill = "gray")
self.canvas.create_line(CentreNum + RadiusLen * PointB, CentreNum + RadiusLen * PointA, CentreNum + ShortLen * PointB, CentreNum + ShortLen * PointA, fill = "gray")
self.canvas.create_line(CentreNum - RadiusLen * PointA, CentreNum - RadiusLen * PointB, CentreNum - ShortLen * PointA, CentreNum - ShortLen * PointB, fill = "gray")
self.canvas.create_line(CentreNum - RadiusLen * PointA, CentreNum + RadiusLen * PointB, CentreNum - ShortLen * PointA, CentreNum + ShortLen * PointB, fill = "gray")
self.canvas.create_line(CentreNum - RadiusLen * PointB, CentreNum - RadiusLen * PointA, CentreNum - ShortLen * PointB, CentreNum - ShortLen * PointA, fill = "gray")
self.canvas.create_line(CentreNum - RadiusLen * PointB, CentreNum + RadiusLen * PointA, CentreNum - ShortLen * PointB, CentreNum + ShortLen * PointA, fill = "gray")
#Paint the Clock Pointer
def DrawPointer(self):
localTime = time.localtime()
self.canvas.create_line((CentreNum + HourLen * math.sin((30 * localTime.tm_hour + 0.5 * localTime.tm_min) * TrsC)), (CentreNum - HourLen * math.cos((30 * localTime.tm_hour * TrsC + 0.1 * localTime.tm_sec * TrsC))), CentreNum, CentreNum, width = 6, fill = "yellow")
self.canvas.create_line((CentreNum + MinLen * math.sin((6 * localTime.tm_min + 0.1 * localTime.tm_sec) * TrsC)), (CentreNum - MinLen * math.cos((6 * localTime.tm_min + 0.1 * localTime.tm_sec) * TrsC)), CentreNum, CentreNum, width = 4, fill = "light green")
self.canvas.create_line((CentreNum + SecLen * math.sin(6 * localTime.tm_sec * TrsC)), (CentreNum - SecLen * math.cos(6 * localTime.tm_sec * TrsC)), CentreNum, CentreNum, width = 2, fill = "Red")
#Paint the clock frame and pointer
def RefreshCanvas(self):
self.canvas.delete('all')
self.DrawBox()
self.DrawPointer()
self.DispCurrtTime()
#1秒钟运行一次RefreshCanvas
self.p_root.after(1000, self.RefreshCanvas)