您的位置:首页 > 编程语言 > Python开发

Python--面向对象编程--时钟实例开发

2016-07-27 17:12 1091 查看
在学习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()


刚刚学习,谢谢指出错误!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: