From f1e0c7d03fe7d61dbb601b4a1f2af37ef875f839 Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Tue, 22 Feb 2022 05:20:03 -0500 Subject: [PATCH] More "Demo Program Catchup" updates. Working through them bit by bit..... --- DemoPrograms/Demo_Columns.py | 10 +++-- .../Demo_Design_Pattern_Multiple_Windows.py | 44 +++++++++++-------- .../Demo_Design_Pattern_Persistent_Window.py | 18 +++++--- DemoPrograms/Demo_Input_Validation.py | 11 +++-- DemoPrograms/Demo_OpenCV_4_Line_Program.py | 7 ++- DemoPrograms/Demo_OpenCV_7_Line_Program.py | 20 ++++++--- DemoPrograms/Demo_Progress_Meter_Simulated.py | 4 +- DemoPrograms/Demo_Sudoku_1_Line.py | 7 +-- DemoPrograms/Demo_System_Tray_Icon.py | 23 +++++++--- 9 files changed, 87 insertions(+), 57 deletions(-) diff --git a/DemoPrograms/Demo_Columns.py b/DemoPrograms/Demo_Columns.py index eb657435..58055507 100644 --- a/DemoPrograms/Demo_Columns.py +++ b/DemoPrograms/Demo_Columns.py @@ -1,8 +1,12 @@ #!/usr/bin/env python import PySimpleGUI as sg -print(sg.version, sg) + ''' Usage of Column Element + + How to embed a layout in a layout + + Copyright 2022 PySimpleGUI ''' sg.theme('BlueMono') @@ -14,12 +18,12 @@ col = [[sg.Text('col Row 1', text_color='white', background_color='blue')], # Window layout layout = [[sg.Listbox(values=('Listbox Item 1', 'Listbox Item 2', 'Listbox Item 3'), select_mode=sg.LISTBOX_SELECT_MODE_MULTIPLE, size=(20, 3)), - sg.Col(col, background_color='blue')], + sg.Column(col, background_color='blue')], [sg.Input('Last input')], [sg.OK()]] # Display the window and get values -window = sg.Window('Compact 1-line form with column', layout, margins=(0,0), element_padding=(0,0)) +window = sg.Window('Column Element', layout, margins=(0,0), element_padding=(0,0)) event, values = window.read() sg.popup(event, values, line_width=200) diff --git a/DemoPrograms/Demo_Design_Pattern_Multiple_Windows.py b/DemoPrograms/Demo_Design_Pattern_Multiple_Windows.py index 6c8e9def..d6ed019a 100644 --- a/DemoPrograms/Demo_Design_Pattern_Multiple_Windows.py +++ b/DemoPrograms/Demo_Design_Pattern_Multiple_Windows.py @@ -24,23 +24,29 @@ def make_win2(): [sg.Button('Erase'), sg.Button('Popup'), sg.Button('Exit')]] return sg.Window('Second Window', layout, finalize=True) -window1, window2 = make_win1(), None # start off with 1 window open -while True: # Event Loop - window, event, values = sg.read_all_windows() - if event == sg.WIN_CLOSED or event == 'Exit': - window.close() - if window == window2: # if closing win 2, mark as closed - window2 = None - elif window == window1: # if closing win 1, exit program - break - elif event == 'Popup': - sg.popup('This is a BLOCKING popup','all windows remain inactive while popup active') - elif event == 'Launch 2nd Window' and not window2: - window2 = make_win2() - elif event == '-IN-': - window['-OUTPUT-'].update(f'You enetered {values["-IN-"]}') - elif event == 'Erase': - window['-OUTPUT-'].update('') - window['-IN-'].update('') -window.close() \ No newline at end of file + +def main(): + window1, window2 = make_win1(), None # start off with 1 window open + + while True: # Event Loop + window, event, values = sg.read_all_windows() + if event == sg.WIN_CLOSED or event == 'Exit': + window.close() + if window == window2: # if closing win 2, mark as closed + window2 = None + elif window == window1: # if closing win 1, exit program + break + elif event == 'Popup': + sg.popup('This is a BLOCKING popup','all windows remain inactive while popup active') + elif event == 'Launch 2nd Window' and not window2: + window2 = make_win2() + elif event == '-IN-': + window['-OUTPUT-'].update(f'You enetered {values["-IN-"]}') + elif event == 'Erase': + window['-OUTPUT-'].update('') + window['-IN-'].update('') + window.close() + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/DemoPrograms/Demo_Design_Pattern_Persistent_Window.py b/DemoPrograms/Demo_Design_Pattern_Persistent_Window.py index 1ac36088..8799315b 100644 --- a/DemoPrograms/Demo_Design_Pattern_Persistent_Window.py +++ b/DemoPrograms/Demo_Design_Pattern_Persistent_Window.py @@ -1,18 +1,22 @@ import PySimpleGUI as sg -layout = [ - [sg.Text('Your typed chars appear here:'), - sg.Text(size=(20, 1), key='-OUTPUT-')], - [sg.Input('', key='-IN-')], - [sg.Button('Show'), sg.Button('Exit')] -] +""" + The basic PySimpleGUI design pattern for a persistent window that is + updated using data input from one of the elements. + + Copyright 2020 PySimpleGUI.org +""" + +layout = [[sg.Text('Your typed chars appear here:'), sg.Text(size=(20, 1), key='-OUTPUT-')], + [sg.Input(key='-IN-')], + [sg.Button('Show'), sg.Button('Exit')]] window = sg.Window('Window Title', layout) while True: event, values = window.read() print(event, values) - if event in (sg.WIN_CLOSED, 'Exit'): + if event == sg.WIN_CLOSED or event == 'Exit': break if event == 'Show': # change the "output" element to be the value of "input" element diff --git a/DemoPrograms/Demo_Input_Validation.py b/DemoPrograms/Demo_Input_Validation.py index 35ba8926..39c657ea 100644 --- a/DemoPrograms/Demo_Input_Validation.py +++ b/DemoPrograms/Demo_Input_Validation.py @@ -2,8 +2,10 @@ import PySimpleGUI as sg """ Simple field validation - Input field should only accept digits. + Input field should only accept digits 0-9. If non-digit entered, it is deleted from the field + + Copyright 2022 PySimpleGUI """ layout = [[sg.Text('Enter digits:')], @@ -14,11 +16,12 @@ window = sg.Window('Window Title', layout) while True: # Event Loop event, values = window.read() - if event in (sg.WIN_CLOSED, 'Exit'): + print(event, values) + if event == sg.WIN_CLOSED or event == 'Exit': break # if last char entered not a digit - if len(values['-INPUT-']) and values['-INPUT-'][-1] not in ('0123456789'): + if event == '-INPUT-' and len(values['-INPUT-']) and values['-INPUT-'][-1] not in ('0123456789'): # delete last char from input window['-INPUT-'].update(values['-INPUT-'][:-1]) -window.close() +window.close() \ No newline at end of file diff --git a/DemoPrograms/Demo_OpenCV_4_Line_Program.py b/DemoPrograms/Demo_OpenCV_4_Line_Program.py index 96f715bb..cc067c71 100644 --- a/DemoPrograms/Demo_OpenCV_4_Line_Program.py +++ b/DemoPrograms/Demo_OpenCV_4_Line_Program.py @@ -1,4 +1,7 @@ import cv2, PySimpleGUI as sg -window, cap = sg.Window('Demo Application - OpenCV Integration', [[sg.Image(key='-IMAGE-')], ], location=(800, 400)), cv2.VideoCapture(0) +# Make the window +window, cap = sg.Window('Demo Application - OpenCV Integration', [[sg.Image(filename='', key='image')], ], location=(800, 400)), cv2.VideoCapture(0) +# Loop reading video frames while window(timeout=20)[0] is not None: - window['-IMAGE-'](data=cv2.imencode('.png', cap.read()[1])[1].tobytes()) + # Read a video frame and write it to the window + window['image'](data=cv2.imencode('.png', cap.read()[1])[1].tobytes()) \ No newline at end of file diff --git a/DemoPrograms/Demo_OpenCV_7_Line_Program.py b/DemoPrograms/Demo_OpenCV_7_Line_Program.py index 765c8fe6..8b7f022f 100644 --- a/DemoPrograms/Demo_OpenCV_7_Line_Program.py +++ b/DemoPrograms/Demo_OpenCV_7_Line_Program.py @@ -1,14 +1,20 @@ import cv2, PySimpleGUI as sg -window = sg.Window('Demo Application - OpenCV Integration', [[sg.Image(filename='', key='image')], ], location=(800, 400)) +window = sg.Window('Demo Application - OpenCV Integration', [[sg.Image(key='-I-')], ], location=(800, 400)) cap = cv2.VideoCapture(0) # Setup the camera as a capture device while True: # The PSG "Event Loop" - event, values = window.read(timeout=20, timeout_key='timeout') # get events for the window with 20ms max wait + event, values = window.read(timeout=20) # get events for the window with 20ms max wait if event == sg.WIN_CLOSED: break # if user closed window, quit - window['image'].update(data=cv2.imencode('.png', cap.read()[1])[1].tobytes()) # Update image in window + window['-I-'].update(data=cv2.imencode('.ppm', cap.read()[1])[1].tobytes()) # Update image in window """ -Putting the comment at the bottom so that you can see that the code is indeed 7 lines long. And, there is nothing -done out of the ordinary to make it 7 lines long. There are no ; for example. OK, so the if statement is on one line -but that's the only place that you would traditionally see one more line. So, call it 8 if you want. -""" + Putting the comment at the bottom so that you can see that the code is indeed 7 lines long. And, there is nothing + done out of the ordinary to make it 7 lines long. There are no ; for example. OK, so the if statement is on one line + but that's the only place that you would traditionally see one more line. So, call it 8 if you want. + + NOTE - the encoding format PPM has been shown to be significantly less CPU intensive than using PNG (thank you reporting PySimpleGUI user!) + + In some cases however, PPM may not be supported. If you have problems with PPM encoding, then change ".ppm" to ".png" on line 8. + + Copyright 2022 PySimpleGUI +""" \ No newline at end of file diff --git a/DemoPrograms/Demo_Progress_Meter_Simulated.py b/DemoPrograms/Demo_Progress_Meter_Simulated.py index 00fc5dfd..17ca094f 100644 --- a/DemoPrograms/Demo_Progress_Meter_Simulated.py +++ b/DemoPrograms/Demo_Progress_Meter_Simulated.py @@ -13,7 +13,7 @@ import PySimpleGUI as sg sg.theme('DarkBlue') -layout = [[sg.Text('', size=(50, 1), relief='sunken', font=('Courier', 11), +layout = [[sg.Text('', size=(50, 1), relief='sunken', text_color='yellow', background_color='black',key='-TEXT-', metadata=0)]] window = sg.Window('Title', layout, finalize=True) @@ -27,6 +27,6 @@ while True: if event == sg.WINDOW_CLOSED: break text.metadata = (text.metadata + 1) % 51 - text.update('█' * text.metadata) + text.update(sg.SYMBOL_SQUARE * text.metadata) window.close() \ No newline at end of file diff --git a/DemoPrograms/Demo_Sudoku_1_Line.py b/DemoPrograms/Demo_Sudoku_1_Line.py index a66bd4f3..093765df 100644 --- a/DemoPrograms/Demo_Sudoku_1_Line.py +++ b/DemoPrograms/Demo_Sudoku_1_Line.py @@ -10,9 +10,4 @@ import PySimpleGUI as sg Copyright 2021 PySimpleGUI """ -sg.Window('Sudoku', [[sg.Frame('', [[sg.Input(justification='r', size=(3,1)) - for col in range(3)] - for row in range(3)]) - for frame_col in range(3)] - for frame_row in range(3)], - use_custom_titlebar=True).read() +sg.Window('Sudoku', [[sg.Frame('', [[sg.Input(justification='r', size=(3,1)) for col in range(3)] for row in range(3)]) for frame_col in range(3)] for frame_row in range(3)], use_custom_titlebar=True).read() diff --git a/DemoPrograms/Demo_System_Tray_Icon.py b/DemoPrograms/Demo_System_Tray_Icon.py index c01723e6..1ff8a4c4 100644 --- a/DemoPrograms/Demo_System_Tray_Icon.py +++ b/DemoPrograms/Demo_System_Tray_Icon.py @@ -1,29 +1,38 @@ -# import PySimpleGUI as sg -import PySimpleGUIWx as sg +import PySimpleGUI as sg +# import PySimpleGUIWx as sg # import PySimpleGUIQt as sg - """ System Tray Icon - Your very own personsal status monitor in your system tray + Your very own peronsal status monitor in your system tray Super easy to use. 1. Find an icon file or use this default - 2. Create your menu definition + 2. Create your menu defintion 3. Add if statements to take action based on your input Note from the imports that this code works on all PySimpleGUI ports (except Web). For the tkinter port, however, the icon isn't located in the system tray. Instead it's located just above the system tray in the form of what looks like an "icon" on your desktop. It's actually a very small window. + + In June 2021 a new package was added to the PySimpleGUI family - psgtray + With this package, you can get a real system tray icon while using tkinter rather than a desktop-icon that this + demo is demonstrating. The Demo Program - Demo_psgtray_Tray_Icon_Tkinter - shows how to integrate it with PySimpleGUI + + Copyright 2022 PySimpleGUI """ -menu_def = ['UNUSED', ['My', 'Simple', '---', 'Menu', 'Exit']] + + +menu_def = ['UNUSED', ['My', '&Simple::my key', 'Has Sub', ['1','2'], '---', 'Menu', 'E&xit']] tray = sg.SystemTray(menu=menu_def, data_base64=sg.DEFAULT_BASE64_ICON) while True: event = tray.read() + print(event) if event == 'Exit': break elif event == 'Menu': - tray.show_message('Title', 'Hey, you clicked Menu!') \ No newline at end of file + tray.show_message('Title', 'Hey, you clicked Menu!') +tray.close() \ No newline at end of file