Introduction:

Many friends have never had a pet before

But you must have had it

Left left left

​​

That’s right

QQ pet

​​

I remember when I was in elementary school and junior high school,

For QQ pet never tired.

In order to give it something good to eat,

If you’re sick, you have to get shots,

When you’re unhappy, play games with it,

Do so much ~

I just want it to grow up,

So that I can get married with my favorite boy’s QQ pet.

​​

Although I haven’t played much since high school

But when I look back, it’s full of memories,

However, in the following 18 years, I said goodbye to QQ pets.

​​​

This is how many generations of youth ah ~

Today xiaobi words to revisit the classic, using Python hand by hand to teach you to make an interface of “QQ pet” desktop version ~

The body of the

Environmental Installation Part (1) :

**Python**** version: **3.6

** Related modules: **PyQt5 module; And some modules that come with Python.

Picture material part (2) : about 50 kinds of materials were downloaded, and different desktop pets were randomly generated each time the code was run.

​​​​​​​​​The above are 3 groups of partial pictures opened randomly and there are many others…… Do whatever you like, huh

The desktop pet made today is based on the PyQt5 module.

First, let’s initialize a desktop pet window component:

​​

The function code of randomly importing all pictures of a pet is as follows:

​​​​​​​

Small pets doing random actions:

​​​​

Where pets appear on the desktop is random, which is more interesting:

​​​​​

​​Two random screenshots, actually giFs, but I don’t know how to record a video, so you’ll have to watch

Ps: I will learn it later. Send four!!!!!

​​​​​​​​​​​

​​​​​

Attached to the source code:

import os import cfg import sys import random from PyQt5.QtGui import * from PyQt5.QtCore import * from PyQt5.QtWidgets Import * from PyQt5 import QtWidgets, QtGui "" "class DesktopPet(QWidget): def __init__(self, parent=None, **kwargs): super(DesktopPet, Self.) __init__ (parent) # initialization self. SetWindowFlags (Qt. FramelessWindowHint | Qt. WindowStaysOnTopHint | Qt. The SubWindow) self.setAutoFillBackground(False) self.setAttribute(Qt.WA_TranslucentBackground, True) self.repaint() # import a random pet self.pet_images, Iconpath = self.randomloadpetimages () # set quit_action = QAction(' exit ', self, triggered=self.quit) quit_action.setIcon(QIcon(iconpath)) self.tray_icon_menu = QMenu(self) self.tray_icon_menu.addAction(quit_action) self.tray_icon = QSystemTrayIcon(self) self.tray_icon.setIcon(QIcon(iconpath)) self.tray_icon.setContextMenu(self.tray_icon_menu) self.tray_icon.show() # Self.image = QLabel(self) self.setimage (self.pet_images[0][0]) # Self.mouse_drag_pos = self.pos() # show self.resize(128, 128) self.randomPosition() self.show() # is_running_action = False self.action_images = [] Self.action_pointer = 0 self.action_max_len = 0 # Do an action every time self.timer = QTimer() Self. The timer. The timeout. Connect (self. RandomAct) self. The timer. The start (500) ' ' 'random do an action' 'def randomAct (self) : if not self.is_running_action: self.is_running_action = True self.action_images = random.choice(self.pet_images) self.action_max_len = Len (self.action_images) self.action_pointer = 0 self.runframe () "def runFrame(self): len(self.action_images) self.action_pointer = 0 self.runframe ()" if self.action_pointer == self.action_max_len: self.is_running_action = False self.action_pointer = 0 self.action_max_len = 0 Self.setimage (self.action_images[self.action_pointer]) self.action_pointer += 1 "def setImage(self, self.action_pointer) self.action_pointer += 1" Def randomLoadPetImages(self): self.image.setpixMap (qpixmap.fromImage (image)) def randomLoadPetImages(self): pet_name = random.choice(list(cfg.PET_ACTIONS_MAP.keys())) actions = cfg.PET_ACTIONS_MAP[pet_name] pet_images = [] for action in actions: pet_images.append([self.loadImage(os.path.join(cfg.ROOT_DIR, pet_name, 'shime'+item+'.png')) for item in action]) iconpath = os.path.join(cfg.ROOT_DIR, pet_name, PNG) return pet_images, iconpath "" 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): 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) event.accept() "" def mouseReleaseEvent(self, event): Self.is_follow_mouse = False self.setcursor (QCursor(qt.arrowcursor)) def loadImage(self, imagepath): self.is_follow_mouse = False self.setcursor (QCursor(qt.arrowcursor)) Image = QImage() image.load(imagepath) return image "" "def randomPosition(self): screen_geo = QDesktopWidget().screenGeometry() pet_geo = self.geometry() width = (screen_geo.width() - pet_geo.width()) * random.random() height = (screen_geo.height() - pet_geo.height()) * random.random() self.move(width, Exit height) "'" 'def quit (self) : the self, close () sys. The exit ()' ' 'run' ' 'if __name__ = = "__main__" : app = QApplication(sys.argv) pet = DesktopPet() sys.exit(app.exec_())Copy the code

conclusion

All right! So much for desktop pets! What kind of pet do you like to make by yourself

Complete project material, source code and other source code base for free: #959755565 #

​​