diff --git a/DemoPrograms/Demo_psutil_Kill_Python_Processes.py b/DemoPrograms/Demo_psutil_Kill_Python_Processes.py index 90147766..dace58bc 100644 --- a/DemoPrograms/Demo_psutil_Kill_Python_Processes.py +++ b/DemoPrograms/Demo_psutil_Kill_Python_Processes.py @@ -75,23 +75,23 @@ def get_all_procs(): return disp_data -def show_list_by_name(): +def show_list_by_name(python_only=False): disp_data = get_all_procs() disp_data = sorted(disp_data, key=operator.itemgetter(3), reverse=False) display_list = [] for process in disp_data: - if 'python' in process[2].lower(): + if not python_only or (python_only and 'python' in process[2].lower()): display_list.append('{:5d} {:5.2f} {} {}\n'.format(process[0], process[1], process[2], process[3])) return display_list -def show_list_by_cpu(): +def show_list_by_cpu(python_only=False): disp_data = get_all_procs() disp_data = sorted(disp_data, key=operator.itemgetter(1), reverse=True) display_list = [] for process in disp_data: - if 'python' in process[2].lower(): + if not python_only or (python_only and 'python' in process[2].lower()): display_list.append('{:5d} {:5.2f} {} {}\n'.format(process[0], process[1], process[2], process[3])) return display_list @@ -99,27 +99,32 @@ def show_list_by_cpu(): def make_window(): layout = [[sg.Text('Python Process Killer - Choose one or more processes', size=(45, 1), font=('Helvetica', 15), text_color='yellow')], - [sg.Listbox(values=[' '], size=(100, 20), select_mode=sg.SELECT_MODE_EXTENDED, font=('Courier', 10), key='-processes-')], + [sg.Listbox(values=[' '], size=(130, 30), select_mode=sg.SELECT_MODE_EXTENDED, font=('Courier', 10), key='-PROCESSES-', expand_x=True, expand_y=True)], [sg.Text('Click refresh once or twice.. once for list, second to get CPU usage')], - [sg.Text('Filter by typing name', font='ANY 14'), sg.Input(size=(15, 1), font='any 14', key='-filter-')], + [sg.Text('Filter by typing name', font='ANY 14'), sg.Input(size=(15, 1), font='any 14', key='-FILTER-', enable_events=True), + sg.Checkbox('Show only Python processes', default=True, enable_events=True, key='-PYTHON ONLY-')], [sg.Button('Sort by Name', ), sg.Button('Sort by % CPU', button_color=('white', 'DarkOrange2')), + sg.Button('Show Open Files', button_color=('white', 'dark green')), sg.Button('Kill Selected', button_color=('white', 'red'), bind_return_key=True), sg.Button('Kill All', button_color='red on white'), sg.Button('Kill All & Exit', button_color='red on white'), - sg.Exit(button_color=('white', 'sea green'))]] + sg.Exit(button_color=('white', 'sea green')), sg.Sizegrip()]] window = sg.Window('Python Process Killer', layout, keep_on_top=True, auto_size_buttons=False, default_button_element_size=(12, 1), return_keyboard_events=True, + resizable=True, + right_click_menu=sg.MENU_RIGHT_CLICK_EDITME_EXIT, finalize=True) - + window.bind('', 'Kill Selected') + window.set_min_size(window.size) return window -def kill_all(): - processes_to_kill = show_list_by_name() +def kill_all(python_only=True): + processes_to_kill = show_list_by_name(python_only=python_only) for proc in processes_to_kill: pid = int(proc[0:5]) try: @@ -130,15 +135,18 @@ def kill_all(): def main(silent=False): if silent: - kill_all() + kill_all(python_only=True) sg.popup_auto_close('Killed everything....', 'This window autocloses') sys.exit() # ---------------- Create Form ---------------- sg.theme('Dark Grey 9') + sg.set_options(icon=icon) + window = make_window() - display_list = show_list_by_name() - window['-processes-'].update(display_list) + current_display_list = display_list = show_list_by_name(window['-PYTHON ONLY-'].get()) + window['-PROCESSES-'].update(display_list) name_sorted = True + # ---------------- main loop ---------------- while True: # --------- Read and update window -------- @@ -152,13 +160,13 @@ def main(silent=False): # --------- Do Button Operations -------- if event == 'Sort by Name': - window['-processes-'].update(show_list_by_name()) + window['-PROCESSES-'].update(show_list_by_name(values['-PYTHON ONLY-'])) name_sorted = True elif event.startswith('Kill'): if event.startswith('Kill All'): - processes_to_kill = show_list_by_name() + processes_to_kill = show_list_by_name(values['-PYTHON ONLY-']) else: - processes_to_kill = values['-processes-'] + processes_to_kill = values['-PROCESSES-'] for proc in processes_to_kill: pid = int(proc[0:5]) try: @@ -167,24 +175,43 @@ def main(silent=False): except Exception as e: if event.endswith('Selected'): # only show the error if trying to kill only 1 process sg.popup_no_wait('Error killing process', e, auto_close_duration=2, auto_close=True, keep_on_top=True) - window['-processes-'].update(show_list_by_name() if name_sorted else show_list_by_cpu()) + current_display_list = show_list_by_name(values['-PYTHON ONLY-']) if name_sorted else show_list_by_cpu(values['-PYTHON ONLY-']) + window['-PROCESSES-'].update(current_display_list) if event.endswith('Exit'): break elif event == 'Sort by % CPU': - window['-processes-'].update(show_list_by_cpu()) + window['-PROCESSES-'].update(show_list_by_cpu(values['-PYTHON ONLY-'])) name_sorted = False - else: # was a typed character + elif event == 'Show Open Files': + for proc in values['-PROCESSES-']: + pid = int(proc[0:5]) + parent = psutil.Process(pid) + file_list = parent.open_files() + out = '' + for f in file_list: + out += f'{f}\n' + sg.popup_scrolled(out, non_blocking=True, keep_on_top=True,size=(100,30)) + elif event == '-PYTHON ONLY-': # if checkbox changed + current_display_list = show_list_by_name(values['-PYTHON ONLY-']) if name_sorted else show_list_by_cpu(values['-PYTHON ONLY-']) + window['-PROCESSES-'].update(current_display_list) + elif event == '-FILTER-': # was a typed character + # display_list = window['-PROCESSES-'].get_list_values() + display_list = current_display_list if display_list is not None: new_output = [] for line in display_list: - if values['-filter-'] in line.lower(): + if values['-FILTER-'] in line.lower(): new_output.append(line) - window['-processes-'].update(new_output) + window['-PROCESSES-'].update(new_output) + elif event == 'Edit Me': + sg.execute_editor(__file__) window.close() if __name__ == "__main__": + icon = b'iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAFLklEQVR4nO2ZW2xURRjHfzNnz9meXnYplHKtIhKEAgoEabUgKARBQTQ8+mLi5dXERIJPhMREownhyQfii8+aKKCCRIPRQiGEi3IRuSXc5dbSbbvdc5vxYUvAsmfP6bKtmuwvOS87s9/3/We++WbOHKhQoUKFChVKR4Q1fP3T3k1Jy5rluu5IxhOKZVk4rnvitWVtGwu1J4r8d0WyqqpVyFCtI4plJXFcdxIwVCHacZwcrutiGgkMwwCthynMEIQgCAK8wEdrDWgnrGuxGcl3MAz2nDzM2WuXQBpljTMSFTBtQhNLm+dFdo0WIg32nztJ+5H9YJoxnOv8yhNlSEnPY9G8VpbPXo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VHaTVhLHdeyw9mIzsrk/29fk+N6Qneb6c2Nvdna/jxgUsdZ67Oj0p1V21c2h2lS+D3AprH1YVu9b6z+eXD+u/mIhIV3Xux75/JMNl8vtc1j2kXGNdSKQ8sEU0ppxjXXDMnglG/1gy2ezlTJbpSFNNei9WgjRiNYbC80IQmzSWt+4/2cpJSpQnonu+PC9d06UEk9JQtZv3jpLCHYnq+yJd0+4g+MtttgH67trw8nlrkgpV3z07psnhxpTSaklpFhRU5ua6DoOhFwvJ0LuibXWA58IHrBKTV1qUl9v5kVgZIRIoS/5nrvT90I/V5SElAIpdGhlqlChQoUK/zn+BhjXF7IsC7cbAAAAAElFTkSuQmCC' + if len(sys.argv) == 2 and sys.argv[1] == 'silent': main(silent=True) else: - main(silent=False) + main(silent=False) \ No newline at end of file