自己写的代码:时钟

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值