使用python自制桌面宠物,好玩!——枫原万叶桌宠,可以直接打包成exe去跟朋友炫耀。。。

大家好,我是小黄。

今天我们使用python实现一个桌面宠物。只需要gif动态图片就行。超级简单容易上手。

#完整源代码可在下方图片免费获取

一:下载相关的库文件。

复制代码
我们本次使用到的库文件为:tkinter和pyautogui

下载命令:

复制代码
pip install tkinter 

pip install pyautogui

二.我们把gif导入,放在DestTop.py同级下面即可。

DeskTop.py(不完整的)

复制代码
import tkinter as tk
import pyautogui as pt
import random
import os
from PIL import Image, ImageTk

# 获取主屏幕分辨率
WIDTH, HEIGHT = pt.size()
taskbarHeight = 40

# 将人物大小减少 40%
imgWidth, imgHeight = 670, 550

# 计算右侧10%区域的起始位置
right_margin = int(WIDTH * 0.1)
posX = WIDTH - imgWidth - right_margin  # 右侧10%区域内的初始X位置
posY = 0  # 初始Y位置

# 创建主窗口
root = tk.Tk()
root.geometry(f"{imgWidth}x{imgHeight}+{posX}+{posY}")
root.overrideredirect(1)
root.configure(bg='black')
root.attributes('-transparentcolor', 'black')
root.wm_attributes('-topmost', 1)

# 右键菜单
def create_context_menu(event):
    context_menu.post(event.x_root, event.y_root)

def exit_application():
    root.quit()

context_menu = tk.Menu(root, tearoff=0)
context_menu.add_command(label="退出", command=exit_application)

def load_images(file_path):
    images = []
    try:
        img = Image.open(file_path)
        for frame in range(img.n_frames):  # 使用 Pillow 获取帧数
            img.seek(frame)  # 定位到每一帧
            images.append(ImageTk.PhotoImage(img.copy()))  # 复制当前帧并转换为 PhotoImage
    except Exception as e:
        print(f"Error loading frames from {file_path}: {e}")
    return images

# 获取当前脚本的目录
current_dir = os.path.dirname(os.path.abspath(__file__))

# 更新 GIF 文件路径
idleRight = load_images(os.path.join(current_dir, "bar.gif"))
idleLeft = load_images(os.path.join(current_dir, "bar1.gif"))
runRight = load_images(os.path.join(current_dir, "bar1.gif"))
runLeft = load_images(os.path.join(current_dir, "bar3.gif"))
fall = load_images(os.path.join(current_dir, "bar1.gif"))

status = {
    0: fall,
    1: idleRight,
    2: idleLeft,
    3: runRight,
    4: runLeft
}
status_num = 0

canvas = tk.Canvas(root, width=imgWidth, height=imgHeight, bg='black', highlightthickness=0)
canvas.pack()

player = canvas.create_image(imgWidth // 2, imgHeight // 2, image=idleLeft[0])

# 说话文本列表
phrases = [
    "海鸥别叫啦,从今天起,我也是漂泊者啊",
    "深山踏红叶,耳畔闻鹿鸣",
    "可叹,落叶飘零",
    "我很喜欢枫叶,可惜,枫叶红时,总多离别。",
    "拾花鸟之一趣,照月风之长路",
    "闲聊?自然!"
]

def changeStatus():
    global status_num
    status_num = random.randint(1, 4)
    root.after(random.randint(1000, 5000), changeStatus)

def falling():
    global status_num, posY
    if root.winfo_y() + imgHeight < HEIGHT - taskbarHeight:
        status_num = 0
        posY += 10  # 增加每次掉落的高度
        root.geometry(f"{imgWidth}x{imgHeight}+{posX}+{posY}")
    elif root.winfo_y() + imgHeight >= HEIGHT - taskbarHeight and status_num == 0:
        status_num = 1
    root.after(10, falling)  # 保持掉落速度

运行效果:

打包为exe可执行文件:

复制代码
pyinstaller --onefile --noconsole --exclude-module numpy --exclude-module pandas --add-data "您的绝对路径\\bar.gif;." --add-data "您的绝对路径\\bar1.gif;." --add-data "您的绝对路径\\bar2.gif;." --add-data "您的绝对路径\\bar3.gif;." DestTop.py

完整源代码和gif资料包请下方获取👇↓↓↓

相关推荐
久邦科技5 分钟前
20个免费电子书下载网站,实现电子书自由(2025持续更新)
学习
m0_736919107 分钟前
C++代码风格检查工具
开发语言·c++·算法
喵手15 分钟前
Python爬虫实战:旅游数据采集实战 - 携程&去哪儿酒店机票价格监控完整方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集结果csv导出·旅游数据采集·携程/去哪儿酒店机票价格监控
2501_9449347319 分钟前
高职大数据技术专业,CDA和Python认证优先考哪个?
大数据·开发语言·python
Gain_chance20 分钟前
34-学习笔记尚硅谷数仓搭建-DWS层最近一日汇总表建表语句汇总
数据仓库·hive·笔记·学习·datagrip
helloworldandy26 分钟前
使用Pandas进行数据分析:从数据清洗到可视化
jvm·数据库·python
黎雁·泠崖1 小时前
【魔法森林冒险】5/14 Allen类(三):任务进度与状态管理
java·开发语言
Gain_chance2 小时前
36-学习笔记尚硅谷数仓搭建-DWS层数据装载脚本
大数据·数据仓库·笔记·学习
2301_763472462 小时前
C++20概念(Concepts)入门指南
开发语言·c++·算法
肖永威2 小时前
macOS环境安装/卸载python实践笔记
笔记·python·macos