99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁(yè)精彩閱讀手把手教你用Python高仿一個(gè)任務(wù)管理器
手把手教你用Python高仿一個(gè)任務(wù)管理器
2021-07-23
收藏

來(lái)源:Python爬蟲(chóng)與數(shù)據(jù)挖掘

作者:Python進(jìn)階者

大家好,我是Python進(jìn)階者。

前言

相信大家對(duì)任務(wù)管理器都不是很陌生了,Ctrl+Alt+Del即可打開(kāi),然后點(diǎn)擊啟動(dòng)任務(wù)管理器,或者右擊任務(wù)欄-啟動(dòng)任務(wù)管理器即可啟動(dòng)任務(wù)管理器,啟動(dòng)之后界面如下:

手把手教你用Python高仿一個(gè)任務(wù)管理器

可以看出它列舉出了一些重要的參數(shù),比如進(jìn)程數(shù)量,CPU使用率,物理內(nèi)存,接下來(lái)我們就來(lái)一一列舉出來(lái)。

一、項(xiàng)目準(zhǔn)備

編輯器:sublime text 3

模塊:psutil tkinter

二、實(shí)現(xiàn)步驟

1、編寫(xiě)主界面

要想實(shí)現(xiàn)任務(wù)管理器,首先我們寫(xiě)一個(gè)界面出來(lái),這里我們運(yùn)用tkinter編寫(xiě)個(gè)界面出來(lái):

手把手教你用Python高仿一個(gè)任務(wù)管理器

2、編寫(xiě)菜單欄

我們先實(shí)現(xiàn)下它的菜單欄,這里用到tkinter的Menu模塊,不知道大家有沒(méi)有印象,那么開(kāi)始吧。

1)首先我們需要?jiǎng)?chuàng)建一個(gè)主菜單,然后將各個(gè)子菜單裝進(jìn)去:

m=t.Menu(root)

2)創(chuàng)建各個(gè)子菜單:

#文件菜單 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ù)管理器')

3)將菜單添加到主界面配置中

root.configure(menu=m)

最后結(jié)果圖,可以看到,基本和任務(wù)管理器差不多。

手把手教你用Python高仿一個(gè)任務(wù)管理器

3、界面中的功能

界面寫(xiě)完了我們?cè)撓蚪缑嫣砑咏M件了,由任務(wù)管理器那張圖我們可以看到它有一個(gè)切換任務(wù)窗口的按鈕:

1)編寫(xiě)按鈕

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)

2)編寫(xiě)多行文本框

text=t.Text(root,width=100,height=40)
text.place(x=10,y=36)

3)編寫(xiě)函數(shù)實(shí)現(xiàn)簡(jiǎn)單功能

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)了不同按鈕之間切換不同的界面。

4)編寫(xiě)下面的進(jìn)程數(shù),CPU使用率,物理內(nèi)存

我們使用標(biāo)簽來(lái)放置這些參數(shù),因?yàn)檫@三項(xiàng)的參數(shù)是可變的,所以暫時(shí)只寫(xiě)前面名字:

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)

5)給多行文本框添加滾動(dòng)條

我們可以使用模塊Scrollbar來(lái)實(shí)現(xiàn),安裝滾動(dòng)條之前需要做兩件事情:

1.指定該組件的yscrollbarcommand參數(shù)為Scrollbar的set()方法

2.指定Scrollbar 的 command 參數(shù)為該組件的 yview() 方法

接下來(lái)我們實(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軸填充

6)添加狀態(tài)欄文本標(biāo)簽

t1=t.Label(text='') t2=t.Label(text='') t3=t.Label(text='')

(注:這里只是隱藏部件,萬(wàn)不可用destroy銷(xiāo)毀部件)

7)實(shí)現(xiàn)狀態(tài)欄標(biāo)簽功能

現(xiàn)在我們來(lái)實(shí)現(xiàn)這三個(gè)標(biāo)簽的內(nèi)容。想必大家剛剛應(yīng)該看到了,上面的標(biāo)簽沒(méi)有設(shè)置任何內(nèi)容,那么這是為什么呢?我們都知道,一旦你把內(nèi)容添加進(jìn)去,它就會(huì)緊隨其后并不會(huì)覆蓋,所以初始值必須是空,才不至于不能覆蓋值。那么我們來(lái)看下具體實(shí)現(xiàn)過(guò)程吧。

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è)功能的,最后將它添加到窗口事件即可。

8)功能編寫(xiě)

可以看到這頁(yè)主要是系統(tǒng)運(yùn)行的一些應(yīng)用程序的名字,所以我們可以這樣這里我們需要用到模塊psutil 來(lái)獲取系統(tǒng)的關(guān)鍵參數(shù)。

1.編寫(xiě)應(yīng)用程序選項(xiàng)

應(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()+'nn')

這樣就可以將這些內(nèi)容添加進(jìn)來(lái)了。

2.編寫(xiě)進(jìn)程選項(xiàng)

這里我們可以投機(jī)取巧,使用cmd中的tasklist命令,它可以打印出當(dāng)前系統(tǒng)所有在運(yùn)行的進(jìn)程的信息。

mm=os.popen('tasklist')
text.insert('insert',mm.read())

3.編寫(xiě)服務(wù)選項(xiàng)

也是使用cmd中的sc 命令,它相當(dāng)于一個(gè)掃描器,可以得到很多有用的信息。

mm=os.popen('sc query type= service')
text.insert('insert',mm.read())

4.編寫(xiě)性能選項(xiàng)

這個(gè)內(nèi)容會(huì)比較多點(diǎn),因?yàn)槲覀円玫奖容^多的參數(shù)和把組件放在多行文本框中,于是增加了一些冗余代碼:

l1=t.Label(root,text='開(kāi)機(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','nn')
  text.window_create('insert',window=l3)
  text.window_create('insert',window=l4)
  text.insert('insert','nn')
  text.window_create('insert',window=l5)
  text.window_create('insert',window=l6)
  text.insert('insert','nn')
  text.window_create('insert',window=l7)
  text.window_create('insert',window=l8)

5.編寫(xiě)聯(lián)網(wǎng)選項(xiàng)

這里我們只獲取網(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')

6.編寫(xiě)用戶選項(xiàng)

這里我們需要獲取當(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ù)管理器的編寫(xiě)了。

手把手教你用Python高仿一個(gè)任務(wù)管理器

三、總結(jié)

通過(guò)對(duì)任務(wù)管理器的了解,使我們認(rèn)識(shí)到了系統(tǒng)中的一些至關(guān)重要的信息,比如說(shuō)通過(guò)進(jìn)程名可以獲取進(jìn)程號(hào),通過(guò)進(jìn)程號(hào)可以獲取進(jìn)程名,cmd命令的用法,可以說(shuō)是相當(dāng)親民了,希望本文可以幫到大家。

數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說(shuō)明請(qǐng)參見(jiàn):http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }