相信大家對(duì)任務(wù)管理器都不是很陌生了,Ctrl+Alt+Del即可打開,然后點(diǎn)擊啟動(dòng)任務(wù)管理器,或者右擊任務(wù)欄-啟動(dòng)任務(wù)管理器即可啟動(dòng)任務(wù)管理器,啟動(dòng)之后界面如下:
可以看出它列舉出了一些重要的參數(shù),比如進(jìn)程數(shù)量,CPU使用率,物理內(nèi)存,接下來我們就來一一列舉出來。
編輯器:sublime text 3
模塊:psutil tkinter
要想實(shí)現(xiàn)任務(wù)管理器,首先我們寫一個(gè)界面出來,這里我們運(yùn)用tkinter編寫個(gè)界面出來:
我們先實(shí)現(xiàn)下它的菜單欄,這里用到tkinter的Menu模塊,不知道大家有沒有印象,那么開始吧。
m=t.Menu(root)
#文件菜單
file=t.Menu(m,tearoff=False)
m.add_cascade(label='文件', menu=file)
file.add_command(label='新建任務(wù)',accelerator='(N)')
file.add_command(label='退出任務(wù)欄管理器',command=root.quit,accelerator='(x)')
#選項(xiàng)菜單
ii=t.IntVar()
ii.set(1)
o=t.Menu(m,tearoff=False)
m.add_cascade(label='選項(xiàng)',menu=o)
o.add_radiobutton(label='前端顯示',variable=ii, value=0)
o.add_radiobutton(label='使用時(shí)最小化',variable=ii, value=1)
o.add_radiobutton(label='最小化時(shí)隱藏',variable=ii, value=2)
#查看菜單
v=t.Menu(m,tearoff=False)
m.add_cascade(label='查看',menu=v)
v.add_command(label='立即刷新')
#二級(jí)菜單
iv=t.IntVar()
iv.set(1)
s=t.Menu(v,tearoff=False)
v.add_cascade(label='更新速度',menu=s)
s.add_radiobutton(label='高',variable=iv, value=0)
s.add_radiobutton(label='普通',variable=iv, value=1)
s.add_radiobutton(label='低',variable=iv, value=2)
s.add_radiobutton(label='暫停',variable=iv, value=3)
v.add_command(label='選項(xiàng)列')
#幫助菜單
h=t.Menu(m,tearoff=False)
m.add_cascade(label='幫助',menu=h)
h.add_command(label='任務(wù)管理器幫助主體')
h.add_command(label='關(guān)于任務(wù)管理器')
root.configure(menu=m)
最后結(jié)果圖,可以看到,基本和任務(wù)管理器差不多。
界面寫完了我們?cè)撓蚪缑嫣砑咏M件了,由任務(wù)管理器那張圖我們可以看到它有一個(gè)切換任務(wù)窗口的按鈕:
b1=t.Button(root,text='應(yīng)用程序',command=yy)
b2=t.Button(root,text='進(jìn)程',command=jc)
b3=t.Button(root,text='服務(wù)',command=fw)
b4=t.Button(root,text='性能',command=xn)
b5=t.Button(root,text='聯(lián)網(wǎng)',command=lw)
b6=t.Button(root,text='用戶',command=yh)
#定位
b1.place(x=10,y=15,height=20,width=60)
b2.place(x=70,y=15,height=20,width=60)
b3.place(x=130,y=15,height=20,width=60)
b4.place(x=190,y=15,height=20,width=60)
b5.place(x=250,y=15,height=20,width=60)
b6.place(x=310,y=15,height=20,width=60)
text=t.Text(root,width=100,height=40)
text.place(x=10,y=36)
def yy():
text.delete(1.0,'end')
text.insert('insert','yy')
def jc():
text.delete(1.0,'end')
text.insert('insert','jc')
def fw():
text.delete(1.0,'end')
text.insert('insert','fw')
def xn():
text.delete(1.0,'end')
text.insert('insert','xn')
def lw():
text.delete(1.0,'end')
text.insert('insert','lw')
def yh():
text.delete(1.0,'end')
text.insert('insert','yh')
這樣就實(shí)現(xiàn)了不同按鈕之間切換不同的界面。
我們使用標(biāo)簽來放置這些參數(shù),因?yàn)檫@三項(xiàng)的參數(shù)是可變的,所以暫時(shí)只寫前面名字:
t1=t.Label(text='進(jìn)程數(shù):')
t2=t.Label(text='CPU 使用率:')
t3=t.Label(text='物理內(nèi)存:')
t1.place(x=10,y=580,width=120)
t2.place(x=150,y=580,width=120)
t3.place(x=300,y=580,width=120)
我們可以使用模塊Scrollbar來實(shí)現(xiàn),安裝滾動(dòng)條之前需要做兩件事情:
1.指定該組件的yscrollbarcommand參數(shù)為Scrollbar的set()方法
2.指定Scrollbar 的 command 參數(shù)為該組件的 yview() 方法
接下來我們實(shí)現(xiàn)它:
sb=t.Scrollbar(root)
sb.pack(side='left',fill='y')
text=t.Text(root,width=100,height=40)
text.place(x=10,y=36)
sb.config(command=text.yview) #文本框內(nèi)容隨滾動(dòng)條滾動(dòng)
text.config(yscrollcommand=sb.set(0.1,0.3)) #Y軸填充
t1=t.Label(text='')
t2=t.Label(text='')
t3=t.Label(text='')
(注:這里只是隱藏部件,萬不可用destroy銷毀部件)
現(xiàn)在我們來實(shí)現(xiàn)這三個(gè)標(biāo)簽的內(nèi)容。想必大家剛剛應(yīng)該看到了,上面的標(biāo)簽沒有設(shè)置任何內(nèi)容,那么這是為什么呢?我們都知道,一旦你把內(nèi)容添加進(jìn)去,它就會(huì)緊隨其后并不會(huì)覆蓋,所以初始值必須是空,才不至于不能覆蓋值。那么我們來看下具體實(shí)現(xiàn)過程吧。
def jcs():
t1.configure(text='進(jìn)程數(shù):'+str(len(psutil.pids())))
root.after(3000,jcs)
def cpu():
pp=str(ceil(psutil.cpu_percent(1)))
t2.configure(text='CPU 使用率:'+pp+'%')
root.after(1500,cpu)
def wlnc():
f= psutil.virtual_memory().free #剩余內(nèi)存
t=psutil.virtual_memory().total#總內(nèi)存
wl= float(t-f)/float(t) #為使得最后值更精確,必須用float
t3.configure(text='物理內(nèi)存:'+str(floor(wl*100))+'%')
root.after(2000,wlnc)
這里的三個(gè)函數(shù)就是分別實(shí)現(xiàn)上面的三個(gè)功能的,最后將它添加到窗口事件即可。
可以看到這頁主要是系統(tǒng)運(yùn)行的一些應(yīng)用程序的名字,所以我們可以這樣這里我們需要用到模塊psutil 來獲取系統(tǒng)的關(guān)鍵參數(shù)。
應(yīng)用程序選項(xiàng)包含進(jìn)程號(hào)和進(jìn)程名和進(jìn)程文件路徑,所以可以用psutil進(jìn)行獲取,方法如下:
text.insert('insert','進(jìn)程號(hào) '+'進(jìn)程名 '+' 進(jìn)程文件路徑'+'\n')
for y in psutil.pids():
a=psutil.Process(y)
if a.name()=='System Idle Process':
continue
else:
text.insert('insert',str(y)+' '+a.name()+' '+a.exe()+'\n\n')
這樣就可以將這些內(nèi)容添加進(jìn)來了。
這里我們可以投機(jī)取巧,使用cmd中的tasklist命令,它可以打印出當(dāng)前系統(tǒng)所有在運(yùn)行的進(jìn)程的信息。
mm=os.popen('tasklist')
text.insert('insert',mm.read())
也是使用cmd中的sc 命令,它相當(dāng)于一個(gè)掃描器,可以得到很多有用的信息。
mm=os.popen('sc query type= service')
text.insert('insert',mm.read())
這個(gè)內(nèi)容會(huì)比較多點(diǎn),因?yàn)槲覀円玫奖容^多的參數(shù)和把組件放在多行文本框中,于是增加了一些冗余代碼:
l1=t.Label(root,text='開機(jī)時(shí)間:') tm=datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")
l2=t.Label(root,text=str(tm))
l3=t.Label(root,text='當(dāng)前時(shí)間:')
l4=t.Label(root,text='')
dq=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
l4.configure(text=str(dq))
l5=t.Label(root,text='物理內(nèi)存使用情況(MB):')
l6=t.Label(root,text='')
jh=psutil.virtual_memory() #物理內(nèi)存
tt=int((jh.total)/1024/1024) #總量
us=int((jh.used)/1024/1024) #使用量
fr=int((jh.free)/1024/1024) #剩余量
l6.configure(text='總量:' + str(tt) +'\n'+'使用:'+str(us) +'\n'+'剩余:'+str(fr))
l7=t.Label(root,text='交換內(nèi)存使用情況(MB):')
l8=t.Label(root,text='')
hj=psutil.swap_memory() #交換內(nèi)存
ht=int((hj.total)/1024/1024)
hu=int((hj.used)/1024/1024)
hf=int((hj.free)/1024/1024)
l8.configure(text='總量:' + str(ht) + ' '+'使用:'+str(hu) +' '+'剩余:'+str(hf))
text.window_create('insert',window=l1) #添加組件到多行文本框
text.window_create('insert',window=l2)
text.insert('insert','\n\n')
text.window_create('insert',window=l3)
text.window_create('insert',window=l4)
text.insert('insert','\n\n')
text.window_create('insert',window=l5)
text.window_create('insert',window=l6)
text.insert('insert','\n\n')
text.window_create('insert',window=l7)
text.window_create('insert',window=l8)
這里我們只獲取網(wǎng)卡的收發(fā)流量,因此:
n = psutil.net_io_counters()
r=str(float(n.bytes_recv / 1024 / 1024))+'MB'
s= str(float(n.bytes_sent / 1024 / 1024))+'MB'
text.insert('insert','網(wǎng)卡接收流量: '+str(r)+'\n'+'網(wǎng)卡發(fā)送流量:'+str(s)+'\n')
這里我們需要獲取當(dāng)前的用戶數(shù):
use=' 用戶'+' '+' 狀態(tài)'+'\n'
text.insert('insert',use)
for y in psutil.users():
text.insert('2.0',str(y.name)+' '+'運(yùn)行中。。。。'+'\n')
這樣就完成了任務(wù)管理器的編寫了。
通過對(duì)任務(wù)管理器的了解,使我們認(rèn)識(shí)到了系統(tǒng)中的一些至關(guān)重要的信息,比如說通過進(jìn)程名可以獲取進(jìn)程號(hào),通過進(jìn)程號(hào)可以獲取進(jìn)程名,cmd命令的用法,可以說是相當(dāng)親民了,希望本文可以幫到大家。
聯(lián)系客服