From 57396f3dc116993d8462b0c501883ec833817cb7 Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Wed, 12 Sep 2018 16:46:35 -0400 Subject: [PATCH] NEW Demos - Floating CPU utlization widget, Compound element - Spinner, refresh of the other Demos --- Demo_Cookbook_Browser.py | 2 +- Demo_Desktop_Widget_CPU_Utilization.py | 49 ++++++++++++++++++++++++++ Demo_Disable_Elements.py | 14 ++++++++ Demo_Fill_Form.py | 2 +- Demo_OpenCV.py | 2 +- Demo_Pi_LEDs.py | 21 +++++------ Demo_Spinner_Compound_Element.py | 34 ++++++++++++++++++ Demo_Table_Simulation.py | 2 +- 8 files changed, 109 insertions(+), 17 deletions(-) create mode 100644 Demo_Desktop_Widget_CPU_Utilization.py create mode 100644 Demo_Spinner_Compound_Element.py diff --git a/Demo_Cookbook_Browser.py b/Demo_Cookbook_Browser.py index 2b378c36..f188253d 100644 --- a/Demo_Cookbook_Browser.py +++ b/Demo_Cookbook_Browser.py @@ -682,7 +682,7 @@ def InputElementUpdate(): keys_entered += button # add the new digit elif button is 'Submit': keys_entered = values['input'] - form.FindElement('outpput').Update(keys_entered) # output the final string + form.FindElement('output').Update(keys_entered) # output the final string form.FindElement('input').Update(keys_entered) # change the form to reflect current key string diff --git a/Demo_Desktop_Widget_CPU_Utilization.py b/Demo_Desktop_Widget_CPU_Utilization.py new file mode 100644 index 00000000..9ba08952 --- /dev/null +++ b/Demo_Desktop_Widget_CPU_Utilization.py @@ -0,0 +1,49 @@ +import PySimpleGUI as sg +import time +import psutil + +""" + PSUTIL Desktop Widget + Creates a floating CPU utilization window that is always on top of other windows + You move it by grabbing anywhere on the window + Good example of how to do a non-blocking, polling program using PySimpleGUI + Use the spinner to adjust the number of seconds between readings of the CPU utilizaiton + + NOTE - you will get a warning message printed when you exit using exit button. + It will look something like: + invalid command name "1616802625480StopMove" +""" + +# ---------------- 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('Running Timer', 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}%') + + +# --------- After loop -------- + +# Broke out of main loop. Close the window. +form.CloseNonBlockingForm() diff --git a/Demo_Disable_Elements.py b/Demo_Disable_Elements.py index 9580d263..4741c4b0 100644 --- a/Demo_Disable_Elements.py +++ b/Demo_Disable_Elements.py @@ -26,6 +26,20 @@ form = sg.FlexForm("Time Tracker", default_element_size=(12, 1), text_justificat form.Layout(layout) +form.ReadNonBlocking() + +form.FindElement('cbox').Update(disabled=True) +form.FindElement('listbox').Update(disabled=True) +form.FindElement('radio1').Update(disabled=True) +form.FindElement('radio2').Update(disabled=True) +form.FindElement('spin').Update(disabled=True) +form.FindElement('option').Update(disabled=True) +form.FindElement('combo').Update(disabled=True) +form.FindElement('reset').Update(disabled=True) +form.FindElement('notes').Update(disabled=True) +form.FindElement('multi').Update(disabled=True) +form.FindElement('slider').Update(disabled=True) + while True: button, values = form.Read() if button is None or button == 'Exit': diff --git a/Demo_Fill_Form.py b/Demo_Fill_Form.py index d6194883..aaa0ae18 100644 --- a/Demo_Fill_Form.py +++ b/Demo_Fill_Form.py @@ -35,7 +35,7 @@ def Everything(): sg.Text(' ' * 40), sg.ReadFormButton('SaveSettings'), sg.ReadFormButton('LoadSettings')] ] - form = sg.FlexForm('Form Fill Demonstration', default_element_size=(40, 1)) + form = sg.FlexForm('Form Fill Demonstration', default_element_size=(40, 1), grab_anywhere=False) # button, values = form.LayoutAndRead(layout, non_blocking=True) form.Layout(layout) diff --git a/Demo_OpenCV.py b/Demo_OpenCV.py index 67acba93..2c8feb7b 100644 --- a/Demo_OpenCV.py +++ b/Demo_OpenCV.py @@ -32,7 +32,7 @@ def main(): [sg.ReadFormButton('Exit', size=(10, 2), pad=((600, 0), 3), font='Helvetica 14')]] # create the form and show it without the plot - form = sg.FlexForm('Demo Application - Embedding Matplotlib In PySimpleGUI', no_titlebar=True, location=(0,0)) + form = sg.FlexForm('Demo Application - OpenCV Integration', no_titlebar=False, location=(0,0)) form.Layout(layout) form.ReadNonBlocking() diff --git a/Demo_Pi_LEDs.py b/Demo_Pi_LEDs.py index 6fb60ca0..ec89ab11 100644 --- a/Demo_Pi_LEDs.py +++ b/Demo_Pi_LEDs.py @@ -12,8 +12,8 @@ GPIO.setup(14, GPIO.OUT) def SwitchLED(): varLEDStatus = GPIO.input(14) - - if varLEDStatus == 0: + varLedStatus = 0 + if varLedStatus == 0: GPIO.output(14, GPIO.HIGH) return "LED is switched ON" else: @@ -28,32 +28,27 @@ def FlashLED(): GPIO.output(14, GPIO.LOW) time.sleep(0.5) -results_elem = rg.T('', size=(30, 1)) - layout = [[rg.T('Raspberry Pi LEDs')], - [results_elem], + [rg.T('', size=(14, 1), key='output')], [rg.ReadFormButton('Switch LED')], [rg.ReadFormButton('Flash LED')], - [rg.ReadFormButton('Show slider value')], - [rg.Slider(range=(0, 100), default_value=0, orientation='h', size=(40, 20), key='slider')], [rg.Exit()] ] -form = rg.FlexForm('Raspberry Pi GUI') +form = rg.FlexForm('Raspberry Pi GUI', grab_anywhere=False) form.Layout(layout) while True: button, values = form.Read() if button is None: break + if button is 'Switch LED': - results_elem.Update(SwitchLED()) + form.FindElement('output').Update(SwitchLED()) elif button is 'Flash LED': - results_elem.Update('LED is Flashing') + form.FindElement('output').Update('LED is Flashing') form.ReadNonBlocking() FlashLED() - results_elem.Update('') - elif button is 'Show slider value': - results_elem.Update('Slider = %s'%values['slider']) + form.FindElement('output').Update('') rg.MsgBox('Done... exiting') diff --git a/Demo_Spinner_Compound_Element.py b/Demo_Spinner_Compound_Element.py new file mode 100644 index 00000000..ec13d240 --- /dev/null +++ b/Demo_Spinner_Compound_Element.py @@ -0,0 +1,34 @@ +import PySimpleGUI as sg + +""" + Demo of how to combine elements into your own custom element +""" + +sg.SetOptions(element_padding=(0,0)) +sg.ChangeLookAndFeel('Dark') +# --- Define our "Big-Button-Spinner" compound element. Has 2 buttons and an input field --- # +NewSpinner = [sg.ReadFormButton('-', size=(4,1), font='Any 12'), + sg.In('0', size=(5,1), font='Any 14', justification='r', key='spin'), + sg.ReadFormButton('+', size=(4,1), font='Any 12')] +# --- Define Window --- # +layout = [ + [sg.Text('Spinner simulation')], + NewSpinner, + [sg.T('')], + [sg.Ok()] + ] + +form = sg.FlexForm('Spinner simulation') +form.Layout(layout) + +# --- Event Loop --- # +counter = 0 +while True: + button, value = form.Read() + + if button == 'Ok' or button is None: # be nice to your user, always have an exit from your form + break + + # --- do spinner stuff --- # + counter += 1 if button =='+' else -1 if button == '-' else 0 + form.FindElement('spin').Update(counter) diff --git a/Demo_Table_Simulation.py b/Demo_Table_Simulation.py index b8d9ed0b..14860b3b 100644 --- a/Demo_Table_Simulation.py +++ b/Demo_Table_Simulation.py @@ -25,7 +25,7 @@ def TableSimulation(): sg.In(key='inputrow', justification='right', size=(8,1), pad=(1,1), do_not_clear=True), sg.In(key='inputcol', size=(8,1), pad=(1,1), justification='right', do_not_clear=True), sg.In(key='value', size=(8,1), pad=(1,1), justification='right', do_not_clear=True)], - [sg.Column(columm_layout, size=(815,600), scrollable=True)]] + [sg.Column(columm_layout, size=(800,600), scrollable=True)]] form = sg.FlexForm('Table', return_keyboard_events=True, grab_anywhere=False) form.Layout(layout)