关闭

Python GUI类设计(可重用时钟)

标签: pythongui继承
182人阅读 评论(0) 收藏 举报
分类:
设计一个GUI类来显示一个时钟,假设要在一个画布内显示它而且能够在其他程序中。需要定义一个时钟类来实现时钟的重用。进一步说,为了在图形上显示这个时钟,需要将它定义为一个widget小构件。因此最好的选择是定义时钟类扩展Canvas类,使时钟对象能够像Canvas对象一样使用。
定义时钟类StillClock为Canvas的扩展,代码如下:
# coding: utf-8
from Tkinter import * #Import all definitions from tkinter
import math
from datetime import datetime

class StillClock(Canvas):
    def __int__(self, container):
        Canvas.__init__(container)
        self.setCurrentTime()

    def getHour(self):
        return self.__hour

    def setHour(self, hour):
        self.__hour = hour
        self.delete('clock')
        self.drawClock()

    def getMinute(self):
        return self.__minute

    def setMnite(self, minute):
        self.__minute = minute
        self.delete("clock")
        self.drawClock()

    def getSecond(self):
        return self.__second

    def setSecond(self, second):
        self.__second = second
        self.delete("clock")
        self.drawClock()

    def setCurrentTime(self):
        d = datetime.now()
        self.__hour = d.hour
        self.__minute = d.minute
        self.__second = d.second
        self.delete("clock")
        self.drawClock()

    def drawClock(self):
        width = float(self["width"])
        height = float(self["height"])
        radius = min(min, height) / 2.4
        secondHandLength = radius * 0.8
        minuteHandLength = radius * 0.65
        hourHandLength = radius * 0.5

        self.create_oval(width / 2 - radius, height / 2 - radius,
             width / 2 + radius, height / 2 + radius, tags = "clock")
        self.create_text(width / 2 - radius + 5, height / 2,
                         text = "9", tags = "clock")
        self.create_text(width / 2 + radius - 5, height / 2,
                         text = "3", tags = "clock")
        self.create_text(width / 2, height / 2 - radius + 5,
                         text = "12", tags = "clock")
        self.create_text(width / 2, height / 2 + radius - 5,
                         text = "6", tags = "clock")

        xCenter = width / 2
        yCenter = height / 2
        second = self.__second
        xSecond = xCenter + secondHandLength \
            * math.sin(second * (2 * math.pi / 60))
        ySecond = yCenter - secondHandLength \
            * math.sin(second * (2 * math.pi / 60))
        self.create_line(xCenter, yCenter, xSecond, ySecond,
                         fill = "red", tags = "clock")

        minute = self.__minute
        xMinute = xCenter + \
            minuteHandLength * math.sin(minute * (2 * math.pi / 60))
        yMinute = yCenter - \
            minuteHandLength * math.cos(minute * (2 * math.pi / 60))
        self.create_line(xCenter, yCenter, xMinute, yMinute,
                         fill = "blue", tags = "clock")

        hour = self.__hour % 12
        xHour = xCenter + hourHandLength * \
                math.sin((hour + minute / 60) * (2 * math.pi / 12))
        yHour = yCenter - hourHandLength * \
                math.cos((hour + minute / 60) * (2 * math.pi / 12))
        self.create_line(xCenter, yCenter, xHour, yHour,
                         fill = "green", tags = "clock")

        timestr = str(hour) + ":" +str(minute) + ":" + str(second)
        self.create_text(width / 2, height / 2 + radius + 10,
                         text = timestr, tags = "clock")

StillClock类扩展自Canvas小构件,因此可以像使用Canvas一样使用StillClock.
—摘自《Python 语言设计》

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4845次
    • 积分:262
    • 等级:
    • 排名:千里之外
    • 原创:19篇
    • 转载:5篇
    • 译文:1篇
    • 评论:1条
    文章分类
    最新评论