前言 本文給大家分享的是如何通過利用Python製作桌面寵物,廢話不多直接開整~ 開發工具 Python版本: 3.6 相關模塊: random模塊 os模塊 cfg模塊 sys模塊 PyQt5模塊 環境搭建 安裝Python並添加到環境變數,pip安裝需要的相關模塊即可。 文中圖片素材實戰教程,評 ...
前言
本文給大家分享的是如何通過利用Python製作桌面寵物,廢話不多直接開整~
開發工具
Python版本: 3.6
相關模塊:
random模塊
os模塊
cfg模塊
sys模塊
PyQt5模塊
環境搭建
安裝Python並添加到環境變數,pip安裝需要的相關模塊即可。
文中圖片素材實戰教程,評論留言獲取。
桌面寵物的圖片素材
代碼實現
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(128, 128)
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(500)
#隨機做一個動作
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, 'shime'+item+'.png')) for item in action])
iconpath = os.path.join(cfg.ROOT_DIR, pet_name, 'shime1.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_())
效果展示
最後
今天的分享到這裡就結束了 ,感興趣的朋友也可以去試試哈
對文章有問題的,或者有其他關於python的問題,可以在評論區留言或者私信我哦
覺得我分享的文章不錯的話,可以關註一下我,或者給文章點贊(/≧▽≦)/