| 35 | |
| 36 | |
| 37 | def main(): |
| 38 | global g_exit, g_response_time |
| 39 | # start ping measurement thread |
| 40 | |
| 41 | sg.theme('Black') |
| 42 | sg.set_options(element_padding=(0, 0)) |
| 43 | |
| 44 | layout = [ |
| 45 | [sg.Quit(button_color=('white', 'black')), |
| 46 | sg.Text('', pad=((100, 0), 0), font='Any 15', key='output')], |
| 47 | [sg.Graph(CANVAS_SIZE, (0, 0), (SAMPLES, SAMPLE_MAX), |
| 48 | background_color='black', key='graph')] |
| 49 | ] |
| 50 | |
| 51 | window = sg.Window('CPU Graph', layout, |
| 52 | grab_anywhere=True, keep_on_top=True, |
| 53 | background_color='black', no_titlebar=True, |
| 54 | use_default_focus=False) |
| 55 | |
| 56 | graph = window['graph'] |
| 57 | output = window['output'] |
| 58 | # start cpu measurement thread |
| 59 | thread = Thread(target=CPU_thread, args=(None,)) |
| 60 | thread.start() |
| 61 | |
| 62 | last_cpu = i = 0 |
| 63 | prev_x, prev_y = 0, 0 |
| 64 | while True: # the Event Loop |
| 65 | event, values = window.read(timeout=500) |
| 66 | if event in ('Quit', None): # always give ths user a way out |
| 67 | break |
| 68 | # do CPU measurement and graph it |
| 69 | current_cpu = int(g_cpu_percent*10) |
| 70 | |
| 71 | if current_cpu == last_cpu: |
| 72 | continue |
| 73 | # show current cpu usage at top |
| 74 | output.update(current_cpu/10) |
| 75 | |
| 76 | if current_cpu > SAMPLE_MAX: |
| 77 | current_cpu = SAMPLE_MAX |
| 78 | new_x, new_y = i, current_cpu |
| 79 | |
| 80 | if i >= SAMPLES: |
| 81 | # shift graph over if full of data |
| 82 | graph.move(-STEP_SIZE, 0) |
| 83 | prev_x = prev_x - STEP_SIZE |
| 84 | graph.draw_line((prev_x, prev_y), (new_x, new_y), color='white') |
| 85 | prev_x, prev_y = new_x, new_y |
| 86 | i += STEP_SIZE if i < SAMPLES else 0 |
| 87 | last_cpu = current_cpu |
| 88 | |
| 89 | g_exit = True |
| 90 | window.close() |
| 91 | |
| 92 | |
| 93 | if __name__ == '__main__': |