树莓派 Python读取信息 CPU GPU温度 显示IP

6 篇文章 0 订阅
3 篇文章 0 订阅
#!/usr/bin/python3
 
import tkinter as tk
from tkinter import *
import socket
def get_host_ip():
    """
    查询本机ip地址
    :return: ip
    """
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(('8.8.8.8', 80))
        ip = s.getsockname()[0]
    finally:
        s.close()
    return ip
import subprocess as commands#python2.0是commands包
import os
import time 
def get():
    # 查看GPU温度
    gpu = commands.getoutput( '/opt/vc/bin/vcgencmd measure_temp' ).replace( 'temp=', '' ).replace( '\'C', '' )
    gpu = float(gpu)
    print('gpu Temp: %.2f ' % gpu)

    # 查看CPU温度
    file = open("/sys/class/thermal/thermal_zone0/temp")
    cpu = float(file.read()) / 1000
    file.close()
    print('cpu Temp: %2.2f' % cpu)

    load_5min_str = str(os.popen("top -n1 | awk '/load average:/ {print $10}'").readline().strip())
    load_5min = load_5min_str.replace(',', '' )
    load_10min_str = str(os.popen("top -n1 | awk '/load average:/ {print $11}'").readline().strip())
    load_10min = load_10min_str.replace(',', '' )
    load_5min=float(load_5min[0:3])
    load_10min=float(load_10min[0:3])
    print('')
    print('load_5min: %.2f'%load_5min)
    print('load_10min: %.2f'%load_10min)
    Ramused = str(os.popen("top -n1 | awk '/MiB/ {print $6}'").readline().strip())
    print(Ramused)
    Ramused = Ramused.replace(',', '' )
    Ramused = Ramused.replace('\nfree', '' )
    Ramused = float(Ramused)

    print('')
    print('Ramused: %.2f '%Ramused +' Mb')
    def getDiskSpace():
            p = os.popen("df -h /")
            i = 0
            while 1:
                    i = i +1
                    line = p.readline()
                    if i==2:
                            return(line.split()[1:5])

    # Disk information
    DISK_stats = getDiskSpace()
    DISK_total = DISK_stats[0].replace('G','')
    DISK_used = DISK_stats[1].replace('G', '' )
    DISK_perc = DISK_stats[3].replace('%', '' )
    print('')
    print('DISK_total: '+str(DISK_total)+' Gb')
    print('DISK_used: '+str(DISK_used)+' Gb')
    print('DISK_perc: '+str(DISK_perc)+' %')
    return gpu,cpu,load_5min,load_10min,Ramused,DISK_total,DISK_used,DISK_perc

def lableset(info):
    ip = get_host_ip()
    gpu,cpu,load_5min,load_10min,Ramused,DISK_total,DISK_used,DISK_perc = get()
    time_ = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    info[0].set("IP:"+get_host_ip())
    info[1].set("CPU Temp:"+str(cpu))
    info[2].set("GPU Temp:"+str(gpu))
    info[3].set("RAM 5min used:"+str(load_5min) )
    info[4].set("RAM 10min used:"+str(load_10min) )
    info[5].set("DISK total:"+str(DISK_total) )
    info[6].set("DISK used:"+str(DISK_used) )
    info[7].set("DISK used:"+str(DISK_perc)+"%")
    info[8].set("Time:"+str(time_))
    return info
    
def refreshText():
    global info
    info = lableset(info)
    root.after(100,refreshText)


root = Tk()                     # 创建窗口对象的背景色
root.title("INFO")
root.geometry('320x320+10+10')

global info
info = []
for i in range(11):
    _v = StringVar()
    info.append(_v)
info = lableset(info)
for i in info:
    l = tk.Label(root, textvariable=i,font=("华文行楷", 20))
    l.pack()


root.after(100,refreshText)
root.mainloop()

主要代码是get() get_host_ip()
用tkinter写了个界面

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nickdlk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值