泛艺舟
发布于 IP属地陕西省

python编写的一个时钟程序

python编写的一个时钟程序,可以设置下班时间,显示距离下班时间的剩余秒数,有下班提醒。

import datetime
import tkinter as tk
import time


class Clock:
    def __init__(self, master):
        self.master = master
        self.master.title("Clock")
        self.label = tk.Label(self.master, font=('Helvetica', 48), bg='black',
                              fg='white')
        self.left = tk.Label(self.master, font=('Helvetica', 48), bg='black',
                             fg='white')
        self.label.pack(fill='both', expand=True)
        self.left.pack(fill='both', expand=True)
        # Define the end_of_work_time attribute with a default value
        self.end_of_work_time = self.load_end_of_work_time()
        self.update_clock()

        # Add a button to open the settings window
        self.settings_button = tk.Button(self.master, text="Settings",
                                         command=self.open_settings)
        self.settings_button.pack(side='bottom')
        self.update_countdown()

    def update_countdown(self):
        end_time = datetime.datetime.strptime(self.end_of_work_time, '%H:%M:%S')
        now_time = datetime.datetime.now().time()
        if now_time > end_time.time():
            self.left.config(text="Time to go home!")
        else:
            delta = datetime.datetime.combine(datetime.date.today(),
                                              end_time.time()) - datetime.datetime.combine(
                datetime.date.today(), now_time)
            self.left.config(text=str(delta.seconds))
        self.master.after(1000, self.update_countdown)

    def update_clock(self):
        now = time.strftime('%H:%M:%S')
        self.label.config(text=now)
        if now == self.end_of_work_time:
            self.reminder()
        self.master.after(1000, self.update_clock)

    def reminder(self):
        reminder_window = tk.Toplevel(self.master)
        reminder_window.title("Reminder")
        reminder_label = tk.Label(reminder_window, text="Time to go home!",
                                  font=('Helvetica', 24))
        reminder_label.pack(padx=50, pady=50)

    def open_settings(self):
        settings_window = tk.Toplevel(self.master)
        settings_window.title("Settings")
        settings_label = tk.Label(settings_window,
                                  text="Enter end of work time (HH:MM:SS):",
                                  font=('Helvetica', 16))
        settings_label.pack(padx=50, pady=50)
        self.settings_entry = tk.Entry(settings_window, font=('Helvetica', 16))
        self.settings_entry.pack(padx=50, pady=50)
        self.settings_entry.insert(0,
                                   self.end_of_work_time)  # Load the current value into the entry field
        settings_button = tk.Button(settings_window, text="Save",
                                    command=self.save_settings)
        settings_button.pack(padx=50, pady=50)

    def save_settings(self):
        self.end_of_work_time = self.settings_entry.get()
        self.save_end_of_work_time(
            self.end_of_work_time)  # Save the value to a file
        self.master.focus_set()

    def load_end_of_work_time(self):
        try:
            with open('end_of_work_time.txt', 'r') as f:
                return f.read().strip()
        except FileNotFoundError:
            return '18:00:00'  # Default value if the file doesn't exist

    def save_end_of_work_time(self, end_of_work_time):
        with open('end_of_work_time.txt', 'w') as f:
            f.write(end_of_work_time)


root = tk.Tk()
clock = Clock(root)
root.mainloop()

浏览 (226)
点赞 (1)
收藏
评论
暂无数据