Sort by CPU %, fixed scrolling problem... looking really good!

This commit is contained in:
MikeTheWatchGuy 2018-10-07 14:18:52 -04:00
parent 7469a14b1f
commit 0c6361793e
1 changed files with 30 additions and 26 deletions

View File

@ -46,57 +46,61 @@ def main():
[sg.Listbox(values=[' '], size=(50, 30), select_mode=sg.SELECT_MODE_EXTENDED, font=('Courier', 12), key='_processes_')], [sg.Listbox(values=[' '], size=(50, 30), select_mode=sg.SELECT_MODE_EXTENDED, font=('Courier', 12), key='_processes_')],
[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.T('Filter by typing name', font='ANY 14'), sg.In(size=(15,1), font='any 14', key='_filter_')], [sg.T('Filter by typing name', font='ANY 14'), sg.In(size=(15,1), font='any 14', key='_filter_')],
[sg.RButton('Refresh'), [sg.RButton('Sort by Name', ),
sg.RButton('Sort by % CPU', button_color=('white', 'DarkOrange2')),
sg.RButton('Kill', button_color=('white','red'), bind_return_key=True), sg.RButton('Kill', button_color=('white','red'), bind_return_key=True),
sg.Exit(button_color=('white', 'sea green'))]] sg.Exit(button_color=('white', 'sea green'))]]
window = sg.Window('Process Killer', window = sg.Window('Process Killer',
keep_on_top=True, keep_on_top=True,
auto_size_buttons=False, auto_size_buttons=False,
default_button_element_size=(9,1), default_button_element_size=(12,1),
return_keyboard_events=True, return_keyboard_events=True,
grab_anywhere=False).Layout(layout) ).Layout(layout)
proc_list = [] display_list = None
# ---------------- main loop ---------------- # ---------------- main loop ----------------
while (True): while (True):
# --------- Read and update window -------- # --------- Read and update window --------
button, values = window.Read() button, values = window.Read()
print(button)
if 'Mouse' in button or 'Control' in button or 'Shift' in button:
continue
# --------- Do Button Operations -------- # --------- Do Button Operations --------
if values is None or button == 'Exit': if values is None or button == 'Exit':
break break
if button == 'Refresh': if button == 'Sort by Name':
psutil.cpu_percent(interval=1) psutil.cpu_percent(interval=1)
procs = psutil.process_iter() procs = psutil.process_iter()
top = {proc.name(): (proc.cpu_percent(), proc.pid) for proc in procs} all_procs = [[proc.cpu_percent(), proc.name(), proc.pid] for proc in procs]
sorted_by_cpu_procs = sorted(all_procs, key=operator.itemgetter(1), reverse=False)
# cpu_percent = psutil.cpu_percent(interval=interval) # if don't wan to use a task display_list = []
for process in sorted_by_cpu_procs:
# --------- Create list of top % CPU porocesses -------- display_list.append('{:5d} {:5.2f} {}\n'.format(process[2], process[0]/10, process[1]))
window.FindElement('_processes_').Update(display_list)
top_sorted = sorted(top.items(), key=operator.itemgetter(0), reverse=False)
if top_sorted:
top_sorted.pop(0)
proc_list = []
for proc, pair in top_sorted:
cpu, pid = pair
proc_list.append('{:5d} {:5.2f} {}\n'.format(pid, cpu/10, proc))
window.FindElement('_processes_').Update(proc_list)
elif button == 'Kill': elif button == 'Kill':
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])
if sg.PopupYesNo('About to kill {} {}'.format(pid, proc[13:]), keep_on_top=True) == 'Yes': if sg.PopupYesNo('About to kill {} {}'.format(pid, proc[13:]), keep_on_top=True) == 'Yes':
kill_proc_tree(pid=pid) kill_proc_tree(pid=pid)
elif button == 'Sort by % CPU':
psutil.cpu_percent(interval=1)
procs = psutil.process_iter()
all_procs = [[proc.cpu_percent(), proc.name(), proc.pid] for proc in procs]
sorted_by_cpu_procs = sorted(all_procs, key=operator.itemgetter(0), reverse=True)
display_list = []
for process in sorted_by_cpu_procs:
display_list.append('{:5d} {:5.2f} {}\n'.format(process[2], process[0]/10, process[1]))
window.FindElement('_processes_').Update(display_list)
else: else:
new_output = [] if display_list is not None:
for line in proc_list: new_output = []
if values['_filter_'] in line.lower(): for line in display_list:
new_output.append(line) if values['_filter_'] in line.lower():
window.FindElement('_processes_').Update(new_output) new_output.append(line)
window.FindElement('_processes_').Update(new_output)
if __name__ == "__main__": if __name__ == "__main__":