博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python小时钟
阅读量:4294 次
发布时间:2019-05-27

本文共 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/

你可能感兴趣的文章
Vue2 速通
查看>>
孤蝉唱夏
查看>>
MQTT 基础速通
查看>>
Golang 基础语法速通
查看>>
ES6 必会总结
查看>>
Git 基操
查看>>
LeetCode - 中等 - 三数之和 - ab剪枝
查看>>
2021年4月前端造火箭副本掉落
查看>>
记 NEST 的一次简单使用
查看>>
使用已有的ceph集群
查看>>
在宿主机(物理机)内部访问不了macvtap的虚拟机
查看>>
OpenSSH 用户枚举漏洞(CVE-2018-15919)
查看>>
Apache HTTP Server 安全漏洞(CVE-2019-0211)
查看>>
crontab不执行的解决。
查看>>
检查硬盘告警的脚本
查看>>
安装openssh提示headers missing的解决方法
查看>>
安装openssh的版本不一致问题解决
查看>>
关于openssh连接时的尝试
查看>>
远程硬盘资源监控通用脚本
查看>>
一个ubuntu服务器的网络不能问题
查看>>