目录
1.实现代码
查找所有进程信息
窗口实现
使用tkinter实现gui窗口
效果展示
安装包
使用Python的Tkinter库实现一个gui内置 psutil 库来查询,控制系统中的进程。这样我们就可以简单的控制系统进程,另外增加了杀死非系统进程的功能,当进程太杂乱就可以直接杀死所有进程。
关于查找,杀死,查看详细信息。这些功能,我们完全可以用 psutil 库来实现。不得不说,psutil库真的很强大!缺点就是当系统用户权限不够时很多进程无法访问到。
def Get_ALL_Process():
Process_message = {}
pids = psutil.pids()
for pid in pids:
process = psutil.Process(pid)
MianProcess = process.parent()
try:
Process_message[f"{process.name()} | {pid}"] = {
"进程名:":process.name(),
"进程PID:":pid,
"是否运行:":process.is_running(),
"路径":process.exe(),
"父进程:":MianProcess.name(),
"父进程pid:":process.ppid(),
"创建时间:":process.create_time(),
"用户名":process.username(),
}
except:
continue
return Process_message
这样就可以获取到进程的json数据信息。以便我们查找时二次查找麻烦。
有内部内容肯定要有个gui窗口了,不然就太没感觉了!
def Insert_Process(ListBox):
ListBox.delete(0, END)
data = Get_ALL_Process()
ListBox.delete(0, END)
for ProcessName in data.keys():
ListBox.insert("end", ProcessName)
return data
class ProcessGui:
def __init__(self):
self.tk = tkinter.Tk()
self.ProcessLog = {"None":"None"}
def main(self):
self.tk.title("Process Manager")
self.tk.geometry("800x400")
self.tk.iconbitmap(f"{os.path.split(__file__)[0]}/image/icon.ico")
self.ProcessList = Listbox(self.tk, width=40, height=20)
self.ProcessList.place(x=1, y=1)
try:
self.ProcessLog = Insert_Process(self.ProcessList)
except:
self.ProcessLog = Insert_Process(self.ProcessList)
Button(text="刷新列表", font=("", 10), command=self.Refresh_List).place(x=1, y=370)
Button(text="查看进程", font=("", 10), command=self.See).place(x=70, y=370)
Button(text="杀死进程", font=("", 10), command=self.Kill).place(x=140, y=370)
Button(text="杀死非系统进程", font=("", 10), command=self.NotSystem).place(x=210, y=370)
Button(text="CPU情况", font=("", 10), command=CPU.CPU_GUI().main).place(x=320, y=370)
self.tk.mainloop()
def Refresh_List(self):
#self.ProcessList.delete(0, END)
self.ProcessLog = Insert_Process(self.ProcessList)
self.tk.update()
def See(self):
ProcessName = self.ProcessList.get(self.ProcessList.curselection())
data = self.ProcessLog[ProcessName]
self.ProcessNameLabel = Label(self.tk, width=40, bg="grey", fg="red", font=("", 15))
self.ProcessNameLabel.place(x=340, y=10)
self.ProcessNameLabel['text'] = ProcessName
Label(text="进程PID:").place(x=300, y=60)
self.ProcessPID = Text(self.tk, bg="Cyan", height=1, width=10)
self.ProcessPID.place(x=360, y=64)
self.ProcessPID.insert("end", data['进程PID:'])
Label(text="进程路径:").place(x=300, y=110)
self.ProcessPath = Text(self.tk, bg="Cyan", height=3, width=40)
self.ProcessPath.place(x=360, y=114)
self.ProcessPath.insert("end", data['路径'])
Label(text="创建时间:").place(x=300, y=170)
self.ProcessCreate_time = Label(self.tk, bg="Cyan")
self.ProcessCreate_time.place(x=360, y=170)
#%Y/%m/%d-
self.ProcessCreate_time['text'] = time.strftime("%Y/%m/%d-%H:%M:%S", time.localtime(data['创建时间:']))
Label(text="父进程:").place(x=300, y=220)
self.ProcessMain = Label(self.tk, bg="Cyan")
self.ProcessMain.place(x=360, y=220)
self.ProcessMain['text'] = data['父进程:']
Label(text="父进程pid:").place(x=300, y=280)
self.ProcessMainPid = Text(self.tk, bg="Cyan", height=1, width=10)
self.ProcessMainPid.place(x=360, y=284)
self.ProcessMainPid.insert("end", data["父进程pid:"])
Label(text="用户名信息:").place(x=300, y=340)
self.ProcessIf_System = Label(self.tk, bg="Cyan")
self.ProcessIf_System.place(x=370, y=340)
self.ProcessIf_System['text'] = f"{data['用户名']}"
global img
try:
Get_Exe_Icon.get_exe_icon(data['路径'])
img = ImageTk.PhotoImage(Image.open(f"{os.path.split(__file__)[0]}/save.jpg"))
except:
img = ImageTk.PhotoImage(Image.open(f"{os.path.split(__file__)[0]}/init.jpg"))
self.icon = Label(self.tk, bg="Cyan")
self.icon.pack()
self.icon.config(image=img)
self.icon.place(x=600, y=250)
def Kill(self):
ProcessName = self.ProcessList.get(self.ProcessList.curselection())
PID = self.ProcessLog[ProcessName]['进程PID:']
try:
psutil.Process(PID).kill()
tkinter.messagebox.showerror("成功!", f"已成功杀死进程:{ProcessName}/{PID}")
self.Refresh_List()
except:
tkinter.messagebox.showerror("进程不存在!", "进程可能已关闭!")
def NotSystem(self):
system = ["Process Manager.exe"]
with open(f"{os.path.split(__file__)[0]}/SystemProcessList.txt", "r", encoding="utf-8") as f:
for read in f.readlines():
system.append(read.strip())
for value in Get_ALL_Process().values():
print(value)
if value['进程名:'] in system:
continue
else:
try:
print(value['进程名:'], value['进程PID:'])
psutil.Process(value['进程PID:']).kill()
except:
print("权限不足!")
continue
self.Refresh_List()
这样我们就生成了一个简易的操作窗口了。
另外我给程序添加了安装包可以直接安装程序:Process Manager Setup.exe