Python Desktop Pet with PyQt5 – Implementation Guide
This article explains how to create a floating desktop pet using Python and PyQt5, covering required packages, project structure, GIF handling, random actions, dialogue display, and steps to package the application into an executable file.
The desktop pet is built by randomly switching GIF animations; you can replace the GIFs yourself and the source code is provided at the end of the article.
The final effect is a pet that floats above all windows, moves, can be clicked, interacts, and can be dragged freely.
Project resources include dialog.txt for pet dialogues, tigerIcon.jpg for the tray icon, and GIF files placed in normal/ and click/ directories.
Required Python packages (install via pip ) are:
<code>import os</code><code>import sys</code><code>import random</code><code>from PyQt5.QtGui import *</code><code>from PyQt5.QtCore import *</code><code>from PyQt5.QtWidgets import *</code>The program’s main flow consists of four key methods:
self.init() – initializes the window and starts the pet GIF.
self.initPall() – configures system tray behavior.
self.initPetImage() – loads static GIF resources, dialogues, and images.
self.petNormalAction() – enables random action switching and dialogue updates.
To package the script as an executable, run:
<code>pip install pyinstaller</code><code>pyinstaller -F -w main.py</code>Pet image loading
<code>def initPetImage(self):</code><code> self.talkLabel = QLabel(self)</code><code> self.talkLabel.setStyleSheet("font:15pt '楷体';border-width: 1px;color:blue;")</code><code> self.image = QLabel(self)</code><code> self.movie = QMovie("normal/normal1.gif")</code><code> self.movie.setScaledSize(QSize(200, 200))</code><code> self.image.setMovie(self.movie)</code><code> self.movie.start()</code><code> self.resize(1024, 1024)</code><code> self.randomPosition()</code><code> self.show()</code><code> self.pet1 = []</code><code> for i in os.listdir("normal"):</code><code> self.pet1.append("normal/" + i)</code><code> self.dialog = []</code><code> with open("dialog.txt", "r") as f:</code><code> text = f.read()</code><code> self.dialog = text.split("\n")</code>Pet normal action (random GIF switching)
<code>def petNormalAction(self):</code><code> self.timer = QTimer()</code><code> self.timer.timeout.connect(self.randomAct)</code><code> self.timer.start(3000)</code><code> self.condition = 0</code><code> self.talkTimer = QTimer()</code><code> self.talkTimer.timeout.connect(self.talk)</code><code> self.talkTimer.start(3000)</code><code> self.talk_condition = 0</code><code> self.talk()</code>Random action switching
<code>def randomAct(self):</code><code> if not self.condition:</code><code> self.movie = QMovie(random.choice(self.pet1))</code><code> self.movie.setScaledSize(QSize(200, 200))</code><code> self.image.setMovie(self.movie)</code><code> self.movie.start()</code><code> else:</code><code> self.movie = QMovie("./click/click.gif")</code><code> self.movie.setScaledSize(QSize(200, 200))</code><code> self.image.setMovie(self.movie)</code><code> self.movie.start()</code><code> self.condition = 0</code><code> self.talk_condition = 0</code>Dialogue display
<code>def talk(self):</code><code> if not self.talk_condition:</code><code> self.talkLabel.setText(random.choice(self.dialog))</code><code> self.talkLabel.setStyleSheet("font: bold; font:25pt '楷体'; color:white; background-color: white")</code><code> self.talkLabel.adjustSize()</code><code> else:</code><code> self.talkLabel.setText("别点我")</code><code> self.talkLabel.setStyleSheet("font: bold; font:25pt '楷体'; color:white; background-color: white")</code><code> self.talkLabel.adjustSize()</code><code> self.talk_condition = 0</code>The article also contains promotional material for a free Python public course and links to related Python tutorials, but the core technical content provides a complete, runnable example of a desktop pet application.
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.