如何使用Python对基于Linux的操作系统的CPU温度进行连续阅读



我正在尝试为Raspberry Pi制作程序,但是我在Mac和Raspberry Pi之间不断切换。有没有一种方法可以使用Python访问Linux的CPU温度。我可以使其与tkinter一起使用吗?(tkinter无法正确使用我的知识中的while循环

# How can I get a continuous CPU temperature reading for Linux (Mac//Raspberry Pi) using a 
def function():
    #stuff
    tkinterFrame.after(delay, function)
# im using tkinter so avoiding while loops
# need to use function .after()```
I expect a continuous output that refreshes every second.

Linux在阅读文件/sys/class/thermal/thermal_zone0/temp时为您提供CPU的当前温度。您将获得具有温度的单行文本,作为 integer 。因此,您必须将结果除以1000,以在°C中获得温度。请看一下这个简单的示例,该示例读取当前时间和在终端中打印的温度。

import time
import datetime
while(True):
    CurrentTime = datetime.datetime.now()
    with open(r"/sys/class/thermal/thermal_zone0/temp") as File:
        CurrentTemp = File.readline()
    print(str(CurrentTime) + " - " + str(float(CurrentTemp) / 1000))
    time.sleep(1)

您现在要做的就是存储结果并打印它们(也许是用图?(。您可以使用额外的线程来执行此操作,因此当您使用某种延迟时,您的应用程序不会卡住(因为您不需要每毫秒读取温度 - 我认为每秒就足够了(。

最新更新