windows平台下不使用WMI获取CPU温度代码
在Windows平台上,如果不想使用WMI来获取CPU温度,可以使用第三方库来实现。以下是一个使用Open Hardware Monitor库来获取CPU温度的示例代码:
import clr
clr.AddReference('OpenHardwareMonitorLib')
from OpenHardwareMonitor.Hardware import Computer
def get_cpu_temperature():
computer = Computer()
computer.CPUEnabled = True
computer.Open()
for hardware in computer.Hardware:
if hardware.HardwareType == "CPU":
hardware.Update()
for sensor in hardware.Sensors:
if sensor.SensorType == "Temperature" and sensor.Name == "CPU Package":
return sensor.Value
return None
cpu_temperature = get_cpu_temperature()
if cpu_temperature is not None:
print("CPU温度: {}°C".format(cpu_temperature))
else:
print("无法获取CPU温度")
在运行此代码之前,需要确保已安装Open Hardware Monitor并将其添加到环境变量中。此外,还需要在Python环境中安装pythonnet库,以便能够在Python中使用.NET库。可以使用以下命令安装pythonnet库:
pip install pythonnet
请注意,由于硬件和操作系统的差异,此代码可能在某些系统上无法正常工作
原文地址: https://www.cveoy.top/t/topic/hFjc 著作权归作者所有。请勿转载和采集!