本文共 1834 字,大约阅读时间需要 6 分钟。
今天用python的tkinter模块写了个小时钟,界面很简单,而且是单线程的,拖动窗口时时间不能刷新(以后改进吧)。
运行环境:Python2
from Tkinter import *import timeimport mathimport threaddef drawclock(canvas): x = 200 y = 200 width = 150 + 10 for i in range(0,12): arc = 2.0 * math.pi / 12 * i new_x = x + width * math.sin(arc) new_y = y - width * math.cos(arc) canvas.create_text(new_x,new_y,text = str(i))def drawpointer(canvas,hour,minute,second): x = 200 y = 200 hour_width = 70 minute_width = 90 second_width = 120 hour_arc = 2.0 * math.pi * hour / 12 minute_arc = 2.0 * math.pi * minute / 60 second_arc = 2.0 * math.pi * second / 60 canvas.create_line(x,y,x+hour_width * math.sin(hour_arc),y-hour_width*math.cos(hour_arc)) canvas.create_line(x,y,x+minute_width*math.sin(minute_arc),y - minute_width*math.cos(minute_arc)) canvas.create_line(x,y,x+second_width*math.sin(second_arc),y - second_width*math.cos(second_arc))def showtime(app,canvas): while 1: mytime = '' localtime = time.localtime() # hour hour = localtime[3] # minute minute = localtime[4] # second second = localtime[5] mytime = str(hour) + ':' + str(minute) + ':' + str(second) #print mytime canvas.create_rectangle(0,0,400,400,fill='white') canvas.create_oval(50,50,350,350) canvas.create_text(200,380,text=str(mytime),fill='red') drawclock(canvas) drawpointer(canvas,hour,minute,second) app.update() time.sleep(1)def main(): app = Tk() #set the size of the window app.wm_maxsize(400,400) app.wm_minsize(400,400) app.wm_title('Python Clock') canvas = Canvas(app,width=400,height=400) canvas.pack() thread.start_new_thread(showtime,(app,canvas)) app.mainloop()if __name__ == "__main__": main()
转载地址:http://jvuws.baihongyu.com/