环境:
ubuntu19.10
xfce4
步骤如下:
python getpet.py(获取宠物动作图片)
然后会弹出来一个对话框,对话框里面你看中哪个宠物就用鼠标双击一下,然后终端就会开始下载这个宠物的图片
python start.py(启动桌面宠物)
小技巧:
可以把你下载的宠物给备份起来,以后就不用重复下载啦.
效果:
目前宠物总是喜欢待在最下面,脚本还需要改进啊.
#------------------------------------------------附录--------------------------------------------------------------------------------------
getpet.py
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import requests
import threading
import os
import zipfile
from PIL import Imageclass Workchange(QThread):"""docstring for WorkThread"""change_done = pyqtSignal()def __init__(self):super(Workchange, self).__init__()def run(self):url = self.urlname = url.split('/')[-1] + '.zip'print('正在下载资源',name)data = requests.get(url).contentwith open(name,'wb') as f:f.write(data)print('下载完成!')# path = os.environ['HOME']+'/shimeji/'path="./"if not os.path.exists(path):os.makedirs(path)print('正在解压资源...')z = zipfile.ZipFile(name, 'r')z.extractall(path=path)z.close()for i in range(1,47):os.rename(path + 'shime%s.png'% str(i),path+str(i)+'.png')pri_image = Image.open(path+'%s.png' %str(i))pri_image.transpose(Image.FLIP_LEFT_RIGHT).save(path+'-%s.png'%str(i))for i in os.listdir(path):os.system('convert %s %s'%(path+i,path+i))print('解压完成')os.remove(name)self.change_done.emit()class Workinit(QThread):"""docstring for WorkThread"""init_done = pyqtSignal(dict)def __init__(self):super(Workinit, self).__init__()def run(self):for i in range(3,90):threading.Thread(target=self.load,args=(i,)).start()def load(self,i):url = ''' http://pepeswap.com/thumb/%s''' %str(i)data = requests.get(url).contentname = str(i)dict_data = dict(data=data,name=name)self.init_done.emit(dict_data)class DQListWidget(QListWidget):def __init__(self):super(DQListWidget, self).__init__()self.workinit = Workinit()self.workchange = Workchange()self.workchange.change_done.connect(self.change_done)self.workinit.init_done.connect(self.load_picture)self.workinit.start()self.itemDoubleClicked.connect(self.change)def change_done(self):QMessageBox.about(self,'提示','更换完成!(*>﹏<*)')def change(self,item):self.workchange.url = 'http://pepeswap.com/mascot/' + item.text()self.workchange.start()def load_picture(self,dict_data): item = QListWidgetItem(dict_data['name'])item.setSizeHint(QSize(10, 128))self.addItem(item)pixmap = QPixmap()label = QLabel()pixmap.loadFromData(dict_data['data'])label.setPixmap(pixmap)self.setItemWidget(item,label)class Mywin(QWidget):def __init__(self):super(Mywin, self).__init__()list_ = DQListWidget() layout = QVBoxLayout(self)layout.addWidget(list_) self.setLayout(layout) app = QApplication(sys.argv)
mywin = Mywin() # 实例化一个窗口小部件
mywin.setWindowTitle('Hello world!') # 设置窗口标题
mywin.show() #显示窗口
sys.exit(app.exec())
start.py
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import time
import random
import osdef random_probability(r):s = random.randint(1,100)if r >= s:return Trueelse:return Falseclass Donghua(QWidget):def __init__(self):super(Donghua, self).__init__()self.desktop = QApplication.desktop()self.screenRect = self.desktop.screenGeometry()self.HEIGHT = self.screenRect.height()self.WIDTH = self.screenRect.width()self.setWindowFlags(Qt.FramelessWindowHint)# self.setWindowFlags(Qt.SplashScreen)self.setAttribute(Qt.WA_TranslucentBackground)self.timer = QTimer()self.timer.timeout.connect(self.update)self.img_timer = QTimer()self.img_timer.timeout.connect(self.update_img)self.label = QLabel(self)self.label.setScaledContents(True)layout = QVBoxLayout(self)layout.addWidget(self.label)self.setLayout(layout)self.index = 0self.x = 0self.is_right = Trueself.m_flag = Falseself.action_dcit = {'left_go':(1,3),'right_go':(-3,-1),'left_hold':(5,7),'right_hold':(-7,-5),'left_release':(4,4),'right_release':(-4,-4),'left_down':(18,19),'right_down':(-19,-18),'left_crawl':(12,14),'right_crawl':(-14,-12),# 'left_rest':(15,17),# 'right_rest':(-17,-15),'left_rest':(26,27),'right_rest':(-27,-26),'left_rest2':(27,29),'right_rest2':(-29,-27),}self.action = 'right_release'self.pre_action = ''# 控制窗体移动及各种动作切换计时器self.timer.start(100)# 控制图片切换计时器self.img_timer.start(200)#监听鼠标动作def mousePressEvent(self, event):x = (event.globalPos()-self.pos()).x()y = (event.globalPos()-self.pos()).y()if event.button() == Qt.LeftButton and x < 128 and y < 128:self.m_flag = Trueself.m_Position = event.globalPos()-self.pos() # 获取鼠标相对窗口的位置event.accept()if self.is_right:self.action='right_hold'else:self.action='left_hold'self.setCursor(QCursor(Qt.OpenHandCursor))def mouseMoveEvent(self, QMouseEvent):if Qt.LeftButton and self.m_flag:self.move(QMouseEvent.globalPos()-self.m_Position) # 更改窗口位置QMouseEvent.accept()#鼠标左键释放def mouseReleaseEvent(self, QMouseEvent):self.m_flag = Falseif self.is_right:self.action = 'right_release'else:self.action = 'left_release'#手动将其放入屏幕边缘if self.x <= 0 and self.y > 0:self.action = 'left_crawl'elif self.x + self.width >= self.WIDTH and self.y > 0:self.action = 'right_crawl'self.setCursor(QCursor(Qt.ArrowCursor))#动作更新def update(self):self.width = self.geometry().width()self.height = self.geometry().height()self.x = self.geometry().x()self.y = self.geometry().y()# print(self.x,self.y)exec('self.%s()'% self.action)def right_crawl(self):if self.y > 0:self.y -= 10self.setGeometry(self.x,self.y,100,100)else:self.action = 'right_release'def left_crawl(self):if self.y > 0:self.y -= 10self.setGeometry(self.x,self.y,100,100)else:self.action = 'left_release'def right_down(self):if self.index+1 == self.action_dcit[self.action][1]:self.action = 'right_go'def left_down(self):if self.index+1 == self.action_dcit[self.action][1]:self.action = 'left_go'def right_release(self):if self.y + self.height > self.HEIGHT:self.action = 'right_down'else:self.action = 'right_release'self.y += 100self.setGeometry(self.x,self.y,100,100)def left_release(self):if self.y + self.height > self.HEIGHT:self.action = 'left_down'else:self.action = 'left_release'self.y += 100self.setGeometry(self.x,self.y,100,100)def right_go(self):self.x += 5self.action = 'right_go'if self.x + self.width> self.WIDTH:if random_probability(30):self.action = 'right_crawl'else:self.is_right = Falseself.action = 'left_go'if random_probability(1):self.action = 'right_rest'self.setGeometry(self.x,self.y,100,100)def left_go(self):self.x -= 5self.action = 'left_go'if self.x < 0:if random_probability(30):self.action = 'left_crawl'else:self.is_right = Trueself.action = 'right_go'if random_probability(1):self.action = 'left_rest'self.setGeometry(self.x,self.y,100,100)def right_rest(self):if random_probability(1):self.action = 'right_go'def left_rest(self):if random_probability(1):self.action = 'left_go'def right_hold(self):passdef left_hold(self):pass#更新图片def update_img(self):start_index,end_index = self.action_dcit[self.action]if self.pre_action != self.action:self.index = start_indexself.pre_action = self.actionif self.index == end_index:self.index = start_indexelse:self.index += 1q = QPixmap(path +'%s.png' % self.index)self.label.setPixmap(q)if __name__ == '__main__':path = "./"app = QApplication(sys.argv)mywin = Donghua()mywin.show()sys.exit(app.exec())