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的id()函数介绍
Feb 10 Python
9种python web 程序的部署方式小结
Jun 30 Python
Python爬取十篇新闻统计TF-IDF
Jan 03 Python
Python装饰器用法实例总结
Feb 07 Python
Python 创建新文件时避免覆盖已有的同名文件的解决方法
Nov 16 Python
python利用跳板机ssh远程连接redis的方法
Feb 19 Python
Python3.4学习笔记之类型判断,异常处理,终止程序操作小结
Mar 01 Python
解决Python对齐文本字符串问题
Aug 28 Python
使用Jupyter notebooks上传文件夹或大量数据到服务器
Apr 14 Python
Python-for循环的内部机制
Jun 12 Python
用Python进行websocket接口测试
Oct 16 Python
python cookie反爬处理的实现
Nov 01 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 Session及如何保持其不过期的方法
2015/08/18 PHP
php 静态属性和静态方法区别详解
2017/04/09 PHP
Aster vs KG BO3 第一场2.19
2021/03/10 DOTA
[全兼容哦]--实用、简洁、炫酷的页面转入效果loing
2007/05/07 Javascript
javascript 动态添加事件代码
2008/11/30 Javascript
浅析javascript闭包 实例分析
2010/12/25 Javascript
最简单的js图片切换效果实现代码
2011/09/24 Javascript
原生javascript实现图片按钮切换
2015/01/12 Javascript
深入剖析JavaScript中的函数currying柯里化
2016/04/29 Javascript
jQuery多级联动下拉插件chained用法示例
2016/08/20 Javascript
JS当前页面登录注册框,固定DIV,底层阴影的实例代码
2016/09/29 Javascript
AngularJS中的缓存使用
2017/01/11 Javascript
JS变量及其作用域
2017/03/29 Javascript
JavaScript实现的浏览器下载文件的方法
2017/08/09 Javascript
基于Vue、Vuex、Vue-router实现的购物商城(原生切换动画)效果
2018/01/09 Javascript
JS实现倒计时图文效果
2018/11/17 Javascript
JavaScript使用百度ECharts插件绘制饼图操作示例
2019/11/26 Javascript
Vue组件生命周期运行原理解析
2020/11/25 Vue.js
[05:40]DOTA2荣耀之路6:Wings最后进攻
2018/05/30 DOTA
python中numpy.zeros(np.zeros)的使用方法
2017/11/07 Python
python 读取摄像头数据并保存的实例
2018/08/03 Python
Python解决pip install时出现的Could not fetch URL问题
2019/08/01 Python
CSS Grid布局教程之网格单元格布局
2014/12/30 HTML / CSS
英国时尚女装购物网站:Missguided
2018/08/23 全球购物
如何通过jdbc调用存储过程
2012/04/19 面试题
行政管理专业推荐信
2013/11/02 职场文书
生产班组长岗位职责
2014/01/05 职场文书
档案接收函范文
2014/01/10 职场文书
个人党性剖析材料
2014/02/03 职场文书
房地产项目策划书
2014/02/05 职场文书
班级活动总结格式
2014/08/30 职场文书
教师求职自荐信
2015/03/26 职场文书
幼儿园六一主持词
2015/06/30 职场文书
新郎新娘致辞
2015/07/31 职场文书
2019年特色火锅店的创业计划书模板
2019/08/28 职场文书
postgreSQL数据库基础知识介绍
2022/04/12 PostgreSQL