Checking in some accumulated changes - Added Sizegrip, window now resizable, show open files, filter checkbox for Python processes, Edit Me right click menu

This commit is contained in:
PySimpleGUI 2022-01-15 17:34:49 -05:00
parent 40757180b5
commit 3aa82075a9
1 changed files with 49 additions and 22 deletions

View File

@ -75,23 +75,23 @@ def get_all_procs():
return disp_data return disp_data
def show_list_by_name(): def show_list_by_name(python_only=False):
disp_data = get_all_procs() disp_data = get_all_procs()
disp_data = sorted(disp_data, key=operator.itemgetter(3), reverse=False) disp_data = sorted(disp_data, key=operator.itemgetter(3), reverse=False)
display_list = [] display_list = []
for process in disp_data: 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])) display_list.append('{:5d} {:5.2f} {} {}\n'.format(process[0], process[1], process[2], process[3]))
return display_list return display_list
def show_list_by_cpu(): def show_list_by_cpu(python_only=False):
disp_data = get_all_procs() disp_data = get_all_procs()
disp_data = sorted(disp_data, key=operator.itemgetter(1), reverse=True) disp_data = sorted(disp_data, key=operator.itemgetter(1), reverse=True)
display_list = [] display_list = []
for process in disp_data: 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])) display_list.append('{:5d} {:5.2f} {} {}\n'.format(process[0], process[1], process[2], process[3]))
return display_list return display_list
@ -99,27 +99,32 @@ def show_list_by_cpu():
def make_window(): def make_window():
layout = [[sg.Text('Python Process Killer - Choose one or more processes', layout = [[sg.Text('Python Process Killer - Choose one or more processes',
size=(45, 1), font=('Helvetica', 15), text_color='yellow')], 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('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 Name', ),
sg.Button('Sort by % CPU', button_color=('white', 'DarkOrange2')), 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 Selected', button_color=('white', 'red'), bind_return_key=True),
sg.Button('Kill All', button_color='red on white'), sg.Button('Kill All', button_color='red on white'),
sg.Button('Kill All & Exit', 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, window = sg.Window('Python Process Killer', layout,
keep_on_top=True, keep_on_top=True,
auto_size_buttons=False, auto_size_buttons=False,
default_button_element_size=(12, 1), default_button_element_size=(12, 1),
return_keyboard_events=True, return_keyboard_events=True,
resizable=True,
right_click_menu=sg.MENU_RIGHT_CLICK_EDITME_EXIT,
finalize=True) finalize=True)
window.bind('<Delete>', 'Kill Selected')
window.set_min_size(window.size)
return window return window
def kill_all(): def kill_all(python_only=True):
processes_to_kill = show_list_by_name() processes_to_kill = show_list_by_name(python_only=python_only)
for proc in processes_to_kill: for proc in processes_to_kill:
pid = int(proc[0:5]) pid = int(proc[0:5])
try: try:
@ -130,15 +135,18 @@ def kill_all():
def main(silent=False): def main(silent=False):
if silent: if silent:
kill_all() kill_all(python_only=True)
sg.popup_auto_close('Killed everything....', 'This window autocloses') sg.popup_auto_close('Killed everything....', 'This window autocloses')
sys.exit() sys.exit()
# ---------------- Create Form ---------------- # ---------------- Create Form ----------------
sg.theme('Dark Grey 9') sg.theme('Dark Grey 9')
sg.set_options(icon=icon)
window = make_window() window = make_window()
display_list = show_list_by_name() current_display_list = display_list = show_list_by_name(window['-PYTHON ONLY-'].get())
window['-processes-'].update(display_list) window['-PROCESSES-'].update(display_list)
name_sorted = True name_sorted = True
# ---------------- main loop ---------------- # ---------------- main loop ----------------
while True: while True:
# --------- Read and update window -------- # --------- Read and update window --------
@ -152,13 +160,13 @@ def main(silent=False):
# --------- Do Button Operations -------- # --------- Do Button Operations --------
if event == 'Sort by Name': 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 name_sorted = True
elif event.startswith('Kill'): elif event.startswith('Kill'):
if event.startswith('Kill All'): if event.startswith('Kill All'):
processes_to_kill = show_list_by_name() processes_to_kill = show_list_by_name(values['-PYTHON ONLY-'])
else: else:
processes_to_kill = values['-processes-'] processes_to_kill = values['-PROCESSES-']
for proc in processes_to_kill: for proc in processes_to_kill:
pid = int(proc[0:5]) pid = int(proc[0:5])
try: try:
@ -167,23 +175,42 @@ def main(silent=False):
except Exception as e: except Exception as e:
if event.endswith('Selected'): # only show the error if trying to kill only 1 process 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) 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'): if event.endswith('Exit'):
break break
elif event == 'Sort by % CPU': 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 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: if display_list is not None:
new_output = [] new_output = []
for line in display_list: for line in display_list:
if values['-filter-'] in line.lower(): if values['-FILTER-'] in line.lower():
new_output.append(line) new_output.append(line)
window['-processes-'].update(new_output) window['-PROCESSES-'].update(new_output)
elif event == 'Edit Me':
sg.execute_editor(__file__)
window.close() window.close()
if __name__ == "__main__": if __name__ == "__main__":
icon = b'iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAFLklEQVR4nO2ZW2xURRjHfzNnz9meXnYplHKtIhKEAgoEabUgKARBQTQ8+mLi5dXERIJPhMREownhyQfii8+aKKCCRIPRQiGEi3IRuSXc5dbSbbvdc5vxYUvAsmfP6bKtmuwvOS87s9/3/We++WbOHKhQoUKFChVKR4Q1fP3T3k1Jy5rluu5IxhOKZVk4rnvitWVtGwu1J4r8d0WyqqpVyFCtI4plJXFcdxIwVCHacZwcrutiGgkMwwCthynMEIQgCAK8wEdrDWgnrGuxGcl3MAz2nDzM2WuXQBpljTMSFTBtQhNLm+dFdo0WIg32nztJ+5H9YJoxnOv8yhNlSEnPY9G8VpbPXoCKijOOPdNIgGlFC9EaWVuNdj2065VBjMj7joF8SE/30BohJaPfWEl1SzP4QdlMx6F8QrwAu2Um1S3NpFa1IOvrQEUlRPkoj5BAYTSkSa9dBFpjN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kXGNdSKQ8sEU0ppxjXXDMnglG/1gy2ezlTJbpSFNNei9WgjRiNYbC80IQmzSWt+4/2cpJSpQnonu+PC9d06UEk9JQtZv3jpLCHYnq+yJd0+4g+MtttgH67trw8nlrkgpV3z07psnhxpTSaklpFhRU5ua6DoOhFwvJ0LuibXWA58IHrBKTV1qUl9v5kVgZIRIoS/5nrvT90I/V5SElAIpdGhlqlChQoUK/zn+BhjXF7IsC7cbAAAAAElFTkSuQmCC'
if len(sys.argv) == 2 and sys.argv[1] == 'silent': if len(sys.argv) == 2 and sys.argv[1] == 'silent':
main(silent=True) main(silent=True)
else: else: