From b62648aa23ff3a5e81d5604a60ea21e86d39243d Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Mon, 29 May 2023 10:19:32 -0400 Subject: [PATCH 1/2] Expanded class wrapper demo to explain it's not a recommended design pattern. When initially released there was no explanation accompanying the code. --- DemoPrograms/Demo_Class_Wrapper.py | 66 +++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/DemoPrograms/Demo_Class_Wrapper.py b/DemoPrograms/Demo_Class_Wrapper.py index 99995ee8..12201b23 100644 --- a/DemoPrograms/Demo_Class_Wrapper.py +++ b/DemoPrograms/Demo_Class_Wrapper.py @@ -4,10 +4,38 @@ import PySimpleGUI as sg Demo - Class wrapper Using a class to encapsulate PySimpleGUI Window creation & event loop + + This is NOT a recommended design pattern. It mimics the object oriented design that many OO-based + GUI frameworks use, but there is no advantage to structuring you code in his manner. It adds + confusion, not clarity. + + The class version is 18 lines of code. The plain version is 11 lines of code. + + Two things about the class wrapper jump out as adding confusion: + 1. Unneccessary fragmentation of the event loop - the button click code is pulled out of the loop entirely + 2. "self" clutters the code without adding value + - Copyright 2022 PySimpleGUI + Copyright 2022, 2023 PySimpleGUI """ +''' + MM'""""'YMM dP + M' .mmm. `M 88 + M MMMMMooM 88 .d8888b. .d8888b. .d8888b. + M MMMMMMMM 88 88' `88 Y8ooooo. Y8ooooo. + M. `MMM' .M 88 88. .88 88 88 + MM. .dM dP `88888P8 `88888P' `88888P' + MMMMMMMMMMM + + M""MMMMM""M oo + M MMMMM M + M MMMMP M .d8888b. 88d888b. .d8888b. dP .d8888b. 88d888b. + M MMMM' .M 88ooood8 88' `88 Y8ooooo. 88 88' `88 88' `88 + M MMP' .MM 88. ... 88 88 88 88. .88 88 88 + M .dMMM `88888P' dP `88888P' dP `88888P' dP dP + MMMMMMMMMMM +''' class SampleGUI(): def __init__(self): @@ -35,3 +63,39 @@ class SampleGUI(): my_gui = SampleGUI() # run the event loop my_gui.run() + + +''' + M"""""""`YM dP + M mmmm. M 88 + M MMMMM M .d8888b. 88d888b. 88d8b.d8b. .d8888b. 88 + M MMMMM M 88' `88 88' `88 88'`88'`88 88' `88 88 + M MMMMM M 88. .88 88 88 88 88 88. .88 88 + M MMMMM M `88888P' dP dP dP dP `88888P8 dP + MMMMMMMMMMM + + M""MMMMM""M oo + M MMMMM M + M MMMMP M .d8888b. 88d888b. .d8888b. dP .d8888b. 88d888b. + M MMMM' .M 88ooood8 88' `88 Y8ooooo. 88 88' `88 88' `88 + M MMP' .MM 88. ... 88 88 88 88. .88 88 88 + M .dMMM `88888P' dP `88888P' dP `88888P' dP dP + MMMMMMMMMMM +''' + +layout = [ [sg.Text('My layout')], + [sg.Input(key='-IN-')], + [sg.Button('Go'), sg.Button('Exit')] ] + +window = sg.Window('My new window', layout) + +while True: # Event Loop + event, values = window.read() + if event in (sg.WIN_CLOSED, 'Exit'): + break + + if event == 'Go': + sg.popup('Go button clicked', 'Input value:', values['-IN-']) + +window.close() + From eeb95398e0b4ee0e16185753c43145d2995e795c Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Mon, 29 May 2023 10:21:17 -0400 Subject: [PATCH 2/2] Added a function around the functional example --- DemoPrograms/Demo_Class_Wrapper.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/DemoPrograms/Demo_Class_Wrapper.py b/DemoPrograms/Demo_Class_Wrapper.py index 12201b23..4cf359c3 100644 --- a/DemoPrograms/Demo_Class_Wrapper.py +++ b/DemoPrograms/Demo_Class_Wrapper.py @@ -9,7 +9,7 @@ import PySimpleGUI as sg GUI frameworks use, but there is no advantage to structuring you code in his manner. It adds confusion, not clarity. - The class version is 18 lines of code. The plain version is 11 lines of code. + The class version is 18 lines of code. The plain version is 13 lines of code. Two things about the class wrapper jump out as adding confusion: 1. Unneccessary fragmentation of the event loop - the button click code is pulled out of the loop entirely @@ -83,19 +83,21 @@ my_gui.run() MMMMMMMMMMM ''' -layout = [ [sg.Text('My layout')], - [sg.Input(key='-IN-')], - [sg.Button('Go'), sg.Button('Exit')] ] +def gui_function(): + layout = [ [sg.Text('My layout')], + [sg.Input(key='-IN-')], + [sg.Button('Go'), sg.Button('Exit')] ] -window = sg.Window('My new window', layout) + window = sg.Window('My new window', layout) -while True: # Event Loop - event, values = window.read() - if event in (sg.WIN_CLOSED, 'Exit'): - break + while True: # Event Loop + event, values = window.read() + if event in (sg.WIN_CLOSED, 'Exit'): + break - if event == 'Go': - sg.popup('Go button clicked', 'Input value:', values['-IN-']) + if event == 'Go': + sg.popup('Go button clicked', 'Input value:', values['-IN-']) -window.close() + window.close() +gui_function()