Creating a Desktop Pet with Python and PyQt5: Full Source Code and Packaging Guide
This article explains how to build a desktop pet application using Python and PyQt5, covering required GIF assets, multiple methods for creating or sourcing those GIFs, the project directory layout, detailed source code walkthrough, and steps to package the app with PyInstaller.
The tutorial starts by describing the visual assets needed for a desktop pet, emphasizing the use of multiple white‑background GIFs that will be stitched together to animate the pet’s actions.
It then outlines three ways to obtain GIFs: (1) converting video to GIF with Premiere (PR), (2) using Photoshop’s timeline to create GIFs, and (3) searching online via Baidu for ready‑made GIF resources.
Next, the article presents the project directory structure, showing that main.py is the entry point and that subfolders such as normal/ and click/ store the animation files, while dialog.txt holds the pet’s dialogue lines.
<code>import os import sys import random from PyQt5.QtGui import * from PyQt5.QtCore import * from PyQt5.QtWidgets import *</code>
The core of the application is the DesktopPet class, a subclass of QWidget . Its __init__ method calls several helper functions:
init() – configures a frameless, always‑on‑top window with a transparent background.
initPall() – creates a system‑tray icon, adds a context menu with "Show" and "Exit" actions, and displays the tray icon.
initPetImage() – loads the static GIFs, sets up a QLabel for the animation, reads the dialogue file, and randomly positions the pet on the screen.
petNormalAction() – starts two QTimer objects that periodically trigger random animation changes and dialogue updates.
Key methods for interactivity include:
randomAct() – selects a random GIF from the normal animation list or, when the pet is clicked, plays a special click animation.
talk() – picks a random line from dialog.txt and displays it with styled text.
mousePressEvent , mouseMoveEvent , and mouseReleaseEvent – enable dragging the pet around the screen and change its state on click.
enterEvent – changes the cursor to a closed‑hand shape when the mouse hovers over the pet.
contextMenuEvent – provides a right‑click menu to hide or exit the application.
The script finishes with the usual PyQt5 entry point:
<code>if __name__ == '__main__':
app = QApplication(sys.argv)
pet = DesktopPet()
sys.exit(app.exec_())</code>After the development part, the guide explains how to package the program into a standalone executable using PyInstaller:
<code>pip install pyinstaller
pyinstaller -F -w main.py</code>It shows screenshots of the packaging process, the resulting dist folder, and notes that the generated main.exe must be placed in the project’s root directory to run correctly.
Finally, the article provides a Baidu Cloud link to download the full project, a summary of the author’s experience, and a promotional QR code for a free Python course.
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.