python静态时钟

stillclock.py:

from tkinter import *
import math
from datetime import datetime


class stillClock(Canvas):
    def __init__(self, container):
        super().__init__(container)
        self.setCurrentTime()

    def getHour(self):
        return self.__hour

    def setHour(self, hour):
        self.__hour = hour

    def getMinute(self):
        return self.__minute

    def setMinute(self, minute):
        self.__minute = minute

    def getSecond(self):
        return self.__second

    def setSecond(self, second):
        self.__second = second

    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):
        self.delete("clock")
        width = float(self["width"])
        height = float(self["height"])
        radius = min(width, height) / 2.4
        secondHandLength = radius * 0.8
        minuteHandLength = radius * 0.65
        hourHandLength = radius * 0.5

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

        xCenter = center_w
        yCenter = center_h
        second = self.__second
        xSecond = xCenter + secondHandLength * math.sin(second * (2 * math.pi / 60))
        ySecond = yCenter - secondHandLength * math.cos(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(xCenter, yCenter + radius + 10, text=timestr, tags="clock")

测试:

from tkinter import *
from stillclock import stillClock


class stillclockTest:
    def __init__(self):
        window = Tk()
        window.title("change clock time")

        self.clock = stillClock(window)
        self.clock.pack()

        frame = Frame(window)
        frame.pack()

        Label(frame, text="Hour").pack(side=LEFT)
        self.hour = IntVar()
        self.hour.set(self.clock.getHour())
        Entry(frame, textvariable=self.hour,width=2).pack(side=LEFT)

        Label(frame, text="Minute").pack(side=LEFT)
        self.minute = IntVar()
        self.minute.set(self.clock.getMinute())
        Entry(frame, textvariable=self.minute,width=2).pack(side=LEFT)

        Label(frame, text="Second").pack(side=LEFT)
        self.second = IntVar()
        self.second.set(self.clock.getSecond())

        Entry(frame, textvariable=self.second,width=2).pack(side=LEFT)

        Button(frame, text="Set New Time", command=self.setNewTime).pack(side=LEFT)

        window.mainloop()

    def setNewTime(self):
        self.clock.setHour(self.hour.get())
        self.clock.setMinute(self.minute.get())
        self.clock.setSecond(self.second.get())
        self.clock.drawClock()

stillclockTest()

初始打开,是真实时间
在这里插入图片描述
改动时间:
在这里插入图片描述

Java基础篇中的StillClock类用于时钟以方便Java进阶篇P228第二十九章多线程(第八版)ClockWithAudio类的调用。 package 多线程练习; import java.awt.*; import javax.swing.*; import java.util.*; public class StillClock extends JPanel { private int hour; private int minute; private int second; /** Construct a default clock with the current time */ public StillClock() { setCurrentTime(); } /** Construct a clock with specified hour, minute, and second */ public StillClock(int hour, int minute, int second) { this.hour = hour; this.minute = minute; this.second = second; } /** Return hour */ public int getHour() { return hour; } /** Set a new hour */ public void setHour(int hour) { this.hour = hour; repaint(); } /** Return minute */ public int getMinute() { return minute; } /** Set a new minute */ public void setMinute(int minute) { this.minute = minute; repaint(); } /** Return second */ public int getSecond() { return second; } /** Set a new second */ public void setSecond(int second) { this.second = second; repaint(); } /** Draw the clock */ protected void paintComponent(Graphics g) { super.paintComponent(g); // Initialize clock parameters int clockRadius = (int) (Math.min(getWidth(), getHeight()) * 0.8 * 0.5); int xCenter = getWidth() / 2; int yCenter = getHeight() / 2; // Draw circle g.setColor(Color.black); g.drawOval(xCenter - clockRadius, yCenter - clockRadius, 2 * clockRadius, 2 * clockRadius); g.drawString("12", xCenter - 5, yCenter - clockRadius + 12); g.drawString("9", xCenter - clockRadius + 3, yCenter + 5); g.drawString("3", xCenter + clockRadius - 10, yCenter + 3); g.drawString("6", xCenter - 3, yCenter + clockRadius - 3); // Draw second hand int sLength = (int) (clockRadius * 0.8); int xSecond = (int) (xCenter + sLength * Math.sin(second * (2 * Math.PI / 60))); int ySecond = (int) (yCenter - sLength * Math.cos(second * (2 * Math.PI / 60))); g.setColor(Color.red); g.drawLine(xCenter, yCenter, xSecond, ySecond); // Draw minute hand int mLength = (int) (clockRadius * 0.65); int xMinute = (int) (xCenter + mLength * Math.sin(minute * (2 * Math.PI / 60))); int yMinute = (int) (yCenter - mLength * Math.cos(minute * (2 * Math.PI / 60))); g.setColor(Color.blue); g.drawLine(xCenter, yCenter, xMinute, yMinute); // Draw hour hand int hLength = (int) (clockRadius * 0.5); int xHour = (int) (xCenter + hLength * Math.sin((hour % 12 + minute / 60.0) * (2 * Math.PI / 12))); int yHour = (int) (yCenter - hLength * Math.cos((hour % 12 + minute / 60.0) * (2 * Math.PI / 12))); g.setColor(Color.BLACK); g.drawLine(xCenter, yCenter, xHour, yHour); } public void setCurrentTime() { // Construct a calendar for the current date and time Calendar calendar = new GregorianCalendar(); // Set current hour, minute and second this.hour = calendar.get(Calendar.HOUR_OF_DAY); this.minute = calendar.get(Calendar.MINUTE); this.second = calendar.get(Calendar.SECOND); } public Dimension getPreferredSize() { return new Dimension(200, 200); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

广大菜鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值