Tkinter应用之时钟

效果:

代码:

# coding=utf-8
from Tkinter import *
import _tkinter
import math
import time

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.timeLabel = None
        #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.timeLabel = self.canvas.create_text(self.centerX, self.radius * 3, text=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, self.timeLabel)

    def run(self):
        while True:
            tm = time.localtime()
            id_lists = self.showTime(tm)
            self.canvas.master.update()
            time.sleep(1)
            self._deleteItems(id_lists)

    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(‘时钟‘)
    clock = Clock(root, 200, 200, 400, 500, 150)
    clock.drawClockDial()
    clock.run()
    root.mainloop()

待解决的bug:

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

时间: 2024-08-25 19:46:12

Tkinter应用之时钟的相关文章

python+tkinter制作一个可自定义的动态时钟及详细解释,珍藏版

1.效果图 2.完整代码 #第1步:导出模块 from tkinter import * import math,time #第2步:定义窗口的相关设置 root = Tk() root.title("a DIY clock") #定义窗口名称 root.geometry("1020x800+500+0") #位置坐标=500,0=就是顶格向右水平移动500 root.configure(bg='pink') #定义窗口的背景颜色 #第3步:定义全局变量 globa

Python tkinter 控件更新信息

下面几个例子关于tkinter界面更新的,简单易懂,分享一下. 例子_1: 代码_1: from tkinter import Tk, Checkbutton, Label from tkinter import StringVar, IntVar root = Tk() text = StringVar() text.set('old') status = IntVar() def change(): if status.get() == 1: # if clicked text.set('n

GUI编程3 tkinter

知识内容: 1.Tkinter介绍 2.Tkinter实战1-用户登录界面 3.Tkinter实战2-选择类组件综合应用 4.Tkinter实战3-简单文本编辑器 5.Tkinter实战4-简单画图程序 6.Tkinter实战5-电子时钟 一.Tkinter介绍 二.Tkinter实战1-用户登录界面 三.Tkinter实战2-选择类组件综合应用 四.Tkinter实战3-简单文本编辑器 五.Tkinter实战4-简单画图程序 六.Tkinter实战5-电子时钟 原文地址:https://www

Python自制小时钟,并转换为exe可执行程序详解

一,简介Python写完程序,要靠命令来执行太LOW,太低调了,还不华丽了. 再说别人的电脑,都没有Python库,怎么执行,还能不能愉快的一起玩耍了. 所以哪怕只会写一个HelloWorld,也要弄成exe程序,方便伟大的代码传播事业. 需要用到工具:pyInstaller.pypiwin32. 二,安装pyInstaller 1.打开cmd窗口,执行命令: pip install pyinstaller Installing collected packages: future, pefil

门控时钟-理论分析 ---- 转载

转载自:http://www.chipsbank.com/news_detail/newsId=123.html 门控的基本要求: 1. 所需要的沿(对于正沿触发的寄存器是正沿,对于负沿触发的寄存器是负沿)不增加,不减少: 1. 不会产生毛刺: 1. 使用后功耗要能够降低: 1. 最好面积还会减小. 1. 上升沿触发的门控时钟的结构研究:应用与上升沿触发的寄存器的门控. 1. 直接与门结构: 1. 高电平使能Latch + 与门结构: 1. 低电平使能Latch + 与门结构: 1. 波形研究:

Tkinter,label内容随多选框变化

当我们改变一个组件后,其他组件一起变化怎么做呢?下面是一个例子 from tkinter import Tk, Checkbutton, Label from tkinter import StringVar, IntVar root = Tk() text = StringVar() text.set('old') status = IntVar() def change(): if status.get() == 1:   # if clicked text.set('new') else:

【JAVA语言程序设计基础篇】--图形-- 三种时钟--增强对类的理解和应用

1.显示任意时间时钟 2.设置三个可见性属性 分别表示时针,分针,秒针的可见性 3.一个精细的时钟 主类:StillClock @SuppressWarnings("serial") class DetailedClock extends JPanel { private int hour; private int minute; private int second; protected int xCenter, yCenter; protected int clockRadius;

第三部分:S5PV210_时钟部分_1

时钟部分 (1)时钟域 S5PV210一共有三个时钟域:MSYS,DSYS,PSYS MSYS:(main system)主时钟域,包括CPU,DDR内存条,IROM和IRAM等 DSYS:(display system)显示时钟域,就是一般的和视频有关的就在这个时钟域中,如HDMI,TVENC... PSYS:(peripheral system)外围时钟域,就是GPIO接口,I2C接口,UART接口等这些外围设备就在这个时钟域上. 每个时钟域通过一条BRG(异步总线的桥梁)连接在一起. (2

Android 开发第七弹:简易时钟(秒表)

本文承接,Android 开发第五弹:简易时钟(闹钟) 和 Android 开发第六弹:简易时钟(计时器),这一部分是关于秒表的. 布局 同样是新建一个类(StopWatchView)并扩展自LinearLayout,并将其用作布局. <myapplication.nomasp.com.clock.StopWatchView android : id = "@+id/tabStopWatch" android : layout_width = "match_parent