好友
阅读权限25
听众
最后登录1970-1-1
|
闲来无事学py,弄了个花哨的桌面效果
漂亮的壁纸是之前52里面分享的,同时分享给大家哦
软件下载链接(内附源码,有需要自取)
https://www.lanzouq.com/iL98U2w9uihe
其他:- 自己可自定义一下
- 不修改源码的情况下
- 只用修改对应的图片资源,比如弄从火柴人,比如弄成雪花,比如弄成雪球,大家自己开脑洞吧
- 如何修改源码其实也可以弄从简单的背景小游戏,无聊的时候摸摸鱼,比如弄成切水果,贪吃蛇啥的,自己拓展吧
![]()
没啥毒哦,顺手带上源码大家看哦
[Python] 纯文本查看 复制代码 import ctypes
import os
import random
import sys
import time
from PyQt5.QtCore import Qt, QTimer, QPoint, QByteArray
from PyQt5.QtGui import QPainter, QColor, QPixmap, QCursor
from PyQt5.QtWidgets import QApplication, QWidget
# 新增:开机自动启动实现
def add_to_startup():
"""通过创建启动项实现开机自动启动"""
try:
import winshell
from win32com.client import Dispatch
except ImportError:
print("请安装依赖库:pip install pywin32 winshell")
return
startup_folder = winshell.startup() # 获取启动目录
print("添加到启动项:", startup_folder)
shortcut_path = os.path.join(startup_folder, "小朋友.lnk")
if not os.path.exists(shortcut_path):
shell = Dispatch('WScript.Shell')
shortcut = shell.CreateShortCut(shortcut_path)
# 判断是否是打包后的exe环境
if getattr(sys, 'frozen', False):
# 打包后的exe路径
executable_path = sys.executable
else:
# 开发环境,使用当前脚本路径
executable_path = os.path.abspath(__file__)
# 快捷方式直接指向 .exe 文件,无需额外参数
shortcut.Targetpath = executable_path
shortcut.Arguments = '' # 因为目标已经是exe,不需要参数
shortcut.WorkingDirectory = os.path.dirname(executable_path) # 设置工作目录为exe所在目录
shortcut.save()
print("已添加到开机启动项")
else:
print("已存在开机启动项")
# 获取桌面窗口句柄并嵌入
def get_desktop_window():
# 查找ProgMan窗口
progman = ctypes.windll.user32.FindWindowW('ProgMan', None)
# 发送0x052C消息生成WorkerW窗口
ctypes.windll.user32.SendMessageW(progman, 0x052C, 0, 0)
hwnd = ctypes.windll.user32.FindWindowExW(None, None, 'WorkerW', None)
while hwnd:
# 查找包含SHELLDLL_DefView的子窗口
shell_view = ctypes.windll.user32.FindWindowExW(hwnd, None, 'SHELLDLL_DefView', None)
if shell_view:
return hwnd
hwnd = ctypes.windll.user32.FindWindowExW(None, hwnd, 'WorkerW', None)
return None
class Snowflake:
def __init__(self, max_width, max_height):
self.x = random.randint(0, max_width)
self.y = random.randint(-max_height, 0) # 从屏幕上方随机位置生成
self.vx = random.uniform(-1, 1)
self.vy = random.uniform(0.5, 1.5) # 向下飘落的速度
self.radius = random.randint(5, 10)
self.color = QColor(255, 255, 255, 200)
self.pixmap = QPixmap("素材.png")
self.pixmap = self.pixmap.scaled(self.radius * 2, self.radius * 2, Qt.KeepAspectRatio, Qt.SmoothTransformation)
def move(self, max_width, max_height, mouse_pos=None):
# 如果有鼠标位置信息,检测是否需要弹开
if mouse_pos:
mouse_x, mouse_y = mouse_pos
distance = ((self.x - mouse_x) ** 2 + (self.y - mouse_y) ** 2) ** 0.5
if distance < 100: # 修改:增大鼠标影响范围阈值
self.vx = (self.x - mouse_x) * 0.2 # 修改:增强弹开力度
self.vy = -(self.y - mouse_y) * 0.2 # 修改:增强弹开力度
self.x += self.vx * 3 # 修改:增强弹开力度
self.y += self.vy * 3 # 修改:增强弹开力度
else:
self.x += self.vx
self.y += self.vy
else:
self.x += self.vx
self.y += self.vy
# 判断是否到达地面或逃出边界
if self.y >= max_height or self.x < 0 or self.x > max_width:
return False #需要销毁
return True # 继续存在
def draw(self, painter):
# 使用图片绘制
# 修改:将 self.x 和 self.y 转换为整数类型
painter.drawPixmap(QPoint(int(self.x - self.radius), int(self.y - self.radius)), self.pixmap)
class DynamicWallpaper(QWidget):
def __init__(self):
super().__init__()
self.broom_pixmap_list = [
QPixmap(os.path.join("img", f)).scaled(100, 100, Qt.KeepAspectRatio, Qt.SmoothTransformation)
for f in sorted(
[f for f in os.listdir("img") if f.endswith(".png")],
key=lambda x: int(os.path.splitext(x)[0]) # 按数字排序
)
]
self.current_broom_index = 0
self.broom_timer = QTimer(self)
# self.broom_timer.timeout.connect(self.switch_broom_image)
self.broom_effect_duration = 6000 # 毫秒
self.last_collision_time = 0 # 记录最后一次触发的时间
# 初始化窗口属性
self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowTransparentForInput)
self.setAttribute(Qt.WA_TranslucentBackground)
self.setGeometry(0, 0, QApplication.desktop().screenGeometry().width(),
QApplication.desktop().screenGeometry().height())
# 创建雪花
self.snowflakes = [Snowflake(self.width(), self.height()) for _ in range(50)] # 初始
# 定时器更新动画
self.timer = QTimer(self)
self.timer.timeout.connect(self.update_snowflakes)
self.timer.start(30)
# 新增:周期性调整雪花数量的定时器
self.snow_timer = QTimer(self)
self.snow_timer.timeout.connect(self.adjust_snow_amount)
self.snow_timer.start(60000) # 每分钟调整一次
self.snow_state = 0 # 0: 小, 1: 大, 2: 暴, 3: 大, 4: 小
# 嵌入到桌面窗口
desktop_hwnd = get_desktop_window()
if desktop_hwnd:
self.winId() # 确保窗口句柄存在
ctypes.windll.user32.SetParent(int(self.windowHandle().winId()), desktop_hwnd)
self.mouse_pos = None
self.broom_pixmap = QPixmap("")
self.broom_pixmap = self.broom_pixmap.scaled(100, 100, Qt.KeepAspectRatio, Qt.SmoothTransformation)
def start_random_broom_switch(self):
current_time = self.get_current_time()
if current_time - self.last_collision_time > self.broom_effect_duration:
return
# 随机下一次切换时间(50ms ~ 300ms)
next_delay = random.randint(150, 250)
QTimer.singleShot(next_delay, self.random_switch_broom)
def random_switch_broom(self):
current_time = self.get_current_time()
if current_time - self.last_collision_time > self.broom_effect_duration:
return
self.current_broom_index = random.randint(0, len(self.broom_pixmap_list) - 1)
self.update()
# 继续下一次切换
self.start_random_broom_switch()
def get_current_time(self):
return int(time.time() * 1000) # 毫秒级时间戳
def adjust_snow_amount(self):
if self.snow_state == 0:
self.snowflakes.extend([Snowflake(self.width(), self.height()) for _ in range(50)])
self.snow_state = 1
elif self.snow_state == 1:
self.snowflakes.extend([Snowflake(self.width(), self.height()) for _ in range(100)])
self.snow_state = 2
elif self.snow_state == 2:
self.snowflakes = self.snowflakes[:150]
self.snow_state = 3
elif self.snow_state == 3:
self.snowflakes = self.snowflakes[:100]
self.snow_state = 4
elif self.snow_state == 4:
self.snowflakes = self.snowflakes[:50]
self.snow_state = 0
def update_snowflakes(self):
for flake in self.snowflakes[:]: # 使用切片复制列表以避免修改时迭代
if not flake.move(self.width(), self.height(), self.mouse_pos): # 如果需要销毁
self.snowflakes.remove(flake) # 移除
self.snowflakes.append(Snowflake(self.width(), self.height())) # 在顶部生成新的
self.update() # 触发重绘
def paintEvent(self, event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
# 获取鼠标当前位置
mouse_pos = QCursor.pos()
mouse_pos = self.mapFromGlobal(mouse_pos)
current_broom = self.broom_pixmap_list[self.current_broom_index]
# 绘制
painter.drawPixmap(
mouse_pos.x() - current_broom.width() // 2 + 40,
mouse_pos.y() + 20,
current_broom
)
# 绘制
for flake in self.snowflakes:
# 计算距离
distance = ((flake.x - mouse_pos.x()) ** 2 + (flake.y - mouse_pos.y()) ** 2) ** 0.5
if distance < 60: # 鼠标影响范围阈值
flake.vx = (flake.x - mouse_pos.x()) * 0.2
flake.vy = -(flake.y - mouse_pos.y()) * 0.2
flake.x += flake.vx * 3
flake.y += flake.vy * 3
flake.color.setAlpha(255)
current_time = self.get_current_time()
if current_time - self.last_collision_time > self.broom_effect_duration:
self.last_collision_time = current_time
self.start_random_broom_switch()
else:
flake.color.setAlpha(200) # 其他时刻保持正常透明度
# 绘制雪花
flake.draw(painter)
# 绘制所有雪花后,新增绘制扫把
painter.drawPixmap(
mouse_pos.x() - self.broom_pixmap.width() // 2 + 40,
mouse_pos.y() + 20, # 鼠标下方20像素位置
self.broom_pixmap
)
# 新增:捕获鼠标移动事件
def mouseMoveEvent(self, event):
self.mouse_pos = (event.x(), event.y())
if __name__ == '__main__':
add_to_startup()
# 新增:检查互斥锁
mutex = ctypes.windll.kernel32.CreateMutexW(None, True, "Local/SnowfallProgramMutex")
if ctypes.windll.kernel32.GetLastError() == 0x00000010:
print("检测到程序已在运行,即将退出...")
sys.exit(-1)
app = QApplication(sys.argv)
w = DynamicWallpaper()
w.show()
sys.exit(app.exec_())
|
免费评分
-
查看全部评分
本帖被以下淘专辑推荐:
- · 软件合辑|主题: 3876, 订阅: 2597
- · 工具|主题: 40, 订阅: 0
- · 收藏必精品|主题: 11, 订阅: 0
|