当前位置: 首页 > 科技观察

教你用Python仿一个任务管理器

时间:2023-03-18 20:55:54 科技观察

大家好,我是Python进阶。前言任务管理器相信大家都不陌生,可以通过Ctrl+Alt+Del打开,然后点击启动任务管理器,或者右键任务栏-启动任务管理器启动任务管理器,启动后的界面如下:可以看到列出了一些重要的参数,比如进程数、CPU占用率、物理内存等。接下来,我们将一一列举。一、项目准备编辑器:sublimetext3个模块:psutiltkinter二、实现步骤1、编写主界面要实现任务管理器,首先我们要写一个界面。这里我们使用tkinter写一个界面:2.写菜单Bar我们先来实现它的菜单栏。这里我们使用了tkinter的Menu模块。不知道大家有没有印象,下面开始吧。1)首先我们需要创建一个主菜单,然后将每个子菜单放入其中:m=t.Menu(root)2)创建每个子菜单:#FileMenufile=t.Menu(m,tearoff=False)m。add_cascade(label='file',menu=file)file.add_command(label='newtask',accelerator='(N)')file.add_command(label='exittaskbarmanager',command=root.quit,加速器='(x)')#Optionmenuii=t.IntVar()ii.set(1)o=t.Menu(m,tearoff=False)m.add_cascade(label='option',menu=o)o.add_radiobutton(label='前置显示',variable=ii,value=0)o.add_radiobutton(label='使用时最小化',variable=ii,value=1)o.add_radiobutton(label='最小化时隐藏',variable=ii,value=2)#Viewmenuv=t.Menu(m,tearoff=False)m.add_cascade(label='View',menu=v)v.add_command(label='Refreshnow')#Secondarymenuiv=t.IntVar()iv.set(1)s=t.Menu(v,tearoff=False)v.add_cascade(label='更新速度',menu=s)s.add_radiobutton(label='high',variable=iv,value=0)s.add_radiobutton(label='normal',variable=iv,value=1)s.add_radiobutton(label='low',variable=iv,value=2)s.add_radiobutton(label='pause',variable=iv,value=3)v.add_command(label='optioncolumn')#helpmenuh=t.Menu(m,tearoff=False)m.add_cascade(label='Help',menu=h)h.add_command(label='任务管理Managerhelpmainbody')h.add_command(label='AboutTaskManager')3)添加菜单到主界面配置root.configure(menu=m)最后的结果图,可以看到,基本和任务管理器差不多3.界面中的功能界面写好后,我们要给界面添加组件。从任务管理器的图片可以看出,它有一个切换任务窗口的按钮:1)写按钮b1=t.Button(root,text='application',command=yy)b2=t.Button(root,text='process',command=jc)b3=t.Button(root,text='service',command=fw)b4=t.Button(root,text='performance',command=xn)b5=t.Button(root,text='network',command=lw)b6=t.Button(root,text='user',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)写一个多行文本框text=t.Text(root,width=100,height=40)text.place(x=10,y=36)3)写一个函数实现简单的功能defyy():text.delete(1.0,'end')text.insert('insert','yy')defjc():text.delete(1.0,'end')text.insert('insert','jc')deffw():text.delete(1.0,'end')text.insert('insert','fw')defxn():text.delete(1.0,'end')text.insert('insert','xn')deflw():text.delete(1.0,'end')text.insert('insert','lw')defyh():文本。delete(1.0,'end')text.insert('insert','yh')这样可以在不同的按钮之间切换不同的界面。4)写入如下进程数、CPU占用率、物理内存。我们用labels来放置这些参数,因为这三项的参数是可变的,所以暂时只写前面的名字:t1=t.Label(text='processNumber:')t2=t.Label(text='CPU使用率:')t3=t.Label(text='物理内存:')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)给多行文本框添加滚动条我们可以使用模块Scrollbar来实现,安装前需要做两件事滚动条:1.将组件的yscrollbarcommand参数指定为Scrollbar的set()方法2.将Scrollbar的command参数指定为组件的yview()方法接下来我们实现: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)#文本框内容随滚动条滚动text.config(yscrollcommand=sb.set(0.1,0.3))#Y轴填充6)添加状态栏文本标签t1=t.Label(文本='')t2=t.Label(text='')t3=t.Label(text='')(注:这只是一个隐藏部分,切勿使用destroy来销毁该部分)7)实现状态栏标签函数下面我们来实现这三个标签的内容。想必大家应该刚刚看到上面的标签没有设置任何内容,那么这是为什么呢?我们都知道,一旦添加内容,内容就会随之而来,不会被覆盖,所以初始值必须为空,这样值才不会被覆盖。那么我们来看看具体的实现过程。defjcs():t1.configure(text='进程数:'+str(len(psutil.pids())))root.after(3000,jcs)defcpu():pp=str(ceil(psutil.cpu_percent(1)))t2.configure(text='CPU使用率:'+pp+'%')root.after(1500,cpu)defwlnc():f=psutil.virtual_memory().free#剩余内存t=psutil.virtual_memory().total#Totalmemorywl=float(t-f)/float(t)#为了让最终的值更加准确,必须使用floatt3.configure(text='physicalmemory:'+str(floor(wl*100))+'%')root.after(2000,wlnc)这里的三个函数就是分别实现以上三个功能,最后添加到window事件中。8)函数编写可以看到这个页面主要是系统运行的一些应用程序的名称,所以我们可以这样做这里需要使用模块psutil来获取系统的关键参数。1.编写应用选项应用选项包括进程号、进程名和进程文件路径,可以使用psutil获取,方法如下:text.insert('insert','进程号'+'进程名'+'进程文件路径'+'\n')foryinpsutil.pids():a=psutil.Process(y)ifa.name()=='SystemIdleProcess':continueelse:text.insert('insert',str(y)+''+a.name()+''+a.exe()+'\n\n')这样就可以添加这些内容了。2.写进程选项这里我们可以投机取巧,使用cmd中的tasklist命令,可以打印出当前系统所有运行进程的信息。mm=os.popen('tasklist')text.insert('insert',mm.read())3.写服务选项也是用cmd中的sc命令,相当于一个扫描仪,可以得到很多有用的信息。mm=os.popen('scquerytype=service')text.insert('insert',mm.read())4.写性能选项的内容会比较多,因为我们需要获取更多的参数,把组件放在里面多行文本框,增加了一些冗余代码:l1=t.Label(root,text='Boottime:')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='当前时间:')l4=t.Label(root,text='')dq=time.strftime('%Y-%m-%d%H:%M:%S',time.localtime(time.time()))l4.configure(文本=str(dq))l5=t.Label(root,text='物理内存占用(MB):')l6=t.Label(root,text='')jh=psutil.virtual_memory()#物理内存tt=int((jh.total)/1024/1024)#总金额us=int((jh.used)/1024/1024)#Usagefr=int((jh.free)/1024/1024)#剩余金额l6.configure(text='总量:'+str(tt)+'\n'+'使用:'+str(us)+'\n'+'剩余:'+str(fr))l7=t.label(root,text='swap内存占用(MB):')l8=t.Label(root,text='')hj=psutil.swap_memory()#swap内存ht=int((hj.total)/1024/1024)hu=int((hj.used)/1024/1024)hf=int((hj.free)/1024/1024)l8.configure(text='Total:'+str(ht)+''+'use:'+str(hu)+''+'remaining:'+str(hf))text.window_create('insert',window=l1)#给多行文本框添加组件文本.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)5.写入网络选项这里我们只获取网卡的发送和接收流量,所以: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','网卡接收流量:'+str(r)+'\n'+'网卡发送流量:'+str(s)+'\n')6.写用户选项这里我们需要获取当前用户数:use='user'+''+'status'+'\n'text.insert('insert',use)foryinpsutil.users():text.insert('2.0',str(y.name)+''+'running...'+'\n')这样就完成了任务管理器的编写。3、小结通过对任务管理器的了解,我们了解到了系统中的一些关键信息,比如可以通过进程名获取进程号,通过进程号获取进程名,以及cmd命令可以说是相当的亲民了,希望这篇文章可以帮助到大家。