可爱的顶流真是一dun难求,所以想用python实现在桌面上拥有一个,他会一直在桌面上活动,可以通过鼠标拖动到桌面任意地方活动。
1. 效果展示
2. 实现步骤
2.1 图片素材
本项目是基于PyQt5实现的桌面程序,所以需要前期准备图片素材,个人建议最好直接使用透明背景的.png格式图片,这里我是在网上找了gif动态图,然后将其转换成png格式的图片序列,并进行编号分别保存在不同文件夹中(每个动作的序列保存为一个pet_%s),这里展示其中一个动作的序列图集:
?2.2?代码实现
引入相关依赖包,如PyQt5等
import os
import cfg
import sys
import random
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5 import QtWidgets, QtGui
初始化桌面宠物窗口组件,并设置属性
class DesktopPet(QWidget):
def __init__(self, parent=None, **kwargs):
super(DesktopPet, self).__init__(parent)
# 初始化
self.setWindowFlags(Qt.FramelessWindowHint|Qt.WindowStaysOnTopHint|Qt.SubWindow)
self.setAutoFillBackground(False)
self.setAttribute(Qt.WA_TranslucentBackground, True)
self.repaint()
首先要实现一个导入所有动作序列图片的函数randomLoadPetImages
def randomLoadPetImages(self):
pet_name = random.choice(list(cfg.PET_ACTIONS_MAP.keys()))
actions = cfg.PET_ACTIONS_MAP[pet_name]
pet_images = []
for action in actions:
pet_images.append(
[self.loadImage(os.path.join(cfg.ROOT_DIR, pet_name, '0-' + item + '.png')) for item in action])
iconpath = os.path.join(cfg.ROOT_DIR, pet_name, '0-1.png')
return pet_images, iconpath
# 随机导入一个宠物(一组动作)
self.pet_images, iconpath = self.randomLoadPetImages()
# 当前显示的图片
self.image = QLabel(self)
self.setImage(self.pet_images[0][0])
接着实现退出功能
# 设置退出选项
quit_action = QAction('退出', self, triggered=self.quit)
quit_action.setIcon(QIcon(iconpath))
self.tray_icon_menu = QMenu(self)
self.tray_icon_menu.addAction(quit_action)
self.tray_icon = QSystemTrayIcon(self)
self.tray_icon.setIcon(QIcon(iconpath))
self.tray_icon.setContextMenu(self.tray_icon_menu)
self.tray_icon.show()
这样在最小化托盘就能找到图标,鼠标右键之后就能退出了
?由于只是处于个人娱乐,互动功能比较简略,只做了鼠标点击拖动的功能,这样也可以避免遮挡我们的屏幕了(当然,看恐怖片或许可以拿来挡一下)
'''鼠标左键按下时, 宠物将和鼠标位置绑定'''
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.is_follow_mouse = True
self.mouse_drag_pos = event.globalPos() - self.pos()
event.accept()
self.setCursor(QCursor(Qt.OpenHandCursor))
'''鼠标移动, 则宠物也移动'''
def mouseMoveEvent(self, event):
if Qt.LeftButton and self.is_follow_mouse:
self.move(event.globalPos() - self.mouse_drag_pos)
event.accept()
'''鼠标释放时, 取消绑定'''
def mouseReleaseEvent(self, event):
self.is_follow_mouse = False
self.setCursor(QCursor(Qt.ArrowCursor))
?最后,由于我们的图片是动作序列,所以需要使用一个定时器进行图片的切换和加载,使得一张张图片连续的拼接成会动画
# 每隔一段时间做个动作
self.timer = QTimer()
self.timer.timeout.connect(self.randomAct)
self.timer.start(200)
'''随机做一个动作'''
def randomAct(self):
if not self.is_running_action:
self.is_running_action = True
self.action_images = random.choice(self.pet_images)
self.action_max_len = len(self.action_images)
self.action_pointer = 0
self.runFrame()
'''完成动作的每一帧'''
def runFrame(self):
if self.action_pointer == self.action_max_len:
self.is_running_action = False
self.action_pointer = 0
self.action_max_len = 0
self.setImage(self.action_images[self.action_pointer])
self.action_pointer += 1
最后进行配置文件cfg.py的设置,由于只用了三个动作,因此只用了3个pet文件夹保存图集,我们使用随机函数,使每次运行都可能出现不同的动作
'''配置文件'''
ROOT_DIR = 'resources'
ACTION_DISTRIBUTION = [['1', '2', '3'],
['4', '5', '6', '7', '8', '9', '10', '11'],
['12', '13', '14'],
['15', '16', '17', '18']]
PET_ACTIONS_MAP = {'pet_1': ACTION_DISTRIBUTION}
for i in range(1, 4):
PET_ACTIONS_MAP.update({'pet_%s' % i: ACTION_DISTRIBUTION})
?完整代码
import os
import cfg
import sys
import random
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5 import QtWidgets, QtGui
'''桌面宠物'''
class DesktopPet(QWidget):
def __init__(self, parent=None, **kwargs):
super(DesktopPet, self).__init__(parent)
# 初始化
self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.SubWindow)
self.setAutoFillBackground(False)
self.setAttribute(Qt.WA_TranslucentBackground, True)
self.repaint()
# 随机导入一个宠物
self.pet_images, iconpath = self.randomLoadPetImages()
# 设置退出选项
quit_action = QAction('退出', self, triggered=self.quit)
quit_action.setIcon(QIcon(iconpath))
self.tray_icon_menu = QMenu(self)
self.tray_icon_menu.addAction(quit_action)
self.tray_icon = QSystemTrayIcon(self)
self.tray_icon.setIcon(QIcon(iconpath))
self.tray_icon.setContextMenu(self.tray_icon_menu)
self.tray_icon.show()
# 当前显示的图片
self.image = QLabel(self)
self.setImage(self.pet_images[0][0])
# 是否跟随鼠标
self.is_follow_mouse = False
# 宠物拖拽时避免鼠标直接跳到左上角
self.mouse_drag_pos = self.pos()
# 显示(图片尺寸大小)
self.resize(512, 512)
self.randomPosition()
self.show()
# 宠物动画动作执行所需的一些变量
self.is_running_action = False
self.action_images = []
self.action_pointer = 0
self.action_max_len = 0
# 每隔一段时间做个动作
self.timer = QTimer()
self.timer.timeout.connect(self.randomAct)
self.timer.start(200)
'''随机做一个动作'''
def randomAct(self):
if not self.is_running_action:
self.is_running_action = True
self.action_images = random.choice(self.pet_images)
self.action_max_len = len(self.action_images)
self.action_pointer = 0
self.runFrame()
'''完成动作的每一帧'''
def runFrame(self):
if self.action_pointer == self.action_max_len:
self.is_running_action = False
self.action_pointer = 0
self.action_max_len = 0
self.setImage(self.action_images[self.action_pointer])
self.action_pointer += 1
'''设置当前显示的图片'''
def setImage(self, image):
self.image.setPixmap(QPixmap.fromImage(image))
'''随机导入一个桌面宠物的所有图片'''
def randomLoadPetImages(self):
pet_name = random.choice(list(cfg.PET_ACTIONS_MAP.keys()))
actions = cfg.PET_ACTIONS_MAP[pet_name]
pet_images = []
for action in actions:
pet_images.append(
[self.loadImage(os.path.join(cfg.ROOT_DIR, pet_name, '0-' + item + '.png')) for item in action])
iconpath = os.path.join(cfg.ROOT_DIR, pet_name, '0-1.png')
return pet_images, iconpath
'''鼠标左键按下时, 宠物将和鼠标位置绑定'''
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.is_follow_mouse = True
self.mouse_drag_pos = event.globalPos() - self.pos()
event.accept()
self.setCursor(QCursor(Qt.OpenHandCursor))
'''鼠标移动, 则宠物也移动'''
def mouseMoveEvent(self, event):
if Qt.LeftButton and self.is_follow_mouse:
self.move(event.globalPos() - self.mouse_drag_pos)
event.accept()
'''鼠标释放时, 取消绑定'''
def mouseReleaseEvent(self, event):
self.is_follow_mouse = False
self.setCursor(QCursor(Qt.ArrowCursor))
'''导入图像'''
def loadImage(self, imagepath):
image = QImage()
image.load(imagepath)
return image
'''随机到一个屏幕上的某个位置'''
def randomPosition(self):
screen_geo = QDesktopWidget().screenGeometry()
pet_geo = self.geometry()
width = (screen_geo.width() - pet_geo.width()) * random.random()
height = (screen_geo.height() - pet_geo.height()) * random.random()
self.move(width, height)
'''退出程序'''
def quit(self):
self.close()
sys.exit()
'''run'''
if __name__ == '__main__':
app = QApplication(sys.argv)
pet = DesktopPet()
sys.exit(app.exec_())
代码和图集项目放在GitHub上:Bing_Dwen_Dwen.git
大家想试试或者在这个基础上实现更多功能的可以自己完善呀
抢不到顶流的我终于也拥有属于自己的顶流啦
?
|