1. 程式人生 > >讀取本機cpu等資料資訊

讀取本機cpu等資料資訊

#_*_ conding:utf-8 _*_
import psutil
import time,datetime
from tkinter import *
import threading

def get_cpu_info():#讀取CPU資訊
    cpu_info = {'user': 0, "system": 0, "idle": 0, 'percent': 0}
    cpu_times = psutil.cpu_times()
    print(cpu_times)
    cpu_info['user'] =cpu_times.user
    cpu_info['system'] = cpu_times.system
    cpu_info['idle'] = cpu_times.idle
    cpu_info['percent'] = psutil.cpu_percent(interval=2)
    cpu_status = cpu_info['percent']
    print("cpu usage is %s%%" % cpu_status)
    return cpu_status
def get_memory_info():#讀取記憶體資訊
    memory_total = []
    memory_available = []
    memory_percent = []
    memory_used = []
    memory_free = []
    mem_info = psutil.virtual_memory()
    memory_total.append( mem_info.total)
    memory_available.append(mem_info.available)
    memory_percent.append(mem_info.percent)
    memory_used.append(mem_info.used)
    memory_free.append(mem_info.free)
    mem_status = memory_percent[0]
    print('memory usage in :%s%%' % mem_status)
    return mem_status
def get_disk_info():#讀取磁碟資訊
    disk_id = []
    disk_total = []
    disk_used = []
    disk_free = []
    disk_percent = []
    for id in psutil.disk_partitions():
        if "cdrom" in id.opts or id.fstype =='':
            continue
        disk_name = id.device.split(':')
        s = disk_name[0]
        disk_id.append(s)
        disk_info = psutil.disk_usage(id.device)
        disk_total.append(disk_info.total)
        disk_used.append(disk_info.used)
        disk_free.append(disk_info.free)
        disk_percent.append(disk_info.percent)
    for i in range(len(disk_id)):
        print("%s disk usage is :%s%%" % (disk_id[i], 100 - disk_percent[i]))
    print(disk_id,disk_percent)
    return disk_id, disk_percent
def thread_info():#把讀取資訊形式在text文字框中
    global buton1
    while True:
        time.sleep(1)
        tp = datetime.datetime.now()
        print('----------本機資料---------')
        print('當前時間:', tp.strftime('%Y-%m-%d %H:%M:%S'))
        print()
        q1=get_cpu_info()
        q2=get_memory_info()
        q3=get_disk_info()
        text1.delete(1.0, END)
        text2.delete(1.0, END)
        text3.delete(1.0, END)
        text1.insert(1.0,'%s%%'%q1 )
        text2.insert(1.0,'%s%%'%q2 )
        text3.insert(1.0, '%s:%s%%\n%s:%s%%\n%s:%s%%\n%s:%s%%\n'
                     %(q3[0][0],100-q3[1][0],q3[0][1],100-q3[1][1],
                       q3[0][2],100-q3[1][2],q3[0][3],100-q3[1][3]))
        if button1==False:
            break
i=1
button1=True
def data():#按鈕開啟讀取資訊
    global i,button1
    if i==1:
        th = threading.Thread(target=thread_info)
        th.start()
        i=i+1
        button1 = True
def data1():#按鈕關閉讀取資訊
    global i,button1
    i=1
    button1=False
#監控介面
window = Tk()
window.title('監控軟體')
window.geometry('250x140')
window.resizable(0,0)
frame1= Frame(window)
button1=Button(window,text='開啟',width=10,height=1,command=data)
button1.grid(row=0,column=0)
button1=Button(window,text='關閉',width=10,height=1,command=data1)
button1.grid(row=0,column=1)
label1=Label(window,text='cpu佔用率:')
label1.grid(row=1,column=0)
label2=Label(window,text='記憶體佔用率:')
label2.grid(row=2,column=0)
label3=Label(window,text='磁碟可用率:')
label3.grid(row=3,column=0)
data1=StringVar()
text1=Text(window,width=10,height=1)
text1.place(x=75,y=35)
text2=Text(window,width=10,height=1)
text2.place(x=75,y=55)
text3=Text(window,width=10,height=4)
text3.place(x=75,y=75)
frame1.grid(row=0,column=0)
mainloop()