init: TrayMon v1.0.0 首次发布

Windows 托盘 CPU / GPU 实时监控小工具
- 托盘图标直显 C/G 占用率,悬停显示一位小数
- 右键一键切换开机自启(当前用户 Run 键,免管理员)
- NVIDIA GPU 走 nvidia-smi,非 N 卡显示 0%
- 单文件免安装 exe(PyInstaller onefile windowed)
MIT License
This commit is contained in:
Zhang Pu 2026-11-02 11:47:20 +08:00
commit f479f4edf2
7 changed files with 461 additions and 0 deletions

25
.gitignore vendored Normal file
View file

@ -0,0 +1,25 @@
# Python
__pycache__/
*.py[cod]
*$py.class
*.egg-info/
.venv/
venv/
# PyInstaller(build/ 目录只保留打包脚本 build.ps1)
build/*
!build/build.ps1
dist/
*.spec
# 打包产物
release/
# 编辑器 / 系统
.vscode/
.idea/
.DS_Store
Thumbs.db
# 本地缓存
*.log

21
LICENSE Normal file
View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2026 zhangpu
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

85
README.md Normal file
View file

@ -0,0 +1,85 @@
# TrayMon
> 挂在 Windows 托盘的 CPU / GPU 实时监控小工具。
常驻右下角托盘,每秒刷新 CPU 和 GPU 占用率,不占任务栏、不弹窗口、零打扰。
![version](https://img.shields.io/badge/version-v1.0-blue)
![platform](https://img.shields.io/badge/platform-Windows%2010%2F11%2064--bit-lightgrey)
![license](https://img.shields.io/badge/license-MIT-green)
## 截图
_(TODO: 托盘图标 + 右键菜单 + 关于窗口)_
## 特性
- **托盘直显**:图标上直接印 `C:xx% / G:xx%`,悬停显示一位小数。
- **右键自启开关**:一键切换开机自启,状态自动记住,无需手动改注册表或 Startup 文件夹。
- **NVIDIA GPU 占用**:通过 `nvidia-smi` 读取显卡使用率(非 N 卡或无独显时显示 0%)。
- **单文件免安装**:PyInstaller 打包成一个 exe,无控制台黑窗,双击即跑。
- **关于面板**:内置作者信息与主页链接。
## 下载
到 [Releases](https://github.com/zhang-pu/traymon/releases) 下载最新版 `TrayMon.exe`(约 30 MB),双击即可运行。
> 仅支持 Windows 10 / 11 64-bit。
## 从源码运行
需要 Python 3.10+:
```powershell
git clone https://github.com/zhang-pu/traymon.git
cd traymon
python -m pip install -r requirements.txt
python system_monitor.py
```
## 从源码打包成单文件 exe
```powershell
./build/build.ps1
```
产物会输出到 `release/TrayMon.exe`。
或者手动:
```powershell
python -m pip install pyinstaller
python -m PyInstaller --onefile --windowed --name TrayMon `
--clean --noconfirm system_monitor.py
```
## 目录结构
```
traymon/
├── system_monitor.py # 主程序(单文件)
├── requirements.txt # Python 依赖
├── build/
│ └── build.ps1 # 一键打包脚本
├── release/ # 打包产物(git 忽略)
├── screenshots/ # 截图资源
├── README.md
└── LICENSE
```
## 技术栈
- Python 3
- [psutil](https://github.com/giampaolo/psutil) — CPU 占用
- [GPUtil](https://github.com/anderskm/gputil) — GPU 占用
- [pystray](https://github.com/moses-palmer/pystray) — 系统托盘
- [Pillow](https://python-pillow.org/) — 图标绘制
- [PyInstaller](https://www.pyinstaller.org/) — 打包 exe
## 作者
**zhangpu** — <https://zhangpu.dev>
## License
[MIT](./LICENSE)

34
build/build.ps1 Normal file
View file

@ -0,0 +1,34 @@
# TrayMon 一键打包脚本(Windows 上执行)
# 用法:PowerShell 里执行 .\build\build.ps1
# 产物:..\release\TrayMon.exe(单文件,无控制台窗口)
$ErrorActionPreference = "Stop"
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$root = Split-Path -Parent $here
Set-Location $root
Write-Host "== 检查 Python ==" -ForegroundColor Cyan
python --version
Write-Host "== 安装依赖 ==" -ForegroundColor Cyan
python -m pip install --upgrade pip
python -m pip install -r requirements.txt
python -m pip install pyinstaller
Write-Host "== 清理旧的打包目录 ==" -ForegroundColor Cyan
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue build\TrayMon
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue dist
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue release
Write-Host "== 打包(单文件 / 无控制台)==" -ForegroundColor Cyan
python -m PyInstaller --onefile --windowed --name TrayMon `
--clean --noconfirm system_monitor.py
Write-Host "== 收集产物 ==" -ForegroundColor Cyan
New-Item -ItemType Directory -Force -Path release | Out-Null
Copy-Item -Force dist\TrayMon.exe release\TrayMon.exe
$size = [math]::Round((Get-Item release\TrayMon.exe).Length / 1MB, 1)
Write-Host ""
Write-Host "完成:$root\release\TrayMon.exe ($size MB)" -ForegroundColor Green
Write-Host "(release/ 已在 .gitignore 中,不会进 Git 仓库)"

4
requirements.txt Normal file
View file

@ -0,0 +1,4 @@
psutil>=5.9.0
GPUtil>=1.4.0
pystray>=0.19.5
Pillow>=10.0.0

0
screenshots/.gitkeep Normal file
View file

292
system_monitor.py Normal file
View file

@ -0,0 +1,292 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
TrayMon · Windows 托盘 CPU / GPU 实时监控
常驻右下角托盘,每秒刷新 CPU 和 GPU 占用率:
- 托盘图标上直接印 `C:xx% / G:xx%`
- 悬停显示一位小数
- 右键菜单:开机自启开关 / 关于面板 / 退出
依赖:psutil(CPU)、GPUtil(NVIDIA GPU)、pystray(托盘)、Pillow(图标绘制)
打包:python -m PyInstaller --onefile --windowed --name TrayMon system_monitor.py
"""
import os
import sys
import platform
import subprocess
import webbrowser
from PIL import Image, ImageDraw, ImageFont
try:
import psutil
except ImportError: # pragma: no cover
psutil = None
try:
import pystray
from pystray import Icon, Menu, MenuItem
except ImportError: # pragma: no cover
pystray = None
Icon = Menu = MenuItem = None
try:
import GPUtil
except ImportError:
GPUtil = None
APP_NAME = "TrayMon"
APP_VERSION = "1.0"
AUTHOR = "zhangpu"
HOME_URL = "https://zhangpu.dev"
REPO_URL = "https://github.com/zhang-pu/traymon"
INTERVAL = 1.0 # 刷新间隔(秒)
ICON_SIZE = 64 # 绘制图标分辨率
# Windows 开机自启:注册表 Run 键
RUN_KEY = r"Software\Microsoft\Windows\CurrentVersion\Run"
# --------------------------------------------------------------------------- #
# 采集
# --------------------------------------------------------------------------- #
def cpu_percent():
"""CPU 占用率;首次调用返回 0(psutil 首调用是基线)。"""
if psutil is None:
return 0.0
try:
return float(psutil.cpu_percent(interval=None))
except Exception:
return 0.0
def gpu_percent():
"""GPU 占用率(NVIDIA,走 nvidia-smi);非 N 卡 / 无独显时返回 0。"""
if GPUtil is None:
return 0.0
try:
gpus = GPUtil.getGPUs()
if not gpus:
return 0.0
return float(max(g.load for g in gpus))
except Exception:
return 0.0
# --------------------------------------------------------------------------- #
# 自启
# --------------------------------------------------------------------------- #
def _is_windows():
return platform.system().lower().startswith("win")
def autostart_enabled():
if not _is_windows():
return False
try:
import winreg
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, RUN_KEY, 0, winreg.KEY_READ) as k:
value, _ = winreg.QueryValueEx(k, APP_NAME)
return bool(value)
except Exception:
return False
def set_autostart(enable):
"""开关开机自启(写当前用户 Run 键,免管理员权限)。"""
if not _is_windows():
return False
try:
import winreg
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, RUN_KEY, 0, winreg.KEY_SET_VALUE) as k:
if enable:
exe = os.path.abspath(sys.argv[0] if getattr(sys, "frozen", False) else sys.executable)
# 源码运行时用 pythonw 静默启动打包后的 exe
cmd = f'"{exe}"' if getattr(sys, "frozen", False) else f'"{exe}" "{os.path.abspath(__file__)}"'
winreg.SetValueEx(k, APP_NAME, 0, winreg.REG_SZ, cmd)
else:
try:
winreg.DeleteValue(k, APP_NAME)
except FileNotFoundError:
pass
return True
except Exception:
return False
# --------------------------------------------------------------------------- #
# 图标绘制
# --------------------------------------------------------------------------- #
def _load_font(size):
"""尽量取一个能显示 ASCII 的字体;取不到就用默认位图字体。"""
candidates = [
os.path.join(os.environ.get("WINDIR", "C:\\Windows"), "Fonts", "segoeui.ttf"),
os.path.join(os.environ.get("WINDIR", "C:\\Windows"), "Fonts", "arial.ttf"),
"/usr/share/fonts/truetype/dejavu/DejaVuSansMono-Bold.ttf",
"/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
]
for path in candidates:
try:
if os.path.exists(path):
return ImageFont.truetype(path, size)
except Exception:
continue
try:
return ImageFont.load_default()
except Exception:
return None
def make_icon(cpu, gpu):
"""画出托盘图标:C:xx% / G:xx% 两行文字。"""
img = Image.new("RGBA", (ICON_SIZE, ICON_SIZE), (0, 0, 0, 0))
d = ImageDraw.Draw(img)
# 背景:深色圆角块,保证在亮/暗任务栏上都可读
d.rounded_rectangle((2, 2, ICON_SIZE - 3, ICON_SIZE - 3), radius=12,
fill=(28, 32, 38, 235))
font = _load_font(15)
small = _load_font(13)
# CPU 行
d.text((8, 6), "C:%d%%" % int(round(cpu)), font=font, fill=(120, 220, 140, 255))
# GPU 行
d.text((8, 30), "G:%d%%" % int(round(gpu)), font=small, fill=(120, 180, 240, 255))
# 细边框
d.rounded_rectangle((2, 2, ICON_SIZE - 3, ICON_SIZE - 3), radius=12,
outline=(90, 100, 110, 255), width=1)
return img
# --------------------------------------------------------------------------- #
# 主程序
# --------------------------------------------------------------------------- #
class TrayMon:
def __init__(self):
self.cpu = 0.0
self.gpu = 0.0
self.icon = None
self._first = True
# ---- 状态 ----
def update_usage(self):
self.cpu = cpu_percent()
self.gpu = gpu_percent()
self._first = False
def title(self):
return "%s · CPU %.1f%% · GPU %.1f%%" % (APP_NAME, self.cpu, self.gpu)
def refresh(self):
self.update_usage()
if self.icon is not None:
self.icon.icon = make_icon(self.cpu, self.gpu)
self.icon.title = self.title()
# ---- 菜单动作 ----
def toggle_autostart(self, icon, item):
enable = not autostart_enabled()
ok = set_autostart(enable)
state = "已开启" if enable else "已关闭"
if not ok:
state = "操作失败"
if self.icon is not None:
self.icon.title = "%s · 开机自启 %s" % (self.title(), state)
self.icon.menu = self.build_menu()
def about(self, icon, item):
lines = [
"%s v%s" % (APP_NAME, APP_VERSION),
"作者:%s" % AUTHOR,
"主页:%s" % HOME_URL,
"",
"托盘显示 CPU / GPU 实时占用率。",
]
try:
_message_box("关于 %s" % APP_NAME, "\n".join(lines))
except Exception:
pass
def open_home(self, icon, item):
webbrowser.open(HOME_URL)
def quit(self, icon, item):
if icon is not None:
icon.stop()
# ---- 菜单 ----
def build_menu(self):
if Menu is None:
return None
autostart_on = autostart_enabled()
return Menu(
MenuItem("CPU %.1f%% GPU %.1f%%" % (self.cpu, self.gpu), None, enabled=False),
Menu.SEPARATOR,
MenuItem(
"开机自启(已开启)" if autostart_on else "开机自启(已关闭)",
self.toggle_autostart,
checked=lambda item: autostart_enabled(),
),
MenuItem("关于", self.about),
MenuItem("打开主页", self.open_home),
Menu.SEPARATOR,
MenuItem("退出", self.quit),
)
def _message_box(title, text):
"""尽量用系统原生对话框;没有就退化到控制台输出。"""
try:
if _is_windows():
ctypes_windll = __import__("ctypes").windll
ctypes_windll.user32.MessageBoxW(None, text, title, 0x40)
return
except Exception:
pass
try:
import tkinter
from tkinter import messagebox
root = tkinter.Tk()
root.withdraw()
messagebox.showinfo(title, text)
root.destroy()
return
except Exception:
pass
print("%s\n%s" % (title, text))
def main():
if pystray is None:
print("缺少依赖 pystray,请先 pip install pystray psutil GPUtil Pillow")
return 1
app = TrayMon()
# 预热一次 psutil 基线,避免首帧 0%
cpu_percent()
app.update_usage()
icon = Icon(
"traymon",
make_icon(app.cpu, app.gpu),
app.title(),
menu=app.build_menu(),
)
app.icon = icon
def _tick():
app.refresh()
return True
icon.run(_tick)
return 0
if __name__ == "__main__":
sys.exit(main())