()
| 29 | |
| 30 | |
| 31 | def main(): |
| 32 | global g_interval |
| 33 | |
| 34 | location = sg.user_settings_get_entry('-location-', (None, None)) |
| 35 | |
| 36 | # ---------------- Create Form ---------------- |
| 37 | sg.theme('Black') |
| 38 | |
| 39 | layout = [[sg.Text(font=('Helvetica', 20), text_color=sg.YELLOWS[0], key='-CPU PERCENT-')], |
| 40 | [sg.Text(size=(35, 12), font=('Courier New', 12), key='-PROCESSES-')], # size will determine how many processes shown |
| 41 | [sg.Text('Update every '), sg.Spin([x+1 for x in range(10)], 3, key='-SPIN-'), sg.T('seconds')]] |
| 42 | |
| 43 | window = sg.Window('Top CPU Processes', layout, no_titlebar=True, keep_on_top=True,location=location, use_default_focus=False, alpha_channel=.8, grab_anywhere=True, right_click_menu=sg.MENU_RIGHT_CLICK_EDITME_VER_EXIT, enable_close_attempted_event=True) |
| 44 | |
| 45 | # start cpu measurement thread |
| 46 | # using the PySimpleGUI call to start and manage the thread |
| 47 | window.start_thread(lambda: CPU_thread(window), '-THREAD FINISHED-') |
| 48 | g_interval = 1 |
| 49 | # Unusual construct of a Try around entire event loop... something is crashing, we need to find out what... |
| 50 | try: |
| 51 | # ---------------- main loop ---------------- |
| 52 | while True: |
| 53 | # --------- Read and update window -------- |
| 54 | event, values = window.read() |
| 55 | # print(event, values) |
| 56 | # --------- Do Button Operations -------- |
| 57 | if event in (sg.WIN_CLOSE_ATTEMPTED_EVENT, 'Exit'): |
| 58 | sg.user_settings_set_entry('-location-', window.current_location()) # save window location before exiting |
| 59 | break |
| 60 | if event == 'Edit Me': |
| 61 | sp = sg.execute_editor(__file__) |
| 62 | elif event == 'Version': |
| 63 | sg.popup_scrolled(__file__, sg.get_versions(), keep_on_top=True, location=window.current_location()) |
| 64 | elif event == '-CPU UPDATE FROM THREAD-': # indicates data from the thread has arrived |
| 65 | cpu_percent, procs = values[event] # the thread sends a tuple |
| 66 | if procs: |
| 67 | # --------- Create dictionary of top % CPU processes. Format is name:cpu_percent -------- |
| 68 | top = {} |
| 69 | for proc in procs: |
| 70 | try: |
| 71 | top[proc.name()] = proc.cpu_percent() |
| 72 | except Exception as e: |
| 73 | pass # it's OK to get an exception here because processes come and go... one may have gone... |
| 74 | # sg.Print('*** GOT Exception looping through procs ***', c='white on red', font='_ 18') |
| 75 | # sg.Print('Exception = ', e, 'procs=', procs, 'proc', proc) |
| 76 | |
| 77 | top_sorted = sorted(top.items(), key=operator.itemgetter(1), reverse=True) # reverse sort to get highest CPU usage on top |
| 78 | if top_sorted: |
| 79 | top_sorted.pop(0) # remove the idle process |
| 80 | display_string = '\n'.join([f'{cpu/10:2.2f} {proc:23}' for proc, cpu in top_sorted]) |
| 81 | # --------- Display timer and proceses in window -------- |
| 82 | window['-CPU PERCENT-'].update(f'CPU {cpu_percent}') |
| 83 | window['-PROCESSES-'].update(display_string) |
| 84 | # get the timeout from the spinner |
| 85 | g_interval = int(values['-SPIN-']) |
| 86 | except Exception as e: |
| 87 | sg.Print('*** GOT Exception in event loop ***', c='white on red', font='_ 18') |
| 88 | sg.Print('Exception = ', e, wait=True) # IMPORTANT to add a wait/blocking so that the print pauses execution. Otherwise program continue and exits |
no test coverage detected