Python Process Monitor Tutorial
To create a Python program for a process sniffer (which monitors and selects processes), you can utilize the psutil library to obtain current system process information, and use the tkinter library to create a graphical interface for displaying process information and performing selection operations.
Here is a simple example code that implements a basic process checker functionality.
import psutil
import tkinter as tk
def update_process_list():
process_list.delete(0, tk.END) # 清空进程列表
for proc in psutil.process_iter():
try:
process = psutil.Process(proc.pid)
process_list.insert(tk.END, f"{proc.pid}: {process.name()}") # 插入进程信息
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
def on_select():
selected_index = process_list.curselection()
if selected_index:
selected_pid = int(process_list.get(selected_index).split(':')[0])
selected_process = psutil.Process(selected_pid)
print(f"Selected process: {selected_process.name()}")
# 创建主窗口
root = tk.Tk()
root.title("Process Selector")
# 创建进程列表框
process_list = tk.Listbox(root, width=50)
process_list.pack()
# 创建更新按钮
update_button = tk.Button(root, text="Update", command=update_process_list)
update_button.pack()
# 创建选择按钮
select_button = tk.Button(root, text="Select", command=on_select)
select_button.pack()
# 初始化进程列表
update_process_list()
# 运行主事件循环
root.mainloop()
The code above creates a simple graphical interface that displays a list of processes currently running on the system. Users can choose a process and click the “Select” button to obtain information about the selected process. It is important to note that this example only showcases basic functionality, and you can further enhance and customize it as needed.