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

原代碼
# 日歷式時(shí)鐘
# 導(dǎo)入所需的庫
# 作者: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()代碼簡說:
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í)鐘屏保的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python爬蟲實(shí)現(xiàn)抓取京東店鋪信息及下載圖片功能示例
這篇文章主要介紹了Python爬蟲實(shí)現(xiàn)抓取京東店鋪信息及下載圖片功能,涉及Python頁面請求、響應(yīng)、解析等相關(guān)操作技巧,需要的朋友可以參考下2018-08-08
使用Selenium在Python中實(shí)現(xiàn)錄屏功能
Selenium 是一個(gè)強(qiáng)大的用于自動(dòng)化測試的工具,但你知道它也可以用來錄制瀏覽器操作的視頻嗎?本文將介紹如何使用 Selenium 在 Python 中實(shí)現(xiàn)錄屏功能,以便記錄和分享你的網(wǎng)頁操作過程,需要的朋友可以參考下2023-11-11
Python標(biāo)準(zhǔn)庫之Sys模塊使用詳解
這篇文章主要介紹了Python標(biāo)準(zhǔn)庫之Sys模塊使用詳解,本文講解了使用sys模塊獲得腳本的參數(shù)、處理模塊、使用sys模塊操作模塊搜索路徑、使用sys模塊查找內(nèi)建模塊、使用sys模塊查找已導(dǎo)入的模塊等使用案例,需要的朋友可以參考下2015-05-05
Python Pandas數(shù)據(jù)中對時(shí)間的操作
這篇文章主要介紹了Python Pandas數(shù)據(jù)中對時(shí)間的操作,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Python 實(shí)現(xiàn)局域網(wǎng)遠(yuǎn)程屏幕截圖案例
這篇文章主要介紹了Python 實(shí)現(xiàn)局域網(wǎng)遠(yuǎn)程屏幕截圖案例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03
Python使用Flask-SQLAlchemy連接數(shù)據(jù)庫操作示例
這篇文章主要介紹了Python使用Flask-SQLAlchemy連接數(shù)據(jù)庫操作,簡單介紹了flask、Mysql-Python以及Flask-SQLAlchemy的安裝方法,并結(jié)合實(shí)例形式分析了基于Flask-SQLAlchemy的數(shù)據(jù)庫連接相關(guān)操作技巧,需要的朋友可以參考下2018-08-08
使用python實(shí)現(xiàn)兩數(shù)之和的畫解算法
這篇文章主要介紹了使用python實(shí)現(xiàn)兩數(shù)之和的畫解算法,采用實(shí)例問題的描述來進(jìn)行問題分析,并給出用暴力求解和哈希表兩種方法解決方案,有需要的朋友可以參考下2021-08-08

