Python--面向对象编程--时钟实例开发
- 在学习python面向对象编程的时候,心血来潮,决定写一个时钟模型来玩玩,所以就有了现在这个小玩意,不过python这个东西确实是挺好玩的
- 方法;运用python的tkinter库开发图形化时钟程序
- 时钟启动时以系统的时间为当前的时间
- 时钟有时针、分针和秒针
- 表盘可以切换为程序绘制的或基于图片的样式
 
- 对象分析
-   指针:
- 坐标集(针头坐标和针头坐标)
- 当前位置坐标(绘制各种针的当前位置)
- 颜色,粗细,当前指针的图形ID(把前一个id删除),画布对象
- 当前指针的上级指针,上下级指针的行走关系(分针走一格,秒针走多少格),行走一格,
- 设置起始位置,绘制指针,删除指针
 
-   基本表盘
- 所有刻度线起讫点坐标(绘制每一个刻度线)
- 画布引用
- 绘制刻度线,删除刻度线
 
-   图像表盘
- 刻度图
- 图像名称
- 绘制id,绘制删除指定图像文件
 
- 装饰图
- 图像名称
- 绘制ID,删除绘制指定图像文件
 
 
- 刻度图
- 钟表
- 三种类型的指针
- 表盘
- 切换表盘的按钮
- 设置图形表盘图像
- 切换表盘(按钮)
 
 
-   指针:
- d代码实现
-   
#coding=gbk 
 # -*- encoding:utf-8 -*- import time,datetime
 import math
 import itertools
 import tkinter
 import threading def get_clock_step(base_pntr,long_pntr):
 pos = []
 for i in range(60):
 pos.append((base_pntr[0]+long_pntr*math.cos(i*math.pi/30),
 base_pntr[0]+long_pntr*math.sin(i*math.pi/30)))
 return pos[45:] + pos[:45] def gen_i_pos(c_pntr,long_pntr):
 for i,p in enumerate(get_clock_step(c_pntr,long_pntr)):
 yield i,p class Pointer: def __init__(self,c_pntr,long_pntr,cvns,scale=None,super_pntr=None,width=1,fill='black'):
 # 参数说明:
 # c_pntr: 表盘的中心坐标;
 # long_pntr: 表针长;
 # cvns: 画布引用;
 # scale: 指针行走比例;
 # super_pntr: 上级指针;
 # width: 指针粗细;
 # fill: 指针颜色;
 self.pos_iter = itertools.cycle(gen_i_pos(c_pntr,long_pntr))
 self.scale = scale
 self.cvns = cvns
 self.crrnt_pos = self.pos_iter.__next__()[1]
 self.c_pntr = c_pntr
 self.super_pntr = super_pntr
 self.id_pntr = None
 self.width = width
 self.fill = fill def draw(self):
 self.id_pntr = cvns.create_line(self.c_pntr,self.crrnt_pos,width=self.width,fill=self.fill) def walk_step(self):
 self.delete()
 self.draw()
 i,self.crrnt_pos = self.pos_iter.__next__()
 if self.super_pntr and self.scale and (i + 1) % self.scale == 0:
 self.super_pntr.walk_step() def delete(self):
 if self.id_pntr:
 self.cvns.delete(self.id_pntr) def set_start(self,steps):
 for i in range(steps-1):
 self.pos_iter.__next__()
 self.crrnt_pos = self.pos_iter.__next__()[1] class PlateImg: def __init__(self,c_pntr,clock_r,cvns,img):
 self.cvns = cvns
 self.clock_r = clock_r
 self.c_pntr = c_pntr
 self.img = img
 self.pid = None
 self.draw() def draw(self):
 self.im = tkinter.PhotoImage(file=self.img)
 self.pid = self.cvns.create_image(self.c_pntr,image = self.im) def delete(self):
 if self.pid:
 self.cvns.delete(self.pid) def set_img(self,img):
 self.img = img
 self.delete()
 self.draw() class InImg(PlateImg):
 def draw(self):
 x = self.c_pntr[0]
 y = self.c_pntr[0] + self.clock_r / 5
 self.im = tkinter.PhotoImage(file=self.img)
 self.pid = self.cvns.create_image(x,y,image = self.im) class CustomPlate:
 def __init__(self,c_pntr,clock_r,cvns,imgp='platex.gif',imgi='flowersx.gif'):
 self.pi = PlateImg(c_pntr,clock_r,cvns,imgp)
 self.ii = InImg(c_pntr,clock_r,cvns,imgi) def set_img(self,imgp,imgi):
 self.pi.set_img(imgp)
 self.ii.set_img(imgi) def delete(self):
 self.pi.delete()
 self.ii.delete() class Plate: def __init__(self,c_pntr,clock_r,cvns,long=10):
 self.pos_a = get_clock_step(c_pntr,clock_r - long)
 self.pos_b = get_clock_step(c_pntr,clock_r)
 self.cvns = cvns
 self.plates = []
 self.draw() def draw(self):
 for i,p in enumerate(zip(self.pos_a,self.pos_b)):
 width = 5 if (i + 1) % 5 == 0 else 3
 pid = self.cvns.create_line(*p,width=width)
 self.plates.append(pid) def delete(self):
 if self.plates:
 for item in self.plates:
 self.cvns.delete(item) class MyClock: def __init__(self,base_pntr,clock_r,cvns):
 self.c_pntr = base_pntr
 self.clock_r = clock_r
 self.cvns = cvns
 self.plate = Plate(base_pntr,clock_r,self.cvns)
 h,m,s = self.start_time()
 self.h_pntr = Pointer(base_pntr,3 * clock_r // 5,self.cvns,width=5,fill='blue')
 self.m_pntr = Pointer(base_pntr,4 * clock_r // 5,self.cvns,12,super_pntr=self.h_pntr,width=3,fill='green')
 self.h_pntr.set_start(h * 5)
 self.m_pntr.set_start(m)
 self.h_pntr.walk_step()
 self.m_pntr.walk_step()
 self.s_pntr = Pointer(base_pntr,clock_r-5,self.cvns,60,super_pntr=self.m_pntr,fill='red')
 self.s_pntr.set_start(s) def chg_plate(self):
 self.plate.delete()
 if isinstance(self.plate,CustomPlate):
 self.plate = Plate(self.c_pntr,self.clock_r,self.cvns)
 else:
 self.plate = CustomPlate(self.c_pntr,self.clock_r,self.cvns)
 self.h_pntr.delete()
 self.h_pntr.draw()
 self.m_pntr.delete()
 self.m_pntr.draw() def set_img(self,imgp,imgi):
 if not isinstance(self.plate,CustomPlate):
 self.chg_plate()
 self.plate.set_img(imgp,imgi) def walk_step(self):
 self.s_pntr.walk_step() def start_time(self):
 crrnt_time = datetime.datetime.now()
 hour = crrnt_time.hour
 minute = crrnt_time.minute
 second = crrnt_time.second
 return hour,minute,second class MyButton:
 def __init__(self,root,clock):
 self.clock = clock
 button = tkinter.Button(root,text = '改变表盘',command = self.chg_plate)
 button.pack() def chg_plate(self):
 self.clock.chg_plate() class MyTk(tkinter.Tk):
 def quit(self):
 StartClock.looping = False
 self.quit() class StartClock:
 looping = True
 def __init__(self,mc,root):
 self.mc = mc
 self.root = root def start_clock(self):
 while StartClock.looping:
 self.mc.walk_step()
 self.root.update()
 time.sleep(0.05) if __name__ == '__main__':
 root = MyTk()
 cvns = tkinter.Canvas(root,width=400,height=400,bg='white')
 cvns.pack()
 mc = MyClock((200,200),200,cvns)
 bt = MyButton(root,mc)
 t = threading.Thread(target=StartClock(mc,root).start_clock)
 t.setDaemon(True)
 t.start()
 root.resizable(False, False)
 root.mainloop()
- 刚刚学习,谢谢指出错误!
 
-   
Python--面向对象编程--时钟实例开发的更多相关文章
- python面向对象编程设计与开发
		一.什么是面向对象的程序设计 1.何为数据结构? 数据结构是指相互之间存在一种或多种特定关系的数据元素的集合,如列表.字典. 2.何为编程? 编程是指程序员用特定的语法+数据结构+算法,组成的代码,告 ... 
- Python面向对象编程——继承与派生
		Python面向对象编程--继承与派生 一.初始继承 1.什么是继承 继承指的是类与类之间的关系,是一种什么"是"什么的关系,继承的功能之一就是用来解决代码重用问题. 继承是一种创 ... 
- python 面向对象编程学习
		1. 问题:将所有代码放入一个py文件:无法维护 方案:如果将代码才分放到多个py文件,好处: 1. 同一个名字的变量互相不影响 2.易于维护 3.引用模块: import module 2.包:解决 ... 
- python 面向对象编程(一)
		一.如何定义一个类 在进行python面向对象编程之前,先来了解几个术语:类,类对象,实例对象,属性,函数和方法. 类是对现实世界中一些事物的封装,定义一个类可以采用下面的方式来定义: class c ... 
- Python面向对象编程指南
		Python面向对象编程指南(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1SbD4gum4yGcUruH9icTPCQ 提取码:fzk5 复制这段内容后打开百度网 ... 
- python面向对象编程进阶
		python面向对象编程进阶 一.isinstance(obj,cls)和issubclass(sub,super) isinstance(obj,cls)检查是否obj是否是类 cls 的对象 1 ... 
- Python面向对象编程(下)
		本文主要通过几个实例介绍Python面向对象编程中的封装.继承.多态三大特性. 封装性 我们还是继续来看下上文中的例子,使用Student类创建一个对象,并修改对象的属性.代码如下: #-*- cod ... 
- Python 面向对象编程——访问限制
		<无访问限制的对象> 在Class内部,可以有属性和方法,而外部代码可以通过直接调用实例变量的方法来操作数据,这样,就隐藏了内部的复杂逻辑.但是,从前面Student类的定义来看(见:Py ... 
- Python 面向对象编程基础
		Python 面向对象编程基础 虽然Pthon是解释性语言,但是Pthon可以进行面向对象开发,小到 脚本程序,大到3D游戏,Python都可以做到. 一类: 语法: class 类名: 类属性,方法 ... 
随机推荐
- WordPress发布文章前强制要求上传特色图像
			如果你的网站需要给每篇文章设置特色图像才能达到理想的显示效果,而且允许其他用户在后台发布文章的,那么您可能需要强制要求他们给文章上传特色图像,否者就无法发布.Require Featured Imag ... 
- python数据类型,int,str,bool
			一,python中的int() int在python中主要用来运算,对字符串的转化,用int(str)表示,并且需要str.isdigit为真. 在int()中二进制的转换如下: #bit_lengt ... 
- Windbg内核调试之二: 常用命令
			运用Windbg进行内核调试, 熟练的运用命令行是必不可少的技能. 但是面对众多繁琐的命令, 实在是不可能全部的了解和掌握. 而了解Kernel正是需要这些命令的指引, 不断深入理解其基本的内容. 下 ... 
- Docker资源
			1.Docker入门教程 http://www.code123.cc/docs/docker-practice/repository/config.html 2.Docker入门教程 http://w ... 
- 获得Oracke中刚插入的ID ---> GetInsertedID()
			(1)首先 需要创建序列: CREATE SEQUENCE SE_TD_POWER MINVALUE 1 NOMAXVALUE START WITH 1 INCREMENT BY 1 NOCYCLE ... 
- java流的操作步骤、、
			在java中使用IO操作必须按照以下的步骤完成: 使用File找到一个文件 使用字节流或字符流的子类为OutputStream.InputStream.Writer.Reader进行实例化操 作 ... 
- windows异常演示,指定异常类型,然后生成异常
			#include "stdafx.h"#include <Windows.h>#include <float.h> DWORD Filter (LPEXCE ... 
- cURL使用教程及实例演示
			curl里面的函数不多,主要有: curl_init — 初始化一个CURL会话curl_setopt — 为CURL调用设置一个选项curl_exec — 执行一个CURL会话curl_close ... 
- Solaris与Windows Active Directory集成
			通过Solaris与Active Directory的集成,Solaris可以使用Windows 2003 R2/ 2008 Active Directory来进行用户登录验证.以下是简要配置过程. ... 
- TS封装格式
			ts流最早应用于数字电视领域,其格式非常复杂包含的配置信息表多达十几个,视频格式主要是mpeg2.苹果公司发明的http live stream流媒体是基于ts文件的,不过他大大简化了传统的ts流,只 ... 
