PYthon写一个桌面宠物 大神勿喷
基于Pyqt5实现,最终效果:
实现效果单一,僵尸在桌面自行移动,托盘。
其他功能自行完善。。。。
视频教程演示:https://www.bilibili.com/video/BV14h411v7Y8/
知识点:
1.创建一个简单的应用程序
from PyQt5.QtWidgets import *
import sys
class Test(QWidget):
def __init__(self):
super(Test, self).__init__()
self.initUi()
def initUi(self):
#窗口位置,大小
self.setGeometry(300, 300, 300, 300)
#标题
self.setWindowTitle('test')
#展示
self.show()
if __name__ == '__main__':
app=QApplication(sys.argv)
test=Test()
sys.exit(app.exec_())2.加载宠物图片,窗体透明
##导入类库
from PyQt5.QtGui import *
from PyQt5.QtCore import *
##在initUi类里面添加
#加载图片
self.lbl = QLabel(self)
self.key = 0
self.pic_url = 'source\Zombie\Zombie_0.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)3.增加托盘方法,在__init__调用
def __init__(self):
super(Test, self).__init__()
##调用方法
self.tray()
# 系统托盘
def tray(self):
##托盘图标
tp = QSystemTrayIcon(self)
tp.setIcon(QIcon('source\Zombie\Zombie_0.png'))
ation_quit = QAction('QUIT', self)
tpMenu = QMenu(self)
tpMenu.addAction(ation_quit)
tp.setContextMenu(tpMenu)
tp.show()4.托盘退出方法
ation_quit = QAction('QUIT', self, triggered=self.quit)
def quit(self):
self.close()
sys.exit()5.让图片动起来
def __init__(self):
。。。。。
# 每隔一段时间做个动作
self.timer = QTimer()
self.timer.timeout.connect(self.randomAct)
self.timer.start(100)
def randomAct(self):
# 读取图片不同的地址,实现动画效果
if self.key<21:
self.key+=1
else:
self.key=0
self.pic_url = 'source\Zombie\Zombie_' + str(self.key) + '.png'
self.pm = QPixmap(self.pic_url)
self.lbl.setPixmap(self.pm)
def initUi(self):
##修改为这样
self.pic_url='source\Zombie\Zombie_'+str(self.key)+'.png'6.让宠物移动
def randomAct(self):
。。。。。。
# 实现行进效果
if self.w > 0:
self.w -= 2
else:
self.w = 1400
self.move(self.w, self.h)
。。。。。。。。。。
def initUi(self):
self.w = 1400
self.h = 800
#窗口位置,大小
self.setGeometry( self.w ,self.h, 300, 300)7.实现鼠标拖放
def __init__(self):
。。。。
self.is_follow_mouse = False
self.mouse_drag_pos = self.pos()
def randomAct(self):
# 读取图片不同的地址,实现动画效果
if self.key<21:
self.key+=1
else:
self.key=0
self.pic_url = 'source\Zombie\Zombie_' + 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 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))完整代码:
# *_* coding : UTF-8 *_*
# author:Leemamas
# 开发时间:2021/5/203:06
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.initUi()
self.tray()
self.is_follow_mouse = False
self.mouse_drag_pos = self.pos()
# 每隔一段时间做个动作
self.timer = QTimer()
self.timer.timeout.connect(self.randomAct)
self.timer.start(100)
def randomAct(self):
# 读取图片不同的地址,实现动画效果
if self.key<21:
self.key+=1
else:
self.key=0
self.pic_url = 'source\Zombie\Zombie_' + 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='source\Zombie\Zombie_'+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)
tpMenu=QMenu(self)
tpMenu.addAction(ation_quit)
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_())
页:
[1]