基于Tkinter的界面,通过psutil获取电脑的CPU主频、CPU使用率、网络上传速度、网络下载速度、网络流量、内存使用量、内存总量、内存使用率、交换内存使用量、交换内存总量、交换内存使用率,并绘制CPU使用率、网速、内存使用率、交换内存使用率的波形,点击粉红色按钮切换波形显示。其中网速的波形有100Kb、1Mb、10Mb、50Mb四个挡位。
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 7 13:49:14 2019
@author: Belth Pixtink
Project: PC监视器
Version: 1.0.0
"""
import psutil
from time import time
from time import sleep
from tkinter import Label
from tkinter import Tk
from tkinter import Button
from tkinter import Canvas
from threading import Thread
def GUI_Init():
global T1,T2,T3,T4,T5
global Map,l7
l1 = Label(root, text='开机时长:',fg = "lightgreen",bg = "black")
l1.pack()
l1.place(x=10,y=10,width=80,height=30)
T1 = Label(root, text=0,fg = "yellow",bg = "black")
T1.pack()
T1.place(x=90,y=10,width=300,height=30)
l2 = Label(root, text='CPU使用率:',fg = "lightgreen",bg = "black")
l2.pack()
l2.place(x=10,y=70,width=80,height=30)
T2 = Label(root, text=0,fg = "yellow",bg = "black")
T2.pack()
T2.place(x=90,y=70,width=300,height=30)
l3 = Label(root, text='网速流量:',fg = "lightgreen",bg = "black")
l3.pack()
l3.place(x=10,y=130,width=80,height=30)
T3 = Label(root, text=0,fg = "yellow",bg = "black")
T3.pack()
T3.place(x=90,y=130,width=300,height=30)
l4 = Label(root, text='内存占用:',fg = "lightgreen",bg = "black")
l4.pack()
l4.place(x=10,y=190,width=80,height=30)
T4 = Label(root, text=0,fg = "yellow",bg = "black")
T4.pack()
T4.place(x=90,y=190,width=300,height=30)
l5 = Label(root, text='交换内存:',fg = "lightgreen",bg = "black")
l5.pack()
l5.place(x=10,y=250,width=80,height=30)
T5 = Label(root, text=0,fg = "yellow",bg = "black")
T5.pack()
T5.place(x=90,y=250,width=300,height=30)
l6 = Label(root, text=u'\u25BA',fg = "white",bg = "black",font = "Helvetica 16 bold")
l6.pack()
l6.place(x=390,y=70,width=30,height=30)
Map = Canvas(root,width=300,height=265,bg = "lightgray")
Map.pack()
Map.place(x=420,y=10)
l7 = Label(root, text='',fg = "blue",bg = "lightgray")
l7.pack()
l7.place(x=680,y=12,width=36,height=16)
def Run():
var = 1
color = 'red'
global Lisdraw
Lisdraw = list()
LNS = psutil.net_io_counters().bytes_sent
LNR = psutil.net_io_counters().bytes_recv
while var == 1:
Time = int(time() - psutil.boot_time())
T1['text'] = str(int(Time/3600))+"h-"+str(int(Time/60)%60)+"min-"+str(Time%60)+"s"
CPU_Pec = psutil.cpu_percent(0)
T2['text'] = str(CPU_Pec)+"%\t"+str(round(psutil.cpu_freq().current/1000,3))+"GHz"
NetS = psutil.net_io_counters().bytes_sent
NetR= psutil.net_io_counters().bytes_recv
T3['text'] = str(round((NetS-LNS)*2/1024,2))+"Kb/s"+u'\u21E7'+" "+str(round((NetR-LNR)*2/1024,2))+"Kb/s"u'\u21E9'+" Data:"+str(round((NetS+NetR)/1024/1024,2))+"Mb"+u'\u21C5'
MemAll = round(psutil.virtual_memory().total/1024/1024,2)
MemUse = round(psutil.virtual_memory().used/1024/1024,2)
MemPec = psutil.virtual_memory().percent
T4['text'] = str(MemUse)+"Mb"+"/"+str(MemAll)+"Mb"+" "+str(MemPec)+"%"
SwapMemAll = round(psutil.swap_memory().total/1024/1024,2)
SwapMemUsed = round(psutil.swap_memory().used/1024/1024,2)
SwapMemPec = psutil.swap_memory().percent
T5['text'] = str(SwapMemUsed)+"Mb"+"/"+str(SwapMemAll)+"Mb"+" "+str(SwapMemPec)+"%"
if mode == 0:
color = 'red'
Het = int(CPU_Pec*260/100)
elif mode == 1:
Het = int((NetS-LNS)*2/1024) + int((NetR-LNR)*2/1024)
elif mode == 2:
color = 'green'
Het = int(MemPec*260/100)
elif mode == 3:
color = 'orange'
Het = int(SwapMemPec*260/100)
Lisdraw.append(Het)
if len(Lisdraw) == 61:
del Lisdraw[0]
Map.delete("all")
if mode != 1 and len(Lisdraw) > 2:
l7['text'] = ''
l7['text'] = '100%'
for i in range(0,len(Lisdraw)-1):
Map.create_line(i*5,260-Lisdraw[i],i*5+5,260-Lisdraw[i+1],fill=color,width = 2)
if mode == 1 and len(Lisdraw) > 2:
l7['text'] = ''
if max(Lisdraw) < 100:
l7['text'] = '100Kb'
for i in range(0,len(Lisdraw)-1):
Map.create_line(i*5,260-Lisdraw[i]*260/100,i*5+5,260-Lisdraw[i+1]*260/100,fill='skyblue',width = 2)
elif max(Lisdraw) < 1000:
l7['text'] = '1Mb'
for i in range(0,len(Lisdraw)-1):
Map.create_line(i*5,260-Lisdraw[i]*260/1000,i*5+5,260-Lisdraw[i+1]*260/1000,fill='blue',width = 2)
elif max(Lisdraw) < 10000:
l7['text'] = '10Mb'
for i in range(0,len(Lisdraw)-1):
Map.create_line(i*5,260-Lisdraw[i]*260/10000,i*5+5,260-Lisdraw[i+1]*260/10000,fill='purple',width = 2)
elif max(Lisdraw) < 100000:
l7['text'] = '50Mb'
for i in range(0,len(Lisdraw)-1):
Map.create_line(i*5,260-Lisdraw[i]*260/50000,i*5+5,260-Lisdraw[i+1]*260/50000,fill='red',width = 2)
sleep(0.5)
LNS = NetS
LNR = NetR
root.protocol('WM_DELETE_WINDOW',End)
def Mode():
global mode
global Lisdraw
l6 = Label(root, text="",bg = "black")
l6.place(x=390,y=10+(mode+1)*60,width=30,height=30)
l7['text'] = ''
mode = mode+1
if mode == 4:
mode = 0
if mode == 0:
l6 = Label(root, text=u'\u25BA',fg = "white",bg = "black",font = "Helvetica 16 bold italic")
l6.place(x=390,y=70,width=30,height=30)
elif mode == 1:
l6 = Label(root, text=u'\u25BA',fg = "white",bg = "black",font = "Helvetica 16 bold italic")
l6.place(x=390,y=130,width=30,height=30)
elif mode == 2:
l6 = Label(root, text=u'\u25BA',fg = "white",bg = "black",font = "Helvetica 16 bold italic")
l6.place(x=390,y=190,width=30,height=30)
elif mode == 3:
l6 = Label(root, text=u'\u25BA',fg = "white",bg = "black",font = "Helvetica 16 bold italic")
l6.place(x=390,y=250,width=30,height=30)
Lisdraw = list()
Map.delete("all")
def End():
root.destroy()
if __name__ == '__main__':
mode = 0
root = Tk()
root.title("PC监视器")
root.geometry('750x300')
root.configure(background = 'black')
GUI_Init()
TIM = Thread(target = Run)
TIM.daemon = True
TIM.start()
Button1 = Button(root, text=u'\u25B2'+u'\u25BC', command = Mode,bg = "pink")
Button1.pack()
Button1.place(x=390,y=10,width=30)
root.mainloop()
运行界面(测试环境—python3.7):
- CPU使用率的波形
- 网速波形
- 内存使用率的波形
- 交换内存使用率的波形
== ---------------------------------------------------------华丽的分割线---------------------------------------------------------==