Creating a Desktop Pet with Python and PyQt5: Materials, Implementation Code, and Packaging Guide
This article explains how to gather GIF assets, write a PyQt5 desktop‑pet application in Python—including directory layout, window initialization, tray integration, random positioning, mouse interaction, and context menus—and finally package the program into an executable using PyInstaller.
1. Desktop‑Pet Assets
The pet’s animations are built from multiple GIF files; you should collect several white‑background GIFs and can obtain them via Premiere Pro export, Photoshop frame animation, or simply download existing GIFs from the web.
2. Python Implementation
2.1 Project Structure
The main script is main.py . Supporting folders contain normal/ GIFs for idle actions, click/ GIFs for click actions, a tigerIcon.jpg for the system tray, and a dialog.txt file with dialogue lines.
2.2 Core Code
Import required modules:
<code>import os
import sys
import random
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *</code>Define the DesktopPet widget, initialize the window (frameless, always‑on‑top, transparent), load GIFs with QMovie , set up the system tray, and start timers for random actions and dialogue.
Key methods include:
init() – configure window flags and transparency.
initPall() – create the tray icon and its menu (Show / Quit).
initPetImage() – load static GIFs and dialogue.
randomPosition() – place the pet at a random screen location.
petNormalAction() – use QTimer to trigger randomAct() and talk() every few seconds.
randomAct() – switch between idle GIFs or the click GIF.
talk() – display a random line from dialog.txt with styled text.
Mouse event handlers ( mousePressEvent , mouseMoveEvent , mouseReleaseEvent ) enable dragging the pet and change its state on click.
enterEvent – change the cursor to a closed hand when hovering.
contextMenuEvent – provide a right‑click menu to hide or quit the pet.
Application entry point:
<code>if __name__ == '__main__':
app = QApplication(sys.argv)
pet = DesktopPet()
sys.exit(app.exec_())</code>3. Packaging the Application
Install PyInstaller:
<code>pip install pyinstaller</code>Run the following command in the project directory to create a single‑file, window‑less executable:
<code>pyinstaller -F -w main.py</code>The resulting main.exe appears in the dist folder; moving it to the project root may be required for proper resource loading.
4. Summary and References
The tutorial concludes with a brief personal note and provides links to additional Python learning resources, a Baidu Cloud folder containing the full project, and several related articles for further exploration.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.