Python代码实现二进制时钟的方法下载,python二进制,这是一篇Python代码
Python代码实现二进制时钟的方法下载,python二进制,这是一篇Python代码
这是一篇Python代码实现二进制时钟的方法源码实例文章。是我闲来无事用python代码写了一个二进制时钟,对于一个理科不太好的人来说要想弄明白二进制什么的真是困难的很。python读写二进制文件,python十进制转二进制等等转换真是让我头疼了好久,不过经过努力我还是写出了这个代码,实现了效果。大家看看我的python代码吧。
杰瑞的二进制时钟实现效果:
杰瑞的二进制时钟完整Python代码部分:
#!usr/bin/env python#当用户没有将python装在默认的/usr/bin路径里时,在代码前加上这句话后,系统会到env设置里查找python的安装路径,再调用对应路径下的解释器程序完成操作。'''python版本: 2.7.1'''#导入模块方法import timefrom Tkinter import *from threading import Thread#www.iplaypy.comclass BinaryClock(Thread): '''the class of the binary clcock''' def __init__(self): Thread.__init__(self) self.root=Tk() self.now_time=StringVar() self.hour=[] self.minute=[] self.seconds=[] self.rect=[] self.rect_state=[] self.draw() def draw(self): self.root.title("Jerry's Binary Clock") self.canvas=Canvas(self.root, width=300, height=150, bg='white') self.label=Label(self.root, text='Now Time:') self.entry=Entry(self.root, textvariable=self.now_time) self.label.place(x=12, y=2) self.entry.pack() for a in range(5): self.canvas.create_line((a+1)*50, 0, (a+1)*50, 150) for b in range(2): self.canvas.create_line(0, (b+1)*50, 300, (b+1)*50) self.rect.append(self.canvas.create_rectangle((0,0,50,50), fill='white')) self.rect.append(self.canvas.create_rectangle((0,50,50,100), fill='white')) self.rect.append(self.canvas.create_rectangle((0,100,50,150), fill='white')) self.rect.append(self.canvas.create_rectangle((50,0,100,50), fill='white')) self.rect.append(self.canvas.create_rectangle((50,50,100,100), fill='white')) self.rect.append(self.canvas.create_rectangle((50,100,100,150), fill='white')) self.rect.append(self.canvas.create_rectangle((100,0,150,50), fill='white')) self.rect.append(self.canvas.create_rectangle((100,50,150,100), fill='white')) self.rect.append(self.canvas.create_rectangle((100,100,150,150), fill='white')) self.rect.append(self.canvas.create_rectangle((150,0,200,50), fill='white')) self.rect.append(self.canvas.create_rectangle((150,50,200,100), fill='white')) self.rect.append(self.canvas.create_rectangle((150,100,200,150), fill='white')) self.rect.append(self.canvas.create_rectangle((200,0,250,50), fill='white')) self.rect.append(self.canvas.create_rectangle((200,50,250,100), fill='white')) self.rect.append(self.canvas.create_rectangle((200,100,250,150), fill='white')) self.rect.append(self.canvas.create_rectangle((250,0,300,50), fill='white')) self.rect.append(self.canvas.create_rectangle((250,50,300,100), fill='white')) self.rect.append(self.canvas.create_rectangle((250,100,300,150), fill='white')) self.canvas.pack() def dec2bin(self, hour, minute, seconds): self.hour=list(bin(hour)) self.hour=self.hour[2:] while len(self.hour)<6: self.hour.insert(0, '0') self.minute=list(bin(minute)) self.minute=self.minute[2:] while len(self.minute)<6: self.minute.insert(0, '0') self.seconds=list(bin(seconds)) self.seconds=self.seconds[2:] while len(self.seconds)<6: self.seconds.insert(0, '0') self.rect_state=self.hour+self.minute+self.seconds def run(self): time.sleep(1) while True: self.now_time.set(str(time.strftime(' %Y-%m-%d %H:%M:%S', time.localtime(time.time())))) hour=int(time.localtime(time.time()).tm_hour) minute=int(time.localtime(time.time()).tm_min) seconds=int(time.localtime(time.time()).tm_sec) self.dec2bin(hour, minute, seconds) for i in range(18): if self.rect_state[i] == '0': self.canvas.itemconfig(self.rect[i], fill='white') elif self.rect_state[i] == '1': self.canvas.itemconfig(self.rect[i], fill='black') time.sleep(1)if __name__ == '__main__': binary_clock=BinaryClock() binary_clock.setDaemon(True) binary_clock.start() binary_clock.root.mainloop()
Python代码实现二进制时钟的方法文中用到了Python time时间模块、Tkinter、Python threading多线程模块等方法,代码比较长,我也想不到哪里可以精简了。但总算是可以用python代码实现想要的效果感觉还是不错的。代码贴上来比较乱,如果想拿出用的小伙伴可能需要自己将python代码下载后再自己整理一下再使用了。
编橙之家文章,
相关内容
- Python FMM算法的中文分词器实现方法源码,pythonfmm,这是
- Python3.2模拟webqq登录方法源代码分享下载,python3.2webq
- Python实现viterbi(维特比)算法原理流程是什么样的,pyt
- Python代码解决windows平台锁定鼠标键盘输入操作的方法,
- 用Python代码完成组合求解问题的案例,python案例,Pytho
- Python截屏工具函数方法源代码分享,python截屏,最近使用
- 用Python编写的一个计算生肖和星座的函数代码,python生
- 如何用Python实现enum功能源码分享,python实现enum源码,本
- 一个Python编写的彩票机选工具实例分析,python彩票机
- 用python查询判读系统进程是否存在的方法,python判读
评论关闭