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中操作文件之truncate()方法的使用教程
May 25 Python
详解Python的Django框架中的模版相关知识
Jul 15 Python
Python的Django框架下管理站点的基本方法
Jul 17 Python
你应该知道的python列表去重方法
Jan 17 Python
浅谈python的dataframe与series的创建方法
Nov 12 Python
Python实现的银行系统模拟程序完整案例
Apr 12 Python
python GUI编程(Tkinter) 创建子窗口及在窗口上用图片绘图实例
Mar 04 Python
python 实现PIL模块在图片画线写字
May 16 Python
Python小白不正确的使用类变量实例
May 29 Python
python mysql中in参数化说明
Jun 05 Python
基于Python3读写INI配置文件过程解析
Jul 23 Python
python实现自动清理文件夹旧文件
May 10 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
人族 Terran 魔法与科技
2020/03/14 星际争霸
php数组函数序列之array_values() 获取数组元素值的函数与方法
2011/10/30 PHP
解析php常用image图像函数集
2013/06/24 PHP
一个简单的javascript类定义例子
2009/09/12 Javascript
Extjs4中的分页应用结合前后台
2013/12/13 Javascript
js判断上传文件类型判断FileUpload文件类型代码
2014/05/20 Javascript
JavaScript通过filereader接口读取文件
2017/05/10 Javascript
nodejs项目windows下开机自启动的方法
2017/11/22 NodeJs
Node.JS枚举统计当前文件夹和子目录下所有代码文件行数
2019/08/23 Javascript
JavaScript 空间坐标的使用
2020/08/19 Javascript
Vue循环中多个input绑定指定v-model实例
2020/08/31 Javascript
一篇超完整的Vue新手入门指导教程
2020/11/18 Vue.js
python实现的一个p2p文件传输实例
2014/06/04 Python
Tensorflow 自带可视化Tensorboard使用方法(附项目代码)
2018/02/10 Python
Python中Numpy ndarray的使用详解
2019/05/24 Python
Python分布式进程中你会遇到的问题解析
2019/05/28 Python
python和c语言的主要区别总结
2019/07/07 Python
python3应用windows api对后台程序窗口及桌面截图并保存的方法
2019/08/27 Python
Django中提示消息messages的设置方式
2019/11/15 Python
Python3连接Mysql8.0遇到的问题及处理步骤
2020/02/17 Python
python实现飞机大战项目
2020/03/11 Python
IDLE下Python文件编辑和运行操作
2020/04/25 Python
Python getsizeof()和getsize()区分详解
2020/11/20 Python
Python自动化测试基础必备知识点总结
2021/02/07 Python
canvas之自定义头像功能实现代码示例
2017/09/29 HTML / CSS
中国高端鲜花第一品牌:roseonly(一生只送一人)
2017/02/12 全球购物
澳大利亚婴儿喂养品牌:Cherub Baby
2018/11/01 全球购物
什么是smarty? Smarty的优点是什么?
2013/08/11 面试题
小学教师岗位职责
2013/11/25 职场文书
大专生求职信
2014/06/29 职场文书
学校师德师风自我剖析材料
2014/09/29 职场文书
离婚协议书格式
2014/11/21 职场文书
教务处教学工作总结
2015/08/10 职场文书
学习委员竞选稿
2015/11/20 职场文书
react 路由Link配置详解
2021/11/11 Javascript
世界十大狙击步枪排行榜
2022/03/20 杂记