diff --git a/Demo_Desktop_Widget_CPU_Utilization.py b/Demo_Desktop_Widget_CPU_Utilization.py index da675f6c..69178101 100644 --- a/Demo_Desktop_Widget_CPU_Utilization.py +++ b/Demo_Desktop_Widget_CPU_Utilization.py @@ -37,8 +37,6 @@ def CPU_thread(args): def main(): global g_interval, g_procs, g_exit - yesno = sg.PopupYesNo('My popup') - # ---------------- Create Form ---------------- sg.ChangeLookAndFeel('Black') form_rows = [[sg.Text('', size=(8,1), font=('Helvetica', 20),text_color=sg.YELLOWS[0], justification='center', key='text')], diff --git a/Demo_Desktop_Widget_CPU_Utilization_Simple.py b/Demo_Desktop_Widget_CPU_Utilization_Simple.py new file mode 100644 index 00000000..17583aab --- /dev/null +++ b/Demo_Desktop_Widget_CPU_Utilization_Simple.py @@ -0,0 +1,34 @@ +import PySimpleGUI as sg +import psutil + +# ---------------- Create Form ---------------- +sg.ChangeLookAndFeel('Black') +form_rows = [[sg.Text('')], + [sg.Text('', size=(8, 2), font=('Helvetica', 20), justification='center', key='text')], + [sg.Exit(button_color=('white', 'firebrick4'), pad=((15, 0), 0)), + sg.Spin([x + 1 for x in range(10)], 1, key='spin')]] +# Layout the rows of the form and perform a read. Indicate the form is non-blocking! +form = sg.FlexForm('CPU Meter', no_titlebar=True, auto_size_buttons=False, keep_on_top=True, grab_anywhere=True) +form.Layout(form_rows) + +# ---------------- main loop ---------------- +while (True): + # --------- Read and update window -------- + button, values = form.ReadNonBlocking() + + # --------- Do Button Operations -------- + if values is None or button == 'Exit': + break + try: + interval = int(values['spin']) + except: + interval = 1 + + cpu_percent = psutil.cpu_percent(interval=interval) + + # --------- Display timer in window -------- + + form.FindElement('text').Update(f'CPU {cpu_percent:02.0f}%') + +# Broke out of main loop. Close the window. +form.CloseNonBlockingForm() \ No newline at end of file