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

yipeiwu_com5年前Python基础

本文实例讲述了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('www.jb51.net 时钟')
  clock = Clock(root, 200, 200, 400, 400, 150)
  clock.drawClockDial()
  clock.run()
  root.mainloop()

待解决的bug:

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

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

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

相关文章

Python单例模式实例详解

本文实例讲述了Python单例模式。分享给大家供大家参考,具体如下: 单例模式:保证一个类仅有一个实例,并提供一个访问他的全局访问点。 实现某个类只有一个实例的途径: 1,让一个全局变量...

Python中多个数组行合并及列合并的方法总结

采用numpy快速将两个矩阵或数组合并成一个数组: import numpy as np 数组 a = [[1,2,3],[4,5,6]] b = [[1,1,1],[2,2,...

python super的使用方法及实例详解

功能 super功能:super函数是子类用于调用父类(超类)的一个方法。 用法 1.在子类 __init__() 方法中正确的初始化父类,保证相同的基类只初始化一次。 2.覆盖特...

如何通过Django使用本地css/js文件

这篇文章主要介绍了如何通过Django使用本地css/js文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 在网上看了很多说Djan...

Python实现繁体中文与简体中文相互转换的方法示例

本文实例讲述了Python实现繁体中文与简体中文相互转换的方法。分享给大家供大家参考,具体如下: 工作中需要将繁体中文转换成简体中文 上网找了些资料,发现这个包最方便:https://g...