基于Python編寫一個(gè)桌面時(shí)鐘屏保
效果圖

原代碼
# 日歷式時(shí)鐘
# 導(dǎo)入所需的庫(kù)
# 作者:Hoye
# 日期:2024年12月16日
# 功能:顯示當(dāng)前日期、星期、時(shí)間,并顯示模擬時(shí)鐘
import tkinter as tk
from tkinter import ttk
import time
import math
import sys
def exit_screensaver(event=None):
root.quit()
def draw_clock_face():
# 清除畫布
clock_canvas.delete("all")
# 獲取當(dāng)前時(shí)間
current_time = time.localtime()
hours = current_time.tm_hour % 12
minutes = current_time.tm_min
seconds = current_time.tm_sec
# 時(shí)鐘外圈
clock_canvas.create_oval(10, 10, 390, 390, width=2, outline="#ECF0F1")
# 繪制刻度和數(shù)字
for i in range(12):
angle = i * math.pi/6 - math.pi/2
# 刻度線
start_x = 200 + 190 * math.cos(angle)
start_y = 200 + 190 * math.sin(angle)
end_x = 200 + 180 * math.cos(angle)
end_y = 200 + 180 * math.sin(angle)
width = 3 if i % 3 == 0 else 1
clock_canvas.create_line(start_x, start_y, end_x, end_y, fill="#ECF0F1", width=width)
# 添加數(shù)字
num = 12 if i == 0 else i
text_x = 200 + 155 * math.cos(angle)
text_y = 200 + 155 * math.sin(angle)
clock_canvas.create_text(text_x, text_y, text=str(num),
font=("Microsoft YaHei UI", 20, "bold"),
fill="#ECF0F1")
# 時(shí)針
hour_angle = (hours + minutes/60) * math.pi/6 - math.pi/2
hour_x = 200 + 100 * math.cos(hour_angle)
hour_y = 200 + 100 * math.sin(hour_angle)
clock_canvas.create_line(200, 200, hour_x, hour_y, fill="#3498DB", width=8)
# 分針
min_angle = minutes * math.pi/30 - math.pi/2
min_x = 200 + 140 * math.cos(min_angle)
min_y = 200 + 140 * math.sin(min_angle)
clock_canvas.create_line(200, 200, min_x, min_y, fill="#ECF0F1", width=6)
# 秒針
sec_angle = seconds * math.pi/30 - math.pi/2
sec_x = 200 + 160 * math.cos(sec_angle)
sec_y = 200 + 160 * math.sin(sec_angle)
clock_canvas.create_line(200, 200, sec_x, sec_y, fill="#BDC3C7", width=2)
# 中心點(diǎn)
clock_canvas.create_oval(195, 195, 205, 205, fill="#3498DB")
# 每秒更新
root.after(1000, draw_clock_face)
def update_clock():
current_time = time.localtime()
year = current_time.tm_year
month = current_time.tm_mon
day = current_time.tm_mday
weekday = current_time.tm_wday
hours = current_time.tm_hour
minutes = current_time.tm_min
seconds = current_time.tm_sec
date_str = f"{year}年{month:02d}月{day:02d}日"
weekday_str = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"][weekday]
time_str = f"{hours:02d}:{minutes:02d}:{seconds:02d}"
date_label.config(text=date_str)
weekday_label.config(text=weekday_str)
time_label.config(text=time_str)
root.after(1000, update_clock)
# 創(chuàng)建主窗口
root = tk.Tk()
root.title("藍(lán)動(dòng)力電腦-桌面時(shí)鐘")
# 設(shè)置全屏
root.attributes('-fullscreen', True) # 全屏顯示
root.attributes('-topmost', True) # 窗口置頂
root.config(cursor="none") # 隱藏鼠標(biāo)光標(biāo)
# 綁定退出事件
root.bind('<Key>', exit_screensaver) # 任意鍵退出
root.bind('<Motion>', exit_screensaver) # 鼠標(biāo)移動(dòng)退出
root.bind('<Button>', exit_screensaver) # 鼠標(biāo)點(diǎn)擊退出
root.bind('<Escape>', exit_screensaver) # ESC鍵退出
# 獲取屏幕尺寸
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
# 設(shè)置背景漸變色
main_frame = tk.Frame(root)
main_frame.pack(expand=True, fill='both')
main_frame.configure(bg='#2C3E50')
# 創(chuàng)建內(nèi)容框架
content_frame = tk.Frame(main_frame, bg='#2C3E50', padx=20, pady=20)
content_frame.pack(expand=True)
# 創(chuàng)建左側(cè)模擬時(shí)鐘框架
analog_frame = tk.Frame(content_frame, bg='#34495E', padx=30, pady=30)
analog_frame.pack(side='left', padx=20)
# 創(chuàng)建模擬時(shí)鐘畫布
clock_canvas = tk.Canvas(
analog_frame,
width=400,
height=400,
bg='#34495E',
highlightthickness=0
)
clock_canvas.pack()
# 創(chuàng)建右側(cè)數(shù)字時(shí)鐘容器
clock_frame = tk.Frame(content_frame, bg='#34495E', padx=30, pady=30)
clock_frame.pack(side='right', padx=20)
# 日期標(biāo)簽
date_label = tk.Label(
clock_frame,
font=("Microsoft YaHei UI", 48, "bold"),
fg="#ECF0F1",
bg="#34495E"
)
date_label.pack(pady=20)
# 星期標(biāo)簽
weekday_label = tk.Label(
clock_frame,
font=("Microsoft YaHei UI", 36),
fg="#BDC3C7",
bg="#34495E"
)
weekday_label.pack(pady=20)
# 時(shí)間標(biāo)簽
time_label = tk.Label(
clock_frame,
font=("Microsoft YaHei UI", 120, "bold"),
fg="#3498DB",
bg="#34495E"
)
time_label.pack(pady=30)
# 添加版權(quán)信息
footer_label = tk.Label(
main_frame,
text="藍(lán)動(dòng)力電腦 ? 2024",
font=("Microsoft YaHei UI", 14),
fg="#95A5A6",
bg="#2C3E50"
)
footer_label.pack(side='bottom', pady=15)
# 啟動(dòng)時(shí)鐘更新
update_clock()
draw_clock_face()
# 啟動(dòng)主循環(huán)
root.mainloop()代碼簡(jiǎn)說(shuō):
1. 添加了 exit_screensaver 函數(shù)處理退出事件
2. 設(shè)置窗口屬性:
• root.attributes('-fullscreen', True) 實(shí)現(xiàn)全屏顯示
• root.attributes('-topmost', True) 使窗口始終置頂
• root.config(cursor="none") 隱藏鼠標(biāo)光標(biāo)
3. 綁定各種退出事件:
• 鍵盤按鍵
• 鼠標(biāo)移動(dòng)
• 鼠標(biāo)點(diǎn)擊
打包成exe 再改 成 .scr
setup.py
import PyInstaller.__main__
PyInstaller.__main__.run([
'9_日歷式時(shí)鐘.py',
'--name=藍(lán)動(dòng)力時(shí)鐘屏保',
'--noconsole',
'--onefile',
# '--icon=clock.ico', # 如果您有圖標(biāo)文件的話
'--windowed',
])py setup.py
1. 打包完成后,在 dist 目錄下找到生成的 exe 文件
2. 將 exe 文件復(fù)制一份,改名為 .scr 后綴 • 例如:藍(lán)動(dòng)力時(shí)鐘屏保.exe → 藍(lán)動(dòng)力時(shí)鐘屏保.scr
3. 將 .scr 文件復(fù)制到 Windows 系統(tǒng)目錄:
• 通常是 C:\Windows\System32
• 或者 C:\Windows\SysWOW64(64位系統(tǒng))
4. 在 Windows 設(shè)置中設(shè)置屏保:
• 右鍵桌面 → 個(gè)性化
• 鎖屏界面 → 屏幕保護(hù)程序設(shè)置
• 在屏幕保護(hù)程序下拉菜單中選擇"藍(lán)動(dòng)力時(shí)鐘屏保"


以上就是基于Python編寫一個(gè)桌面時(shí)鐘屏保的詳細(xì)內(nèi)容,更多關(guān)于Python桌面時(shí)鐘屏保的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python+selenium+chrome批量文件下載并自動(dòng)創(chuàng)建文件夾實(shí)例
這篇文章主要介紹了python+selenium+chrome批量文件下載并自動(dòng)創(chuàng)建文件夾實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
Python進(jìn)行有限元仿真的使用及創(chuàng)建
這篇文章主要為大家介紹了Python進(jìn)行有限元仿真的創(chuàng)建及使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
10行Python代碼計(jì)算汽車數(shù)量的實(shí)現(xiàn)方法
這篇文章主要介紹了10行Python代碼計(jì)算汽車數(shù)量的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Python中defaultdict方法常見的使用方法及問(wèn)題解答
Python?defaultdict?是?collections?模塊的?dict?子類,自動(dòng)處理缺失鍵,這篇文章主要介紹了Python中defaultdict方法常見的使用方法及問(wèn)題解答,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-07-07
Python練習(xí)之操作MySQL數(shù)據(jù)庫(kù)
這篇文章主要介紹了Python練習(xí)之操作MySQL數(shù)據(jù)庫(kù),文章通過(guò)如何創(chuàng)建MySQL數(shù)據(jù)表?如何向MySQL表中插入數(shù)據(jù)?如何查詢MySQL中的數(shù)據(jù)?的三個(gè)問(wèn)題展開了詳細(xì)的內(nèi)容介紹2022-06-06
基于OpenCV和Gradio實(shí)現(xiàn)簡(jiǎn)單的人臉識(shí)別詳解
這篇文章主要為大家詳細(xì)介紹了如何基于OpenCV和Gradio實(shí)現(xiàn)簡(jiǎn)單的人臉識(shí)別功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-04-04

