Python+Tkinter绘制一个数字时钟
作者:严长生 时间:2021-08-06 05:24:05
Tkinter 实现上述功能并不复杂,只要使用 Tkinter 的相关组件和一些简单的逻辑处理即可,在编写这个案例的过程中大家要做到温故而知新。
程序代码如下所示:
from tkinter import *
from time import strftime
root = Tk()
root.geometry('500x350+300+300')
root.iconbitmap('C:/Users/Administrator/Desktop/C语言中文网logo.ico')
root.title("C语言中文网出品")
# 设置文本标签
lb = Label(root, font=("微软雅黑", 50, "bold"), bg='#87CEEB', fg="#B452CD")
lb.pack(anchor="center", fill="both", expand=1)
# 定义一个mode标志
mode = 'time'
# 定义显示时间的函数
def showtime():
if mode == 'time':
#时间格式化处理
string = strftime("%H:%M:%S %p")
else:
string = strftime("%Y-%m-%d")
lb.config(text=string)
# 每隔 1秒钟执行time函数
lb.after(1000, showtime)
# 定义鼠标处理事件,点击时间切换为日期样式显示
def mouseClick(event):
global mode
if mode == 'time':
# 点击切换mode样式为日期样式
mode = 'date'
else:
mode = 'time'
lb.bind("<Button>", mouseClick)
# 调用showtime()函数
showtime()
# 显示窗口
mainloop()
程序运行结果如下:
图1:简单的数字时钟
通过上述代码就实现了一个简单的数字时钟,是不是非常的简单。
补充
除了数字时钟,Tkinter还能绘制一个简易的钟表
具体实现代码如下:
# coding:utf-8
from tkinter import *
import math,time
def points():
for i in range(1,13):
x = 200 + 130*math.sin(2*math.pi*i/12)
y = 200 - 130*math.cos(2*math.pi*i/12)
canvas.create_text(x,y,text=i)
def createline(radius,line_width,rad):
global List
global i
List = []
x = 200+radius*math.sin(rad)
y = 200-radius*math.cos(rad)
i=canvas.create_line(200,200,x,y,width=line_width)
List.append(i)
root = Tk()
root.resizable(0,0)
canvas = Canvas(root,width=400,height=500,bd=0,highlightthickness=0)
canvas.pack()
canvas.create_oval(50,50,350,350)
points()
while 1:
tm=time.localtime()
t=time.asctime(tm)
t_hour=0
if tm.tm_hour<=12:
t_hour=tm_hour
else:
t_hour=tm.tm_hour-12
rad1=2*math.pi*(t_hour+tm.tm_min/60)/12
rad2=2*math.pi*(tm.tm_min+tm.tm_sec/60)/60
rad3=2*math.pi*tm.tm_sec/60
createline(50,6,rad1,)
createline(90,3,rad2)
createline(120,1,rad3)
l=canvas.create_text(170,450,text=t)
root.update()
time.sleep(1)
for item in List:
canvas.delete(item)
canvas.delete(l)
root.update()
mainloop()
效果如下
来源:http://c.biancheng.net/tkinter/project-case.html
标签:Python,Tkinter,时钟
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python自动化操作之动态验证码、滑动验证码的降噪和识别
2023-03-26 02:48:28
![](https://img.aspxhome.com/file/2023/2/84922_0s.png)
Python如何遍历numpy数组
2023-04-15 00:40:17
在python的类中动态添加属性与生成对象
2021-04-27 00:26:59
asp如何制作一个弹出式的调查窗口?
2010-06-29 21:24:00
python matplotlib实现条形图的填充效果
2022-03-12 07:55:20
![](https://img.aspxhome.com/file/2023/1/79561_0s.jpg)
asp如何显示最后十名来访者信息?
2010-06-09 18:45:00
白鸦:内容设计,初始内容
2008-03-04 16:23:00
python获取一组数据里最大值max函数用法实例
2022-01-28 00:02:44
在Windows服务器下用Apache和mod_wsgi配置Python应用的教程
2021-10-28 22:36:17
python 按不同维度求和,最值,均值的实例
2023-06-12 15:08:23
![](https://img.aspxhome.com/file/2023/9/59479_0s.jpg)
Python实战之手写一个搜索引擎
2023-07-11 21:16:49
![](https://img.aspxhome.com/file/2023/3/67323_0s.jpg)
Django实现文件上传下载功能
2023-06-13 20:09:46
![](https://img.aspxhome.com/file/2023/9/85009_0s.gif)
浅谈python中的__init__、__new__和__call__方法
2023-01-05 21:21:03
python爬虫中PhantomJS加载页面的实例方法
2021-09-12 09:44:33
![](https://img.aspxhome.com/file/2023/7/99587_0s.png)
在SQL 2005中实现循环每一行做一定的操作
2009-03-05 14:57:00
Python 一键获取百度网盘提取码的方法
2023-11-30 06:13:49
![](https://img.aspxhome.com/file/2023/6/86456_0s.gif)
用Python程序抓取网页的HTML信息的一个小实例
2021-02-14 12:52:05
![](https://img.aspxhome.com/file/2023/1/103701_0s.png)
python探索之BaseHTTPServer-实现Web服务器介绍
2023-06-03 03:14:34
编程经验点滴 动态SQL的拼接技巧
2012-11-30 20:03:58
ASP编写计数器的优化方法
2009-01-21 19:46:00