获取当前所有进程 python
# -*- coding: utf-8 -*-
#!/usr/bin/env python3
'''
psutil==6.0.0
'''
import psutil
import tkinter as tk
from tkinter import ttk
import socket
# 定义一个函数来更新列表显示
def update_list():
# 获取搜索框中的文本
search_text = search_entry.get().lower()
listbox.delete(0, tk.END) # 清空 listbox
# 根据搜索文本更新列表显示
for index, item in enumerate(get_all_processes()):
if search_text in item.lower():
if search_text in item.lower(): # 如果搜索文本在进程信息中
listbox.insert(tk.END, item) # 将匹配的进程信息添加到 listbox
else:
listbox.selection_clear(index)
def end_process():
# 获取选中的进程 ID
selection = listbox.curselection()
if selection:
index = int(selection[0])
print('索引:',index)
selection = listbox.get(selection)
print('selection:',selection)
parts = selection.split(", ")
print('parts:',parts)
# 检查 parts 列表是否有足够的元素
# 若get_all_processes()函数的拼接方式出现改变,这里的截取方式都需要调整
pid_part = parts[0].split(":")[1] # 尝试获取 PID
pid_part = int(pid_part) # 将 PID 转换为整数
name_part = parts[1].split(":")[1] # "System Idle Process"
username_part = parts[2].split(":")[1] # "NT AUTHORITY\SYSTEM"
print('pid_part:',pid_part,'name_part',name_part,'username_part',username_part)
try:
# 通过进程 ID 结束进程
proc = psutil.Process(pid_part)
proc.terminate() # 发送终止信号
listbox.delete(index) # 从列表中删除进程项
except (psutil.NoSuchProcess, psutil.AccessDenied) as e:
print(f"Error terminating process {pid_part}: {e}")
listbox.delete(index) # 从列表中删除进程项
def get_all_processes():
# 获取当前运行的所有进程的列表
processes = psutil.process_iter(['pid', 'name', 'username'])
proList=[]
for proc in processes:
try:
pid = proc.info['pid']
name = proc.info['name']
username = proc.info['username']
# 使用 net_connections() 获取网络连接信息
# 这里因为获取了当前进程使用的所有端口号,所以可能会拖慢程序的速度,使用过程中的体验就是突然顿了一下又好了
ports = set()
for conn in psutil.net_connections(kind='inet'):
if conn.pid == pid and conn.type == socket.SOCK_STREAM and conn.status == 'ESTABLISHED':
ports.add(conn.laddr.port)
ports_str = ', '.join(map(str, ports)) if ports else '无'
process_info = f"进程ID:{pid}, 进程名:{name}, 进程所属用户名:{username}, 使用的端口号:{ports_str}"
proList.append(process_info)
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
# 忽略已经结束的进程或访问被拒绝的进程
pass
return proList
def center_window(window):
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
window_width = window.winfo_reqwidth()
window_height = window.winfo_reqheight()
x = (screen_width // 2) - (window_width // 2)
y = (screen_height // 2) - (window_height // 2)
window.geometry(f'+{x}+{y}')
# 创建主窗口
root = tk.Tk()
root.title("Tkinter List Search")
root.geometry("400x400")
center_window(root)
# 创建搜索框
search_frame = tk.Frame(root)
search_label = tk.Label(search_frame, text="Search:")
search_label.pack(side=tk.LEFT)
search_entry = tk.Entry(search_frame)
search_entry.pack(side=tk.LEFT, expand=True, fill=tk.X)
search_entry.bind("<Return>", lambda event: update_list())
search_frame.pack(fill=tk.X)
# 创建列表显示
listbox = tk.Listbox(root)
listbox.pack(fill=tk.BOTH, expand=True)
# 填充列表
for item in get_all_processes():
print(item)
listbox.insert(tk.END, item)
# 搜索框获得焦点
search_entry.focus()
# 更新列表的函数绑定到搜索框的键入事件
search_entry.bind("<KeyRelease>", lambda event: update_list())
# 创建右键菜单
right_click_menu = tk.Menu(root, tearoff=0)
end_process_item = right_click_menu.add_command(label="结束进程", command=end_process)
# 绑定右键点击事件到 Listbox
listbox.bind("<Button-3>", lambda event: right_click_menu.post(event.x_root, event.y_root))
if __name__ == "__main__":
root.mainloop()
版权声明:
作者:kukey-admin
链接:http://kuyour.top/index.php/2024/08/11/%e8%8e%b7%e5%8f%96%e5%bd%93%e5%89%8d%e6%89%80%e6%9c%89%e8%bf%9b%e7%a8%8b-python/
来源:KuKey
文章版权归作者所有,未经允许请勿转载。
THE END
0
二维码
打赏
海报
获取当前所有进程 python
# -*- coding: utf-8 -*-
#!/usr/bin/env python3
'''
psutil==6.0.0
'''
import psutil
import tkinter as tk
from tkinter import ttk
import socke……
共有 0 条评论