(location)
| 21 | |
| 22 | |
| 23 | def main(location): |
| 24 | graph = sg.Graph(GSIZE, (0, 0), GSIZE, key='-GRAPH-') |
| 25 | |
| 26 | layout = [[graph]] |
| 27 | |
| 28 | window = sg.Window('CPU Usage Widget Square', layout, location=location, no_titlebar=True, grab_anywhere=True, margins=(0, 0), element_padding=(0, 0), alpha_channel=ALPHA, finalize=True, right_click_menu=sg.MENU_RIGHT_CLICK_EDITME_VER_EXIT, enable_close_attempted_event=True) |
| 29 | |
| 30 | text_id2 = graph.draw_text(f'CPU', (GSIZE[0] // 2, GSIZE[1] // 4), font='Any 20', text_location=sg.TEXT_LOCATION_CENTER, color=sg.theme_button_color()[0]) |
| 31 | |
| 32 | |
| 33 | while True: # Event Loop |
| 34 | # ----------- update the graphics and text in the window ------------ |
| 35 | cpu_percent = psutil.cpu_percent(interval=1) |
| 36 | # Draw the filled rectangle |
| 37 | rect_height = int(GSIZE[1] * float(cpu_percent) / 100) |
| 38 | rect_id = graph.draw_rectangle((0, rect_height), (GSIZE[0], 0), fill_color=sg.theme_button_color()[1], line_width=0) |
| 39 | # Draw the % used text and the close "X" on bottom |
| 40 | text_id1 = graph.draw_text(f'{int(cpu_percent)}%', (GSIZE[0] // 2, GSIZE[1] // 2), font='Any 40', text_location=sg.TEXT_LOCATION_CENTER, color=sg.theme_button_color()[0]) |
| 41 | # put the bar behind everything else |
| 42 | graph.send_figure_to_back(rect_id) |
| 43 | |
| 44 | # update the window, wait for a while, then check for exit |
| 45 | event, values = window.read(timeout=UPDATE_FREQUENCY_MILLISECONDS) |
| 46 | if event in (sg.WIN_CLOSE_ATTEMPTED_EVENT, 'Exit'): |
| 47 | sg.user_settings_set_entry('-location-', window.current_location()) # The line of code to save the position before exiting |
| 48 | break |
| 49 | if event == 'Edit Me': |
| 50 | sg.execute_editor(__file__) |
| 51 | elif event == 'Version': |
| 52 | sg.popup_scrolled(__file__, sg.get_versions(), location=window.current_location(), keep_on_top=True, non_blocking=True) |
| 53 | # erase figures so they can be redrawn |
| 54 | graph.delete_figure(rect_id) |
| 55 | graph.delete_figure(text_id1) |
| 56 | window.close() |
| 57 | |
| 58 | |
| 59 | if __name__ == '__main__': |
no test coverage detected