基于Python編寫一個桌面時鐘屏保
效果圖
原代碼
# 日歷式時鐘 # 導入所需的庫 # 作者:Hoye # 日期:2024年12月16日 # 功能:顯示當前日期、星期、時間,并顯示模擬時鐘 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") # 獲取當前時間 current_time = time.localtime() hours = current_time.tm_hour % 12 minutes = current_time.tm_min seconds = current_time.tm_sec # 時鐘外圈 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") # 時針 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) # 中心點 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("藍動力電腦-桌面時鐘") # 設置全屏 root.attributes('-fullscreen', True) # 全屏顯示 root.attributes('-topmost', True) # 窗口置頂 root.config(cursor="none") # 隱藏鼠標光標 # 綁定退出事件 root.bind('<Key>', exit_screensaver) # 任意鍵退出 root.bind('<Motion>', exit_screensaver) # 鼠標移動退出 root.bind('<Button>', exit_screensaver) # 鼠標點擊退出 root.bind('<Escape>', exit_screensaver) # ESC鍵退出 # 獲取屏幕尺寸 screen_width = root.winfo_screenwidth() screen_height = root.winfo_screenheight() # 設置背景漸變色 main_frame = tk.Frame(root) main_frame.pack(expand=True, fill='both') main_frame.configure(bg='#2C3E50') # 創(chuàng)建內容框架 content_frame = tk.Frame(main_frame, bg='#2C3E50', padx=20, pady=20) content_frame.pack(expand=True) # 創(chuàng)建左側模擬時鐘框架 analog_frame = tk.Frame(content_frame, bg='#34495E', padx=30, pady=30) analog_frame.pack(side='left', padx=20) # 創(chuàng)建模擬時鐘畫布 clock_canvas = tk.Canvas( analog_frame, width=400, height=400, bg='#34495E', highlightthickness=0 ) clock_canvas.pack() # 創(chuàng)建右側數(shù)字時鐘容器 clock_frame = tk.Frame(content_frame, bg='#34495E', padx=30, pady=30) clock_frame.pack(side='right', padx=20) # 日期標簽 date_label = tk.Label( clock_frame, font=("Microsoft YaHei UI", 48, "bold"), fg="#ECF0F1", bg="#34495E" ) date_label.pack(pady=20) # 星期標簽 weekday_label = tk.Label( clock_frame, font=("Microsoft YaHei UI", 36), fg="#BDC3C7", bg="#34495E" ) weekday_label.pack(pady=20) # 時間標簽 time_label = tk.Label( clock_frame, font=("Microsoft YaHei UI", 120, "bold"), fg="#3498DB", bg="#34495E" ) time_label.pack(pady=30) # 添加版權信息 footer_label = tk.Label( main_frame, text="藍動力電腦 ? 2024", font=("Microsoft YaHei UI", 14), fg="#95A5A6", bg="#2C3E50" ) footer_label.pack(side='bottom', pady=15) # 啟動時鐘更新 update_clock() draw_clock_face() # 啟動主循環(huán) root.mainloop()
代碼簡說:
1. 添加了 exit_screensaver 函數(shù)處理退出事件
2. 設置窗口屬性:
• root.attributes('-fullscreen', True) 實現(xiàn)全屏顯示
• root.attributes('-topmost', True) 使窗口始終置頂
• root.config(cursor="none") 隱藏鼠標光標
3. 綁定各種退出事件:
• 鍵盤按鍵
• 鼠標移動
• 鼠標點擊
打包成exe 再改 成 .scr
setup.py
import PyInstaller.__main__ PyInstaller.__main__.run([ '9_日歷式時鐘.py', '--name=藍動力時鐘屏保', '--noconsole', '--onefile', # '--icon=clock.ico', # 如果您有圖標文件的話 '--windowed', ])
py setup.py
1. 打包完成后,在 dist 目錄下找到生成的 exe 文件
2. 將 exe 文件復制一份,改名為 .scr 后綴 • 例如:藍動力時鐘屏保.exe → 藍動力時鐘屏保.scr
3. 將 .scr 文件復制到 Windows 系統(tǒng)目錄:
• 通常是 C:\Windows\System32
• 或者 C:\Windows\SysWOW64(64位系統(tǒng))
4. 在 Windows 設置中設置屏保:
• 右鍵桌面 → 個性化
• 鎖屏界面 → 屏幕保護程序設置
• 在屏幕保護程序下拉菜單中選擇"藍動力時鐘屏保"
以上就是基于Python編寫一個桌面時鐘屏保的詳細內容,更多關于Python桌面時鐘屏保的資料請關注腳本之家其它相關文章!
相關文章
Python爬蟲實現(xiàn)抓取京東店鋪信息及下載圖片功能示例
這篇文章主要介紹了Python爬蟲實現(xiàn)抓取京東店鋪信息及下載圖片功能,涉及Python頁面請求、響應、解析等相關操作技巧,需要的朋友可以參考下2018-08-08使用Selenium在Python中實現(xiàn)錄屏功能
Selenium 是一個強大的用于自動化測試的工具,但你知道它也可以用來錄制瀏覽器操作的視頻嗎?本文將介紹如何使用 Selenium 在 Python 中實現(xiàn)錄屏功能,以便記錄和分享你的網頁操作過程,需要的朋友可以參考下2023-11-11Python Pandas數(shù)據(jù)中對時間的操作
這篇文章主要介紹了Python Pandas數(shù)據(jù)中對時間的操作,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-07-07Python使用Flask-SQLAlchemy連接數(shù)據(jù)庫操作示例
這篇文章主要介紹了Python使用Flask-SQLAlchemy連接數(shù)據(jù)庫操作,簡單介紹了flask、Mysql-Python以及Flask-SQLAlchemy的安裝方法,并結合實例形式分析了基于Flask-SQLAlchemy的數(shù)據(jù)庫連接相關操作技巧,需要的朋友可以參考下2018-08-08使用python實現(xiàn)兩數(shù)之和的畫解算法
這篇文章主要介紹了使用python實現(xiàn)兩數(shù)之和的畫解算法,采用實例問題的描述來進行問題分析,并給出用暴力求解和哈希表兩種方法解決方案,有需要的朋友可以參考下2021-08-08