python桌面宠物V2,0
本帖最后由 kk159 于 2021-5-28 02:12 编辑V1.0剧情回顾
V2.0 新增功能,变换形态
效果演示:
1.新加另外一种外形
2.托盘添加变身按钮
图片素材:
完整代码:
# *_* coding : UTF-8 *_*
# author:Leemamas
# 开发时间:2021/5/280:48
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class TablePet(QWidget):
def __init__(self):
super(TablePet, self).__init__()
self.is_follow_mouse = False
self.mouse_drag_pos = self.pos()
# 每隔一段时间做个动作
self.timer = QTimer()
self.timer.timeout.connect(self.randomAct)
self.timer.start(100)
##僵尸形态
self.sharp=1
##皇帝的新衣
self.clothes = ''
self.clothes_key = 0
self.wardrobe(self.sharp)
self.initUi()
self.tray()
##衣柜
def wardrobe(self,key):
default = 'source\Zombie\Zombie_'
battle = 'source\ConeheadZombie\ConeheadZombie_'
if key == 0:
self.clothes=default
##TODO 根据文件数自动获取
self.clothes_key=21
else:
self.clothes=battle
self.clothes_key=20
##变身
def transformation(self):
if self.sharp==0:
self.wardrobe(1)
self.sharp=1
else:
self.wardrobe(0)
self.sharp=0
def randomAct(self):
# 读取图片不同的地址,实现动画效果
if self.key<self.clothes_key:
self.key+=1
else:
self.key=0
self.pic_url = self.clothes + str(self.key) + '.png'
self.pm = QPixmap(self.pic_url)
if not self.is_follow_mouse:
# 实现行进效果
if self.w>0:
self.w-=2
else:
self.w=1400
self.move(self.w, self.h)
self.lbl.setPixmap(self.pm)
def initUi(self):
screen = QDesktopWidget().screenGeometry()
self.w=1400
self.h=800
self.setGeometry(self.w,self.h,300,300)
# self.setWindowTitle('mypet')
self.lbl = QLabel(self)
self.key=0
self.pic_url=self.clothes+str(self.key)+'.png'
self.pm = QPixmap(self.pic_url)
self.lbl.setPixmap(self.pm)
# 背景透明等效果
self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.SubWindow)
self.setAutoFillBackground(False)
self.setAttribute(Qt.WA_TranslucentBackground, True)
self.show()
# self.repaint()
#系统托盘
def tray(self):
tp=QSystemTrayIcon(self)
tp.setIcon(QIcon('source\Zombie\Zombie_0.png'))
ation_quit= QAction('QUIT', self, triggered=self.quit)
transformation= QAction('transformation', self, triggered=self.transformation)
tpMenu=QMenu(self)
tpMenu.addAction(ation_quit)
tpMenu.addAction(transformation)
tp.setContextMenu(tpMenu)
tp.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)
xy=self.pos()
self.w,self.h=xy.x(),xy.y()
event.accept()
def mouseReleaseEvent(self, event):
self.is_follow_mouse = False
self.setCursor(QCursor(Qt.ArrowCursor))
def quit(self):
self.close()
sys.exit()
if __name__ == '__main__':
app=QApplication(sys.argv)
myPet=TablePet()
sys.exit(app.exec_()) 做了点小小改动 :lol,
# -*-coding:utf-8-*-
import sys
import time
from threading import Thread
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class TablePet(QWidget):
def __init__(self):
super(TablePet, self).__init__()
self.is_follow_mouse = False
self.mouse_drag_pos = self.pos()
self.initUi()
self.tray()
##衣柜
def wardrobe(self, key):
default = 'source\Zombie\Zombie_'
battle = 'source\ConeheadZombie\ConeheadZombie_'
if key == 0:
clothes = default
##TODO 根据文件数自动获取
clothes_key = 21
return clothes,int(clothes_key),int(key)
else:
clothes = battle
clothes_key = 20
return clothes, int(clothes_key), int(key)
##变身
def transformation(self):
if self.sharp == 0:
self.wardrobe(1)
self.sharp = 1
else:
self.wardrobe(0)
self.sharp = 0
def randomAct(self,key,clothes_key,clothes,lbl):
# 读取图片不同的地址,实现动画效果
while 1:
if key < clothes_key:
key += 1
else:
key = 0
pic_url = clothes + str(key) + '.png'
pm = QPixmap(pic_url)
if not self.is_follow_mouse:
# 实现行进效果
if self.w > 0 and self.w >1000:
print('self.x2:',self.x2)
if self.x2:
self.w += 2
if self.w > 1400:
self.x2 = False
else:
self.w -= 2
elif self.w <= 1000:
self.x2 = True
self.w += 2
else:
self.w = 1400
print('self.w', self.w,self.h)
self.move(self.w, self.h)
print(lbl)
lbl.setPixmap(pm)
time.sleep(0.6)
def initUi(self):
self.x2 = False
self.w = 1400
self.h = 800
self.setGeometry(self.w, self.h, 300, 300)
self.lbl = QLabel(self)
sharp = 1
clothes, clothes_key, key = self.wardrobe(sharp)
self.key = 0
self.pic_url = clothes + str(self.key) + '.png'
self.pm = QPixmap(self.pic_url)
self.lbl.setPixmap(self.pm)
print(clothes, clothes_key, key)
#
self.lbl2 = QLabel(self)
sharp2 = 0
clothes2,clothes_key2,key2 = self.wardrobe(sharp2)
self.pic_url2 = clothes2 + str(self.key) + '.png'
self.pm2 = QPixmap(self.pic_url2)
self.lbl2.setPixmap(self.pm2)
# 背景透明等效果
self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.SubWindow)
self.setAutoFillBackground(True)
self.setAttribute(Qt.WA_TranslucentBackground, True)
vbox = QVBoxLayout()
vbox.addWidget(self.lbl)
vbox.addWidget(self.lbl2)
self.setLayout(vbox)
# 系统托盘
def tray(self):
tp = QSystemTrayIcon(self)
tp.setIcon(QIcon('source\Zombie\Zombie_0.png'))
ation_quit = QAction('QUIT', self, triggered=self.quit)
transformation = QAction('transformation', self, triggered=self.transformation)
tpMenu = QMenu(self)
tpMenu.addAction(ation_quit)
tpMenu.addAction(transformation)
tp.setContextMenu(tpMenu)
tp.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)
xy = self.pos()
self.w, self.h = xy.x(), xy.y()
event.accept()
def mouseReleaseEvent(self, event):
self.is_follow_mouse = False
self.setCursor(QCursor(Qt.ArrowCursor))
def quit(self):
self.close()
sys.exit()
def a(self):
while 1:
print('7')
def run(self):
ls = []
for i in :
if i == 1:
print(4)
clothes, clothes_key, key = myPet.wardrobe(i)
g = Thread(target=myPet.randomAct, args=(key, clothes_key, clothes, self.lbl))
ls.append(g)
elif i == 0:
print(5)
clothes2, clothes_key2, key2 = myPet.wardrobe(i)
g2 = Thread(target=myPet.randomAct, args=(key2, clothes_key2, clothes2, self.lbl2))
ls.append(g2)
for j in ls:
j.start()
time.sleep(0.3)
if __name__ == '__main__':
app = QApplication(sys.argv)
myPet = TablePet()
myPet.run()
myPet.show()
sys.exit(app.exec_()) 刚看了第一个帖子!你这都2.0了?????学习了 真的厉害! 我觉得作者可以再加一个快捷键隐藏,关闭的方法。。。 虔来学习 发表于 2021-5-28 05:41
我觉得作者可以再加一个快捷键隐藏,关闭的方法。。。
good point! 感谢分享 解压小软件,哈哈 有点意思,感谢分享 感谢分享 感谢分享 感谢分享思路尝试一下