二进制时钟

    由于chrome装了很多插件,没打开几个页面内存就“喝高”了。改用opera,并安装了个binary clock,蛮好玩的。刚写了下,总结之.

#-*- coding:UTF-8 -*-
from Tkinter import *
import time


class ClockCanvas(Canvas):
    def __init__(self, parent):
        Canvas.__init__(self, parent, bg='white')
        self.pack()
        #
        self.nPos_x, self.nPos_y = 30, 40       # 起始点
        self.nLength_x, self.nLength_y = 30,30  # 矩形长宽
        self.nSpan = 10        # 相邻的距离
        #
        self.rectID = [] # 控件ID列表
        #
        self.DrawClock()
        self.run()


    def DrawClock(self):
        for row in xrange(4):
            for col in xrange(3):
                #
                firstPos_x = self.nPos_x  + self.nLength_x*(col*3) + self.nSpan*(col*3)
                firstPos_y = self.nPos_y  + self.nLength_y*row + self.nSpan*row
                #
                secondPos_x = firstPos_x + self.nLength_x + self.nSpan
                secondPos_y = firstPos_y
                # first
                if col == 0 and row in (0,1):
                    self.rectID.append(0) # for easy to map after
                else:
                    self.rectID.append(self.create_rectangle(firstPos_x , firstPos_y, firstPos_x + self.nLength_x, firstPos_y + self.nLength_y, fill='gray'))
                    # second
                self.rectID.append(self.create_rectangle(secondPos_x, secondPos_y, secondPos_x + self.nLength_x, secondPos_y + self.nLength_y, fill='gray'))
    def run(self):
        nTime = time.localtime(time.time())
        nHour = nTime.tm_hour
        nMinute = nTime.tm_min
        nSecond = nTime.tm_sec
        timeList = [nHour / 10, nHour % 10,
                    nMinute / 10, nMinute % 10,
                    nSecond / 10, nSecond % 10]
        judgement = 16
        for row in xrange(4):
            judgement >>= 1
            for col in xrange(6):
                if col == 0 and row in (0,1):
                    pass
                else:
                    #
                    nResult = timeList[col] & judgement
                    if nResult:
                        self.itemconfig(self.rectID[col+row*6], fill='blue')
                    else:
                        self.itemconfig(self.rectID[col+row*6], fill='gray')
        self.after(250, self.run)

root = Tk()
root.title('Binary Clock')
ClockCanvas(root)
root.mainloop()

效果:

二进制时钟

转载于:https://my.oschina.net/ScottYang/blog/57303

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值