Нужна помощь, решил написать мини диспетчер задач, но вот проблема не работает кнопка "Kill"
- чтобы завершить процесс нажимаю ее, но ничего не происходит.
Вот весь код:
import psutil
import os
import tkinter as tk
from tkinter import ttk, Menu, simpledialog
class TaskManagerApp:
def __init__(self, root):
self.root = root
self.root.title("Task Manager")
self.process_tree = ttk.Treeview(self.root)
self.process_tree["columns"] = ("PID")
self.process_tree.column("#0", width=250)
self.process_tree.column("PID", width=100)
self.process_tree.heading("#0", text="Process Name")
self.process_tree.heading("PID", text="PID")
self.process_tree.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
self.search_entry = tk.Entry(self.root, width=40)
self.search_entry.pack()
self.process_tree.bind("<Button-3>", self.show_context_menu)
update_button = tk.Button(self.root, text="Update", command=self.update_running_processes)
update_button.pack(pady=10)
add_button = tk.Button(self.root, text="Add Process", command=self.add_process)
add_button.pack()
self.selected_pid = None
self.process_groups = {} # Словарь для группировки процессов по имени
def clear_process_groups(self):
for process_group in self.process_groups.values():
self.process_tree.delete(process_group)
self.process_groups = {}
def update_running_processes(self):
self.clear_process_groups() # Очистка групп перед обновлением
search_text = self.search_entry.get().lower()
for pid, name in self.get_running_processes():
if search_text in name.lower():
process_group = self.process_groups.get(name, None)
if process_group is None:
process_group = self.process_tree.insert("", "end", text=name, values=(pid))
self.process_groups[name] = process_group
else:
self.process_tree.insert(process_group, "end", text=name, values=(pid))
def update_all(self):
self.update_running_processes()
def get_running_processes(self):
processes = []
for process in psutil.process_iter(['pid', 'name']):
processes.append((process.info['pid'], process.info['name']))
return processes
def select_process(self, event):
item = self.process_tree.identify('item', event.x, event.y)
if item:
self.selected_pid = int(self.process_tree.item(item, "values")[0])
else:
self.selected_pid = None
def show_context_menu(self, event):
menu = Menu(self.root, tearoff=0)
menu.add_command(label="Kill", command=self.kill_selected_process)
menu.post(event.x_root, event.y_root)
def add_process(self):
process_name = simpledialog.askstring("Add Process", "Enter process name:")
if process_name:
try:
process = psutil.Popen(process_name)
except Exception as e:
print(e)
self.update_all()
def kill_selected_process(self):
if self.selected_pid is not None:
try:
process = psutil.Process(self.selected_pid)
process.terminate()
process.wait()
process.suspend()
except psutil.NoSuchProcess:
pass
self.update_all()
if __name__ == "__main__":
root = tk.Tk()
app = TaskManagerApp(root)
root.mainloop()