()
| 59 | return display_list |
| 60 | |
| 61 | def main(): |
| 62 | |
| 63 | # ---------------- Create Form ---------------- |
| 64 | sg.theme('Dark Grey 9') |
| 65 | |
| 66 | layout = [[sg.Text('Process Killer - Choose one or more processes', |
| 67 | size=(45,1), font=('Helvetica', 15), text_color='yellow')], |
| 68 | [sg.Listbox(values=[' '], size=(130, 30), select_mode=sg.SELECT_MODE_EXTENDED, horizontal_scroll=True, font=('Courier', 12), key='-PROCESSES-')], |
| 69 | [sg.Col([ |
| 70 | [sg.Text('Click refresh once or twice.. once for list, second to get CPU usage')], |
| 71 | [sg.Text('Filter by typing name', font='ANY 14'), sg.Input(size=(15,1), font='any 14', key='-FILTER-')], |
| 72 | [sg.Button('Sort by Name', ), |
| 73 | sg.Button('Sort by % CPU', button_color=('white', 'DarkOrange2')), |
| 74 | sg.Button('Kill', button_color=('white','red'), bind_return_key=True), |
| 75 | sg.Exit(button_color=('white', 'sea green')), sg.Sizegrip()]], expand_x=True) ]] |
| 76 | |
| 77 | window = sg.Window('Process Killer', layout, |
| 78 | keep_on_top=True, |
| 79 | auto_size_buttons=False, |
| 80 | default_button_element_size=(12,1), |
| 81 | return_keyboard_events=True, |
| 82 | resizable=True, |
| 83 | right_click_menu=sg.MENU_RIGHT_CLICK_EDITME_EXIT, |
| 84 | finalize=True) |
| 85 | window['-PROCESSES-'].expand(True, True) |
| 86 | window.set_min_size(window.size) |
| 87 | display_list = show_list_by_name(window) |
| 88 | # ---------------- main loop ---------------- |
| 89 | while True: |
| 90 | # --------- Read and update window -------- |
| 91 | event, values = window.read() |
| 92 | if event in (sg.WIN_CLOSED, 'Exit'): |
| 93 | break |
| 94 | |
| 95 | # skip mouse, control key and shift key events entirely |
| 96 | if 'Mouse' in event or 'Control' in event or 'Shift' in event: |
| 97 | continue |
| 98 | |
| 99 | # --------- Do Button Operations -------- |
| 100 | if event == 'Sort by Name': |
| 101 | display_list = show_list_by_name(window) |
| 102 | # psutil.cpu_percent(interval=.1) |
| 103 | # procs = psutil.process_iter() |
| 104 | # all_procs = [[proc.cpu_percent(), proc.name(), proc.pid] for proc in procs] |
| 105 | # sorted_by_cpu_procs = sorted(all_procs, key=operator.itemgetter(1), reverse=False) |
| 106 | # display_list = [] |
| 107 | # for process in sorted_by_cpu_procs: |
| 108 | # display_list.append('{:5d} {:5.2f} {}\n'.format(process[2], process[0]/10, process[1])) |
| 109 | # window['-PROCESSES-'].update(display_list) |
| 110 | new_output = [] |
| 111 | for line in display_list: |
| 112 | if values['-FILTER-'] in line.lower(): |
| 113 | new_output.append(line) |
| 114 | window['-PROCESSES-'].update(new_output) |
| 115 | elif event == 'Kill': |
| 116 | processes_to_kill = values['-PROCESSES-'] |
| 117 | for proc in processes_to_kill: |
| 118 | pid = int(proc[0:5]) |
no test coverage detected