代码概述
这是一个基于Tkinter的GUI应用程序,用于实时监控计算机硬件状态,包括CPU、GPU、内存、硬盘等组件的信息。
主要功能模块
1. 系统信息获取
系统基本信息:操作系统版本、计算机名、制造商、型号
主板信息:制造商和产品型号
使用WMI库通过Windows管理接口获取硬件信息
2. CPU监控
基本信息:品牌、核心数(物理/逻辑)、频率
温度监控:通过多种方法尝试获取
优先使用OpenHardwareMonitor
备用WMI方法
风扇转速:同样支持多种获取方式
3. GPU监控
基本信息:GPU型号、显存、驱动版本
温度监控:通过GPUtil库获取
风扇转速:需要专用驱动支持
4. 其他硬件监控
内存使用:总内存、已使用、使用百分比
硬盘使用:总空间、已使用空间、使用百分比
网络统计:上传和下载数据量
技术特点
依赖库
python
import tkinter as tk # GUI界面 import psutil # 系统监控 import GPUtil # GPU信息 import cpuinfo # CPU详细信息 import wmi # Windows硬件信息
错误处理机制
所有硬件信息获取都有try-catch保护
温度监控提供多种备选方案
友好的错误提示信息
实时更新
使用实现每2秒自动更新
root.after(2000, update_info)
动态显示硬件状态变化
界面设计
采用分层布局:
系统基本信息
CPU相关信息(含温度、风扇)
GPU相关信息
内存、硬盘、网络信息
安装提示按钮
使用要求
必需条件
Windows操作系统(依赖WMI)
安装必要的Python库
建议安装OpenHardwareMonitor以获取准确的温度和风扇数据
安装命令
bash
pip install psutil GPUtil py-cpuinfo pywin32
改进建议
可移植性:目前严重依赖Windows,可考虑跨平台方案
性能优化:减少不必要的重复查询
用户体验:添加数据可视化图表
异常处理:增加更多硬件兼容性检查
这个程序是一个功能完整的硬件监控工具,特别适合需要实时了解计算机运行状态的用户使用。
import tkinter as tk
from tkinter import messagebox
import psutil
import GPUtil
import platform
import cpuinfo
import os
import time
import subprocess
import webbrowser
try:
import wmi
except ImportError:
os.system('pip install pywin32')
import wmi
def get_system_info():
“””获取系统基本信息”””
try:
computer = wmi.WMI()
system_info = computer.Win32_ComputerSystem()[0]
os_info = computer.Win32_OperatingSystem()[0]
info = f”系统: {platform.system()} {platform.version()}
“
info += f”计算机名: {platform.node()}
“
info += f”制造商: {system_info.Manufacturer}
“
info += f”型号: {system_info.Model}”
return info
except Exception as e:
return f”系统信息: 无法获取 ({e})”
def get_motherboard_info():
“””获取主板信息”””
try:
computer = wmi.WMI()
motherboard = computer.Win32_BaseBoard()[0]
return f”主板: {motherboard.Manufacturer} {motherboard.Product}”
except Exception as e:
return f”主板: 无法获取 ({e})”
def get_cpu_info():
“””获取CPU详细信息”””
try:
info = cpuinfo.get_cpu_info()
cpu_freq = psutil.cpu_freq()
cpu_details = f”CPU: {info['brand_raw']}
“
cpu_details += f”核心数: {psutil.cpu_count(logical=False)} 物理 / {psutil.cpu_count(logical=True)} 逻辑
“
if cpu_freq:
cpu_details += f”频率: {cpu_freq.current:.2f} MHz (最大: {cpu_freq.max:.2f} MHz)”
return cpu_details
except Exception as e:
return f”CPU信息: 无法获取 ({e})”
def get_cpu_temperature():
“””获取CPU温度 – 多种方法尝试”””
try:
# 方法1: 尝试OpenHardwareMonitor
w = wmi.WMI(namespace=”root\OpenHardwareMonitor”)
temperature_infos = w.Sensor()
for sensor in temperature_infos:
if sensor.SensorType == 'Temperature' and “core” in sensor.Name.lower():
return f”CPU 温度: {sensor.Value:.1f} °C”
except:
pass
try:
# 方法2: 尝试其他WMI温度传感器
w = wmi.WMI(namespace=”root\WMI”)
temperature_infos = w.MSAcpi_ThermalZoneTemperature()
if temperature_infos:
temp = (temperature_infos[0].CurrentTemperature – 2732) / 10.0
return f”CPU 温度: {temp:.1f} °C”
except:
pass
return “CPU 温度: 需要安装OpenHardwareMonitor”
def get_cpu_fan_speed():
“””获取CPU风扇转速 – 多种方法尝试”””
try:
# 方法1: OpenHardwareMonitor
w = wmi.WMI(namespace=”root\OpenHardwareMonitor”)
fan_infos = w.Sensor()
for sensor in fan_infos:
if sensor.SensorType == 'Fan':
return f”CPU 风扇转速: {sensor.Value:.0f} RPM”
except:
pass
try:
# 方法2: 标准WMI
w = wmi.WMI()
fan_infos = w.Win32_Fan()
if fan_infos:
return f”CPU 风扇转速: {fan_infos[0].DesiredSpeed if hasattr(fan_infos[0], 'DesiredSpeed') else 'N/A'} RPM”
except:
pass
return “CPU 风扇转速: 需要安装OpenHardwareMonitor”
def get_gpu_info():
“””获取GPU详细信息”””
try:
gpus = GPUtil.getGPUs()
if gpus:
gpu_info = “GPU信息:
“
for i, gpu in enumerate(gpus):
gpu_info += f” GPU{i+1}: {gpu.name}
“
gpu_info += f” 显存: {gpu.memoryTotal} MB
“
gpu_info += f” 驱动: {gpu.driver}”
return gpu_info
return “GPU: 未检测到”
except Exception as e:
return f”GPU信息: 无法获取 ({e})”
def get_gpu_temperature():
“””获取GPU温度”””
try:
gpus = GPUtil.getGPUs()
if gpus:
gpu = gpus[0]
return f”GPU 温度: {gpu.temperature} °C”
return “GPU 温度: 无法获取”
except Exception as e:
return f”GPU 温度: 无法获取 ({e})”
def get_gpu_fan_speed():
“””获取GPU风扇转速”””
return “GPU 风扇转速: 需要专用驱动支持”
def get_memory_info():
“””获取内存详细信息”””
try:
memory = psutil.virtual_memory()
memory_total_gb = memory.total / (1024**3)
memory_used_gb = memory.used / (1024**3)
memory_percent = memory.percent
return f”内存: {memory_used_gb:.1f} GB / {memory_total_gb:.1f} GB ({memory_percent}%)”
except Exception as e:
return f”内存信息: 无法获取 ({e})”
def get_disk_info():
“””获取硬盘详细信息”””
try:
disk = psutil.disk_usage('/')
disk_total_gb = disk.total / (1024**3)
disk_used_gb = disk.used / (1024**3)
disk_percent = disk.percent
return f”硬盘: {disk_used_gb:.1f} GB / {disk_total_gb:.1f} GB ({disk_percent}%)”
except Exception as e:
return f”硬盘信息: 无法获取 ({e})”
def get_network_info():
“””获取网络信息”””
try:
net = psutil.net_io_counters()
upload_mb = net.bytes_sent / 1024 / 1024
download_mb = net.bytes_recv / 1024 / 1024
return f”网络 – 上传: {upload_mb:.2f} MB, 下载: {download_mb:.2f} MB”
except Exception as e:
return f”网络信息: 无法获取 ({e})”
def install_openhardwaremonitor():
“””提示用户安装OpenHardwareMonitor”””
try:
webbrowser.open(“https://openhardwaremonitor.org/downloads/”)
return “已尝试打开下载页面,请安装OpenHardwareMonitor后重启本程序。
安装后请以管理员权限运行OpenHardwareMonitor。”
except Exception as e:
return f”无法打开浏览器,请手动访问: https://openhardwaremonitor.org/downloads/”
def show_install_info():
“””显示安装信息”””
info = install_openhardwaremonitor()
messagebox.showinfo(“安装OpenHardwareMonitor”, info)
def update_info():
“””更新信息显示”””
system_label.config(text=get_system_info())
motherboard_label.config(text=get_motherboard_info())
cpu_label.config(text=get_cpu_info())
cpu_temp_label.config(text=get_cpu_temperature())
cpu_fan_label.config(text=get_cpu_fan_speed())
gpu_label.config(text=get_gpu_info())
gpu_temp_label.config(text=get_gpu_temperature())
gpu_fan_label.config(text=get_gpu_fan_speed())
memory_label.config(text=get_memory_info())
disk_label.config(text=get_disk_info())
network_label.config(text=get_network_info())
root.after(2000, update_info) # 每隔2秒更新一次
# 创建主窗口
root = tk.Tk()
root.title(“硬件状态监控 – 完整版”)
root.geometry(“600×600”)
# 创建框架容器
main_frame = tk.Frame(root)
main_frame.pack(padx=10, pady=10, fill=tk.BOTH, expand=True)
# 系统信息
system_label = tk.Label(main_frame, text=””, justify=tk.LEFT, font=(“Arial”, 10))
system_label.pack(anchor=”w”, pady=2)
# 主板信息
motherboard_label = tk.Label(main_frame, text=””, justify=tk.LEFT, font=(“Arial”, 10))
motherboard_label.pack(anchor=”w”, pady=2)
# 分隔线
separator1 = tk.Frame(main_frame, height=2, bd=1, relief=tk.SUNKEN)
separator1.pack(fill=tk.X, pady=10)
# CPU信息
cpu_label = tk.Label(main_frame, text=””, justify=tk.LEFT, font=(“Arial”, 10))
cpu_label.pack(anchor=”w”, pady=2)
cpu_temp_label = tk.Label(main_frame, text=””, justify=tk.LEFT, font=(“Arial”, 10))
cpu_temp_label.pack(anchor=”w”, pady=2)
cpu_fan_label = tk.Label(main_frame, text=””, justify=tk.LEFT, font=(“Arial”, 10))
cpu_fan_label.pack(anchor=”w”, pady=2)
# 分隔线
separator2 = tk.Frame(main_frame, height=2, bd=1, relief=tk.SUNKEN)
separator2.pack(fill=tk.X, pady=10)
# GPU信息
gpu_label = tk.Label(main_frame, text=””, justify=tk.LEFT, font=(“Arial”, 10))
gpu_label.pack(anchor=”w”, pady=2)
gpu_temp_label = tk.Label(main_frame, text=””, justify=tk.LEFT, font=(“Arial”, 10))
gpu_temp_label.pack(anchor=”w”, pady=2)
gpu_fan_label = tk.Label(main_frame, text=””, justify=tk.LEFT, font=(“Arial”, 10))
gpu_fan_label.pack(anchor=”w”, pady=2)
# 分隔线
separator3 = tk.Frame(main_frame, height=2, bd=1, relief=tk.SUNKEN)
separator3.pack(fill=tk.X, pady=10)
# 内存使用
memory_label = tk.Label(main_frame, text=””, justify=tk.LEFT, font=(“Arial”, 10))
memory_label.pack(anchor=”w”, pady=2)
# 硬盘使用
disk_label = tk.Label(main_frame, text=””, justify=tk.LEFT, font=(“Arial”, 10))
disk_label.pack(anchor=”w”, pady=2)
# 网络情况
network_label = tk.Label(main_frame, text=””, justify=tk.LEFT, font=(“Arial”, 10))
network_label.pack(anchor=”w”, pady=2)
# 安装提示按钮
install_button = tk.Button(main_frame, text=”安装OpenHardwareMonitor(用于温度和风扇监测)”,
command=show_install_info)
install_button.pack(pady=10)
# 更新信息
update_info()
# 启动主循环
root.mainloop()



