共计 1858 个字符,预计需要花费 5 分钟才能阅读完成。
import tkinter as tk
from tkinter import messagebox
from tkinter import simpledialog
import time
import ctypes
import pyautogui
class FullScreenClock:
def __init__(self, master=None):
self.master = master
self.master.overrideredirect(True) # 隐藏窗口边框
# 获取屏幕尺寸
user32 = ctypes.windll.user32
screen_width, screen_height = user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)
# 设置窗口尺寸与位置
self.master.geometry(f"{screen_width}x{screen_height}+0+0")
# 设置背景颜色
self.master.configure(bg='black')
# 创建标签用于显示时间
self.time_label = tk.Label(self.master, text="", font=("Arial", 0), bg="black", fg="white")
self.time_label.pack(expand=True)
# 计算字体大小
font_size = min(int(screen_width * 0.7 / 8), int(screen_height * 0.7 / 2)) # 假设每个字符大约 8 像素宽
self.time_label.config(font=("Arial", font_size))
# 绑定键盘事件
self.master.bind("<Key>", self.on_key_press)
# 显示初始时间
self.update_clock(screen_width, screen_height)
# 开始保持系统活跃
self.keep_system_awake()
# 设置密码
self.password = "password" # 更改为您的密码
def update_clock(self, width, height):
"""更新时间显示"""
current_time = time.strftime("%H:%M:%S")
self.time_label.config(text=current_time)
self.after_id = self.master.after(1000, lambda: self.update_clock(width, height)) # 每秒更新一次
def on_key_press(self, event):
"""处理键盘事件,当按下任何键时退出屏幕保护程序"""
if event.keysym == "Escape":
self.prompt_password()
elif event.keysym == "p": # 按下 'p' 键暂停时间更新
self.master.after_cancel(self.after_id)
elif event.keysym == "r": # 按下 'r' 键恢复时间更新
self.update_clock(self.master.winfo_width(), self.master.winfo_height())
def keep_system_awake(self):
"""模拟键盘事件以保持系统活跃"""
# 每隔一段时间(例如 30 秒)模拟一个键盘事件
pyautogui.press('shift')
self.master.after(30000, self.keep_system_awake) # 每 30 秒模拟一次
def prompt_password(self):
"""提示用户输入密码以退出程序"""
password = simpledialog.askstring("Password", "Enter the password to exit:", show='*')
if password == self.password:
self.master.destroy()
else:
messagebox.showerror("Error", "Incorrect password!")
def run(self):
self.master.mainloop()
def main():
root = tk.Tk()
app = FullScreenClock(root)
app.run()
if __name__ == "__main__":
main()
正文完