MCPcopy
hub / github.com/PySimpleGUI/PySimpleGUI / main

Function main

DemoPrograms/Demo_Desktop_Widget_CPU_Gauge.py:270–305  ·  view source on GitHub ↗
(location)

Source from the content-addressed store, hash-verified

268 return str(bytes) + ' ' + units[0] if bytes < 1024 else human_size(bytes >> 10, units[1:])
269
270def main(location):
271 sg.theme(THEME)
272 gsize = (100, 55)
273 layout = [
274 [sg.T('CPU', font='Any 20', background_color='black')],
275 [sg.Graph(gsize, (-gsize[0] // 2, 0), (gsize[0] // 2, gsize[1]), key='-Graph-')],
276 [sg.T(size=(5, 1), font='Any 20', justification='c', background_color='black', k='-gauge VALUE-')]]
277
278
279 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, background_color='black', element_justification='c', finalize=True, right_click_menu=sg.MENU_RIGHT_CLICK_EDITME_VER_EXIT, enable_close_attempted_event=True)
280
281 gauge = Gauge(pointer_color=sg.theme_text_color(), clock_color=sg.theme_text_color(), major_tick_color=sg.theme_text_color(),
282 minor_tick_color=sg.theme_input_background_color(), pointer_outer_color=sg.theme_text_color(), major_tick_start_radius=45,
283 minor_tick_start_radius=45, minor_tick_stop_radius=50, major_tick_stop_radius=50, major_tick_step=30, clock_radius=50, pointer_line_width=3, pointer_inner_radius=10, pointer_outer_radius=50, graph_elem=window['-Graph-'])
284
285 gauge.change(degree=0)
286
287 while True: # Event Loop
288 cpu_percent = psutil.cpu_percent(interval=1)
289
290 if gauge.change():
291 new_angle = cpu_percent*180/100
292 window['-gauge VALUE-'].update(f'{int(cpu_percent)}%')
293 gauge.change(degree=new_angle, step=180)
294 gauge.change()
295 # ----------- update the graphics and text in the window ------------
296 # update the window, wait for a while, then check for exit
297 event, values = window.read(timeout=UPDATE_FREQUENCY_MILLISECONDS)
298 if event in (sg.WIN_CLOSE_ATTEMPTED_EVENT, 'Exit'):
299 sg.user_settings_set_entry('-location-', window.current_location()) # The line of code to save the position before exiting
300 break
301 if event == 'Edit Me':
302 sg.execute_editor(__file__)
303 elif event == 'Version':
304 sg.popup_scrolled(__file__, sg.get_versions(), location=window.current_location(), keep_on_top=True, non_blocking=True)
305 window.close()
306
307
308if __name__ == '__main__':

Calls 6

changeMethod · 0.95
readMethod · 0.95
current_locationMethod · 0.95
closeMethod · 0.95
GaugeClass · 0.70
updateMethod · 0.45

Tested by

no test coverage detected