🐱 桌面跑动的小猫

📋 功能说明
  • 显示桌面宠物
  • 移动时CPU占用显示
  • 可爱趣味效果
💻 源代码
import tkinter as tk
import psutil
import random
import time

class DesktopCat:
    def __init__(self):
        self.window = tk.Tk()
        self.window.attributes('-topmost', True)
        self.window.overrideredirect(True)
        self.window.configure(bg='systemTransparent')
        
        self.label = tk.Label(self.window, text='🐱', font=('', 32))
        self.label.pack()
        
        self.x = 500
        self.y = 300
        self.dx = 5
        self.dy = 3
        
        self.move()
        self.window.mainloop()
        
    def get_cpu_usage(self):
        """获取CPU使用率"""
        return psutil.cpu_percent(interval=0.1)
    
    def move(self):
        """移动"""
        cpu = self.get_cpu_usage()
        
        # 根据CPU调整速度
        self.dx = 3 + cpu / 20
        self.dy = 2 + cpu / 30
        
        self.x += self.dx
        self.y += self.dy
        
        # 边界检测
        if self.x < 0 or self.x > 1500:
            self.dx = -self.dx
        if self.y < 0 or self.y > 900:
            self.dy = -self.dy
            
        # 随机改变方向
        if random.random() < 0.1:
            self.dx = -self.dx
        if random.random() < 0.1:
            self.dy = -self.dy
            
        self.window.geometry(f'+{int(self.x)}+{int(self.y)}')
        self.window.after(50, self.move)

if __name__ == '__main__':
    DesktopCat()
📦 运行环境
pip install psutil
功能特点
  • ✅ 始终置顶
  • ✅ CPU关联移动
  • ✅ 无边框窗口