Python Tkinter模块实现时钟功能应用示例


Posted in Python onJuly 23, 2018

本文实例讲述了Python Tkinter模块实现时钟功能。分享给大家供大家参考,具体如下:

本机测试效果:

Python Tkinter模块实现时钟功能应用示例

完整代码:

# coding=utf-8
from Tkinter import *
import _tkinter
import math
import time
from threading import Thread
class Clock:
  def __init__(self, master, x, y, width, height, radius):
    '''
    :param master: 父窗口
    :param x: 时钟中心点的x坐标
    :param y: 时钟中心点的y坐标
    :param width: 画布的宽度
    :param height: 画布的高度
    :param radius: 时钟钟盘的半径
    '''
    self.centerX = x
    self.centerY = y
    self.radius = radius
    self.canvas = Canvas(master, width=width, height=height) # 画布
    self.canvas.pack()
    self.canvas.create_oval(
      x - radius,
      y - radius,
      x + radius,
      y + radius) # 画钟框
    self.id_lists = []
    self.hourHandRadius = self.radius * 1.0 / 4  # 指针长度
    self.minHandRadius = self.radius * 2.0 / 3  # 分针长度
    self.secHandRadius = self.radius * 4.0 / 5  # 秒针长度
    self.timeVar = StringVar()
    # self.timeVar.set('')
    self.timeLabel = Label(self.canvas.master, textvariable=self.timeVar)
    self.timeLabel.pack(side=BOTTOM)
    #self.canvas.master.protocol('WM_DELETE_WINDOW', self.canvas.master.destroy)
  def __del__(self):
    self._deleteItems(self.id_lists)
  # 绘制时钟钟盘
  def drawClockDial(self):
    # 绘制钟盘上的数字1-12
    r = self.radius - 15
    for i in range(1, 13):
      rad = 2 * math.pi / 12 * i
      x = self.centerX + math.sin(rad) * r
      y = self.centerY - math.cos(rad) * r
      id = self.canvas.create_text(x, y, text=str(i))
      self.id_lists.append(id)
    # 绘制钟盘上的刻度
    r1 = self.radius - 5
    r2 = self.radius
    for i in range(1, 61):
      rad = 2 * math.pi / 60 * i
      x1, y1 = self._getPosByRadAndRadius(rad, r1)
      x2, y2 = self._getPosByRadAndRadius(rad, r2)
      id = self.canvas.create_line(x1, y1, x2, y2)
      self.id_lists.append(id)
  # 显示时间
  def showTime(self, tm):
    hour = tm.tm_hour % 12
    min = tm.tm_min
    sec = tm.tm_sec
    sec_rad = 2 * math.pi / 60 * sec
    min_rad = 2 * math.pi / 60 * (min + sec / 60.0)
    hour_rad = 2 * math.pi / 12 * (hour + min / 60.0)
    timeStr = '当前时间: %d-%02d-%02d %02d:%02d:%02d' % (
      tm.tm_year, tm.tm_mon, tm.tm_mday, hour, min, sec)
    self.timeVar.set(timeStr)
    hour_id = self._drawLine(hour_rad, self.hourHandRadius, 6)
    min_id = self._drawLine(min_rad, self.minHandRadius, 4)
    sec_id = self._drawLine(sec_rad, self.secHandRadius, 3)
    return (hour_id, min_id, sec_id)
  def run(self):
    def _run():
      while True:
        tm = time.localtime()
        id_lists = self.showTime(tm)
        self.canvas.master.update()
        time.sleep(1)
        self._deleteItems(id_lists)
    thrd = Thread(target=_run) # 创建新的线程
    thrd.run() # 启动线程
  def _drawLine(self, rad, radius, width):
    x, y = self._getPosByRadAndRadius(rad, radius)
    id = self.canvas.create_line(
      self.centerX, self.centerY, x, y, width=width)
    return id
  def _getPosByRadAndRadius(self, rad, radius):
    x = self.centerX + radius * math.sin(rad)
    y = self.centerY - radius * math.cos(rad)
    return (x, y)
  def _deleteItems(self, id_lists):
    for id in id_lists:
      try:
        self.canvas.delete(id)
      except BaseException:
        pass
if __name__ == '__main__':
  root = Tk()
  root.title('3water.com 时钟')
  clock = Clock(root, 200, 200, 400, 400, 150)
  clock.drawClockDial()
  clock.run()
  root.mainloop()

待解决的bug:

关闭程序的时候,会出现如下的错误:

Python Tkinter模块实现时钟功能应用示例

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
Python之eval()函数危险性浅析
Jul 03 Python
Python中模块pymysql查询结果后如何获取字段列表
Jun 05 Python
python kmeans聚类简单介绍和实现代码
Feb 23 Python
Django实现全文检索的方法(支持中文)
May 14 Python
python实现基于信息增益的决策树归纳
Dec 18 Python
PyQt5 多窗口连接实例
Jun 19 Python
Python : turtle色彩控制实例详解
Jan 19 Python
Python 实现日志同时输出到屏幕和文件
Feb 19 Python
Python基于Dlib的人脸识别系统的实现
Feb 26 Python
关于python 跨域处理方式详解
Mar 28 Python
python实现俄罗斯方块小游戏
Apr 24 Python
python中np是做什么的
Jul 21 Python
python定向爬虫校园论坛帖子信息
Jul 23 #Python
python实现图片批量压缩程序
Jul 23 #Python
python中的插值 scipy-interp的实现代码
Jul 23 #Python
Flask框架URL管理操作示例【基于@app.route】
Jul 23 #Python
python中的turtle库函数简单使用教程
Jul 23 #Python
Flask框架配置与调试操作示例
Jul 23 #Python
python实现时间o(1)的最小栈的实例代码
Jul 23 #Python
You might like
在PHP中操作Excel实例代码
2010/04/29 PHP
学习php设计模式 php实现装饰器模式(decorator)
2015/12/07 PHP
PHP使用内置函数生成图片的方法详解
2016/05/09 PHP
PHP生成各种随机验证码的方法总结【附demo源码】
2017/06/05 PHP
php使用flock阻塞写入文件和非阻塞写入文件的实例讲解
2017/07/10 PHP
PHP延迟静态绑定的深入讲解
2018/04/02 PHP
浅谈PHP封装CURL
2019/03/06 PHP
YII2框架中查询生成器Query()的使用方法示例
2020/03/18 PHP
提高 DHTML 页面性能
2006/12/25 Javascript
Javascript中获取出错代码所在文件及行数的代码
2010/09/23 Javascript
javascript smipleChart 简单图标类
2011/01/12 Javascript
javascript异步编程的4种方法
2014/02/19 Javascript
删除条目时弹出的确认对话框
2014/06/05 Javascript
javascript实现checkBox的全选,反选与赋值
2015/03/12 Javascript
深入分析Javascript跨域问题
2015/04/17 Javascript
下雪了 javascript实现雪花飞舞
2020/08/02 Javascript
JavaScript易错知识点整理
2016/12/05 Javascript
D3.js中强制异步文件读取同步的几种方法
2017/02/06 Javascript
利用ES6语法重构React组件详解
2017/03/02 Javascript
vue中appear的用法
2017/08/17 Javascript
vue用addRoutes实现动态路由的示例
2017/09/15 Javascript
微信小程序 按钮滑动的实现方法
2017/09/27 Javascript
详解vue.js数据传递以及数据分发slot
2018/01/20 Javascript
layui 上传插件 带预览 非自动上传功能的实例(非常实用)
2019/09/23 Javascript
electron 安装,调试,打包的具体使用
2019/11/06 Javascript
Angular+ionic实现折叠展开效果的示例代码
2020/07/29 Javascript
python使用webbrowser浏览指定url的方法
2015/04/04 Python
使用Python对IP进行转换的一些操作技巧小结
2015/11/09 Python
Python使用try except处理程序异常的三种常用方法分析
2018/09/05 Python
德国自行车商店:Tretwerk
2019/06/21 全球购物
周鸿祎:教你写创业计划书
2013/12/30 职场文书
乡镇爱国卫生月活动总结
2014/06/25 职场文书
叶问观后感
2015/06/15 职场文书
nginx 反向代理之 proxy_pass的实现
2021/03/31 Servers
Pyqt5将多个类组合在一个界面显示的完整示例
2021/09/04 Python
动作冒险《Hell Is Us》将采用虚幻5 消灭怪物探索王国
2022/04/13 其他游戏