赞
踩
大家曾经记忆里的回忆,是不是腾讯企鹅的。它又萌又可爱,如图:
但是我们多么想制作一款自己的智能宠物啊,今天我们就将带你手把手制作桌宠。最主要的是文末我将给出源代码哦!大家可以DIY设计自己专属的桌面宠物和其智能功能。
其特点之一:
会运动,还会变身:
特点之二:
会说话聊天:
特点之三:
右键多功能性(可语音控制,连接WiFi等,最主要学完这篇文章之后可以自己添加功能):
代码如下:
class newWindow(QWidget): def __init__(self, parent=None): super(newWindow, self).__init__(parent) self.i = 1 self.mypix() self.move(1750,50) self.timer = QTimer() self.timer.setInterval(500) self.timer.timeout.connect(self.timeChanged) self.timer.start() self.setWindowFlags(Qt.FramelessWindowHint) # 去除界面边框 self.setWindowFlags(Qt.WindowStaysOnTopHint) self.setAttribute(Qt.WA_TranslucentBackground) # 背景透明 self.setMouseTracking(False) # 设置鼠标移动跟踪是否有效 self.initUI() def mypix1(self): self.update() if self.i == 6:#散步 time.sleep(5) elif self.i ==11:#甩膀子 time.sleep(1) elif self.i ==16:#唱歌 time.sleep(1) elif self.i ==21:#拖东西 time.sleep(1) elif self.i ==26:#坐在地上无聊 time.sleep(1) elif self.i ==31:#坐在地上沮丧 time.sleep(1) elif self.i ==36:#爬起来 time.sleep(1) elif self.i ==43:#准备变红 time.sleep(1) elif self.i ==48:#变红 time.sleep(1) elif self.i ==54:#红色状态 time.sleep(1) elif self.i ==56:#返回 self.i = 1 time.sleep(2) self.mypic = {1: '.\img\shime1.png', 2: '.\img\shime1.png', 3: '.\img\shime1.png', 4: '.\img\shime1.png',5: '.\img\shime1.png',6: '.\img\shime2.png',7: '.\img\shime3.png',8: '.\img\shime2.png', 9: '.\img\shime3.png' , 10: '.\img\shime3.png',11: '.\img\shime5.png',12: '.\img\shime6.png',13: '.\img\shime5.png',14: '.\img\shime6.png', 15: '.\img\shime6.png',16: '.\img\shime47.png', 17: '.\img\shime48.png',18: '.\img\shime49.png',19: '.\img\shime50.png',20: '.\img\shime50.png',21: '.\img\shime38.png',22: '.\img\shime39.png',23: '.\img\shime40.png',24: '.\img\shime41.png', 25: '.\img\shime30.png',26: '.\img\shime31.png',27: '.\img\shime32.png',28: '.\img\shime33.png',29: '.\img\shime33.png',30: '.\img\shime16.png',31: '.\img\shime15.png',32: '.\img\shime16.png' , 33: '.\img\shime17.png',34: '.\img\shime18.png',35: '.\img\shime19.png',36: '.\img\shime19.png',37: '.\img\shime19.png',38: '.\img\shime20.png',39: '.\img\shime20.png',40: '.\img\shime21.png' , 41: '.\img\shime22.png',42: '.\img\shime26.png',43: '.\img\shime27.png',44: '.\img\shime28.png',45: '.\img\shime29.png',46: '.\img\shime30.png',47: '.\img\shime47.png',48: '.\img\shime46.png', 49: '.\img\shime45.png',50: '.\img\shime44.png',51: '.\img\shime43.png',52: '.\img\shime42.png',53: '.\img\shime42.png',54: '.\img\shime43.png',55: '.\img\shime42.png',56: '.\img\shime43.png'} self.pix = QPixmap(self.mypic[self.i], '0', Qt.AvoidDither | Qt.ThresholdAlphaDither | Qt.ThresholdDither) self.resize(self.pix.size()) self.setMask(self.pix.mask()) self.dragPosition = None def mousePressEvent(self, event): QtCore.Qt.NoButton - 0 - 没有按下鼠标键 QtCore.Qt.LeftButton -1 -按下鼠标左键 QtCore.Qt.RightButton -2 -按下鼠标右键 QtCore.Qt.Mion 或 QtCore.Qt.MiddleButton -4 -按下鼠标中键 nn = event.buttons() def mouseReleaseEvent(self, event): print('鼠标键放开了') # 显示不规则图片 def mypix(self): self.update() self.pix=QPixmap('.\img\shime1.png','0',Qt.AvoidDither|Qt.ThresholdAlphaDither|Qt.ThresholdDither) self.resize(self.pix.size()) self.setMask(self.pix.mask()) def mouseMoveEvent(self, event): # 鼠标键移动时调用 ret = self.hasMouseTracking() #print('鼠标移动了:%s' % ret) x = event.x() # 返回鼠标相对于窗口的x轴坐标 y = event.y() # 返回鼠标相对于窗口的y轴坐标 # print('鼠标x坐标:%s ,鼠标y坐标:%s' % (x, y)) xy = event.pos() s = self.mapToGlobal(xy) # 将窗口坐标转换成屏幕坐标.属于QWidget类的方法;参数类型QPoint #print('鼠标x坐标:%s ,鼠标y坐标:%s' % (s.x(), s.y())) self.move(s.x()-75, s.y()-100) self.update() self.pix = QPixmap('.\img\shime4.png', '0', Qt.AvoidDither | Qt.ThresholdAlphaDither | Qt.ThresholdDither) self.resize(self.pix.size()) self.setMask(self.pix.mask()) xy1 = event.globalPos() # 返回鼠标相对于屏幕的坐标。PyQt5.QtCore.QPoint(1096, 37)【用xy1.x() xy1.y()提取值】 s1 = self.mapFromGlobal(xy1) # 将屏幕坐标转换成窗口坐标.属于QWidget类的方法;参数类型QPoint # mapToParent(QPoint) - 将窗口坐标转换成父窗口坐标。如果没有父窗口,则相当于mapToGlobal (QPoint) # mapFromParent(QPoint) - 将父窗口坐标转换成窗口坐标。如果没有父窗口,则相当于mapFromGlobal(QPoint) # mapTo (QWidget, QPoint) - 将窗口坐标转换成 QWidget父窗口坐标 px = event.globalX() # 返回相对于屏幕的x坐标 py = event.globalY() # 返回相对于屏幕的y坐标 s = event.windowPos() # 相对于窗口的坐标(保留一位小数),PyQt5.QtCore.QPointF(481.0, 1.0)【用s.x() s.y()提取值】 p = event.screenPos() # 相对于屏幕的坐标(保留一位小数).PyQt5.QtCore.QPointF(476.0, 49.0)【用p.x() p.y()提取值】 t = event.timestamp()
完整代码,欢迎大家关注我的公众号回复“制作桌宠”
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。