查看: 919|回复: 1

[技术分享] PYthon写一个桌面宠物 大神勿喷

[复制链接]
累计签到:357 天
连续签到:1 天

51

主题

312

回帖

6257

积分

星主

名望
236
星币
3837
星辰
7
好评
15

欢乐天使奖灌水天才奖鼎力支持奖幸运猪星辰勋章优秀会员奖明星会员奖魅力会员奖

QQ
发表于 2022-8-8 16:01:39 | 显示全部楼层 |阅读模式

注册登录后全站资源免费查看下载

您需要 登录 才可以下载或查看,没有账号?立即注册

×
基于Pyqt5实现,
最终效果

微信截图_20220808155557.png
实现效果单一,僵尸在桌面自行移动,托盘。
其他功能自行完善。。。。

视频教程演示:https://www.bilibili.com/video/BV14h411v7Y8/
知识点:
1.创建一个简单的应用程序
  1. from PyQt5.QtWidgets import *
  2. import sys

  3. class Test(QWidget):
  4.     def __init__(self):
  5.         super(Test, self).__init__()
  6.         self.initUi()

  7.     def initUi(self):
  8.         #窗口位置,大小
  9.         self.setGeometry(300, 300, 300, 300)
  10.         #标题
  11.         self.setWindowTitle('test')
  12.         #展示
  13.         self.show()

  14. if __name__ == '__main__':
  15.     app=QApplication(sys.argv)
  16.     test=Test()
  17.     sys.exit(app.exec_())
复制代码
2.加载宠物图片,窗体透明
  1. ##导入类库
  2. from PyQt5.QtGui import *
  3. from PyQt5.QtCore import *
  4. ##在initUi类里面添加  
  5. #加载图片
  6.         self.lbl = QLabel(self)
  7.         self.key = 0
  8.         self.pic_url = 'source\Zombie\Zombie_0.png'
  9.         self.pm = QPixmap(self.pic_url)
  10.         self.lbl.setPixmap(self.pm)

  11.         # 背景透明等效果
  12.         self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.SubWindow)
  13.         self.setAutoFillBackground(False)
  14.         self.setAttribute(Qt.WA_TranslucentBackground, True)
复制代码
3.增加托盘方法,在__init__调用
  1. def __init__(self):
  2.         super(Test, self).__init__()
  3.         ##调用方法
  4.         self.tray()
  5. # 系统托盘
  6.     def tray(self):
  7.         ##托盘图标
  8.         tp = QSystemTrayIcon(self)
  9.         tp.setIcon(QIcon('source\Zombie\Zombie_0.png'))
  10.         ation_quit = QAction('QUIT', self)
  11.         tpMenu = QMenu(self)
  12.         tpMenu.addAction(ation_quit)
  13.         tp.setContextMenu(tpMenu)
  14.         tp.show()
复制代码
4.托盘退出方法
  1. ation_quit = QAction('QUIT', self, triggered=self.quit)
  2.    def quit(self):
  3.        self.close()
  4.        sys.exit()
复制代码
5.让图片动起来
  1. def __init__(self):
  2.       。。。。。

  3.         # 每隔一段时间做个动作
  4.         self.timer = QTimer()
  5.         self.timer.timeout.connect(self.randomAct)
  6.         self.timer.start(100)

  7.     def randomAct(self):
  8.         # 读取图片不同的地址,实现动画效果
  9.         if self.key<21:
  10.             self.key+=1
  11.         else:
  12.             self.key=0

  13.         self.pic_url = 'source\Zombie\Zombie_' + str(self.key) + '.png'
  14.         self.pm = QPixmap(self.pic_url)
  15.         self.lbl.setPixmap(self.pm)

  16.     def initUi(self):
  17.       
  18. ##修改为这样  
  19. self.pic_url='source\Zombie\Zombie_'+str(self.key)+'.png'
复制代码
6.让宠物移动
  1. def randomAct(self):
  2.      。。。。。。
  3.     # 实现行进效果
  4.     if self.w > 0:
  5.         self.w -= 2
  6.     else:
  7.         self.w = 1400
  8.     self.move(self.w, self.h)
  9.   。。。。。。。。。。
  10. def initUi(self):
  11.     self.w = 1400
  12.     self.h = 800
  13.     #窗口位置,大小
  14.     self.setGeometry( self.w ,self.h, 300, 300)
复制代码
7.实现鼠标拖放
  1. def __init__(self):
  2.         。。。。
  3.         self.is_follow_mouse = False
  4.         self.mouse_drag_pos = self.pos()
  5.     def randomAct(self):
  6.         # 读取图片不同的地址,实现动画效果
  7.         if self.key<21:
  8.             self.key+=1
  9.         else:
  10.             self.key=0

  11.         self.pic_url = 'source\Zombie\Zombie_' + str(self.key) + '.png'
  12.         self.pm = QPixmap(self.pic_url)
  13.         if not self.is_follow_mouse:
  14.             # 实现行进效果
  15.             if self.w>0:
  16.                 self.w-=2
  17.             else:
  18.                 self.w=1400
  19.             self.move(self.w, self.h)
  20.         self.lbl.setPixmap(self.pm)
  21. #鼠标事件
  22.     def mousePressEvent(self, event):
  23.         if event.button() == Qt.LeftButton:
  24.             self.is_follow_mouse = True
  25.             self.mouse_drag_pos = event.globalPos() - self.pos()
  26.             event.accept()
  27.             self.setCursor(QCursor(Qt.OpenHandCursor))

  28.     def mouseMoveEvent(self, event):
  29.         if Qt.LeftButton and self.is_follow_mouse:
  30.             self.move(event.globalPos() - self.mouse_drag_pos)
  31.             xy=self.pos()
  32.             self.w,self.h=xy.x(),xy.y()
  33.             event.accept()

  34.     def mouseReleaseEvent(self, event):
  35.         self.is_follow_mouse = False
  36.         self.setCursor(QCursor(Qt.ArrowCursor))
复制代码
完整代码
  1. # *_* coding : UTF-8 *_*
  2. # author  :  Leemamas
  3. # 开发时间  :  2021/5/20  3:06

  4. import sys
  5. from PyQt5.QtGui import *
  6. from PyQt5.QtCore import *
  7. from PyQt5.QtWidgets import *

  8. class TablePet(QWidget):
  9.     def __init__(self):
  10.         super(TablePet, self).__init__()
  11.         self.initUi()
  12.         self.tray()

  13.         self.is_follow_mouse = False
  14.         self.mouse_drag_pos = self.pos()
  15.         # 每隔一段时间做个动作
  16.         self.timer = QTimer()
  17.         self.timer.timeout.connect(self.randomAct)
  18.         self.timer.start(100)


  19.     def randomAct(self):
  20.         # 读取图片不同的地址,实现动画效果
  21.         if self.key<21:
  22.             self.key+=1
  23.         else:
  24.             self.key=0

  25.         self.pic_url = 'source\Zombie\Zombie_' + str(self.key) + '.png'
  26.         self.pm = QPixmap(self.pic_url)
  27.         if not self.is_follow_mouse:
  28.             # 实现行进效果
  29.             if self.w>0:
  30.                 self.w-=2
  31.             else:
  32.                 self.w=1400
  33.             self.move(self.w, self.h)
  34.         self.lbl.setPixmap(self.pm)



  35.     def initUi(self):
  36.         screen = QDesktopWidget().screenGeometry()
  37.         self.w=1400
  38.         self.h=800
  39.         self.setGeometry(self.w,self.h,300,300)
  40.         # self.setWindowTitle('mypet')
  41.         self.lbl = QLabel(self)
  42.         self.key=0
  43.         self.pic_url='source\Zombie\Zombie_'+str(self.key)+'.png'
  44.         self.pm = QPixmap(self.pic_url)
  45.         self.lbl.setPixmap(self.pm)

  46.         # 背景透明等效果
  47.         self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.SubWindow)
  48.         self.setAutoFillBackground(False)
  49.         self.setAttribute(Qt.WA_TranslucentBackground, True)
  50.         self.show()
  51.         # self.repaint()


  52.     #系统托盘
  53.     def tray(self):
  54.         tp=QSystemTrayIcon(self)
  55.         tp.setIcon(QIcon('source\Zombie\Zombie_0.png'))
  56.         ation_quit= QAction('QUIT', self, triggered=self.quit)
  57.         tpMenu=QMenu(self)
  58.         tpMenu.addAction(ation_quit)
  59.         tp.setContextMenu(tpMenu)
  60.         tp.show()

  61.     #鼠标事件
  62.     def mousePressEvent(self, event):
  63.         if event.button() == Qt.LeftButton:
  64.             self.is_follow_mouse = True
  65.             self.mouse_drag_pos = event.globalPos() - self.pos()
  66.             event.accept()
  67.             self.setCursor(QCursor(Qt.OpenHandCursor))

  68.     def mouseMoveEvent(self, event):
  69.         if Qt.LeftButton and self.is_follow_mouse:
  70.             self.move(event.globalPos() - self.mouse_drag_pos)
  71.             xy=self.pos()
  72.             self.w,self.h=xy.x(),xy.y()
  73.             event.accept()

  74.     def mouseReleaseEvent(self, event):
  75.         self.is_follow_mouse = False
  76.         self.setCursor(QCursor(Qt.ArrowCursor))

  77.     def quit(self):
  78.         self.close()
  79.         sys.exit()


  80. if __name__ == '__main__':
  81.     app=QApplication(sys.argv)
  82.     myPet=TablePet()
  83.     sys.exit(app.exec_())
复制代码




回复

使用道具 举报

累计签到:52 天
连续签到:1 天

1

主题

26

回帖

235

积分

星碎

名望
0
星币
185
星辰
0
好评
0
发表于 2022-11-13 19:11:13 | 显示全部楼层
6
默认签名:偏爱是我家,发展靠大家! 社区反馈邮箱Mail To:service@pai.al或paijishu@outlook.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|偏爱技术社区-偏爱技术吧-源码-科学刀-我爱辅助-娱乐网--教开服-游戏源码

偏爱技术社区-偏爱技术吧-源码-科学刀-我爱辅助-娱乐网-游戏源码

Powered by Discuz! X3.5

GMT+8, 2024-5-4 23:29 , Processed in 0.092847 second(s), 41 queries .

快速回复 返回顶部 返回列表