New Demo Programs - SDK Help used in the Udemy course

This commit is contained in:
PySimpleGUI 2022-01-27 11:56:16 -05:00
parent 7ba9a8694b
commit 63a10659ff
2 changed files with 206 additions and 0 deletions

View File

@ -0,0 +1,110 @@
import PySimpleGUI as sg
import inspect
import sys
"""
Displays a window showing the parms for the PySimpleGUI functions
The default view is to only show function names that are all lower case.
You can change this by right clicking and choosing "Upper Case Too"
Copyright 2022 PySimpleGUI Project
"""
def main():
"""
Display a window that will display the docstrings for each PySimpleGUI Element and the Window object
"""
sg.set_options(font='courier 12')
functions = [m for m in inspect.getmembers(sys.modules['PySimpleGUI'], inspect.isfunction)]
functions_names_lower = [f for f in functions if f[0][0].islower()]
functions_names_upper = [f for f in functions if f[0][0].isupper()]
# functions_names = sorted(functions_names_lower) + sorted(functions_names_upper)
# func_names_str = [f[0] for f in functions if f[0][0].islower()]
func_names_str = [f[0] for f in functions]
func_parm_dict = {}
# for func_str, func in functions_names_lower:
for func_str, func in functions:
# Build info about init method
args = inspect.signature(func)
params = args.parameters
func_parm_list = []
for a in params.values():
func_def = str(a).split('=')
if len(func_def) == 1:
name, default = func_def[0], '*Required*'
if name[0] == '*':
default = '*Optional*'
elif len(func_def) == 2:
name, default = func_def[0], func_def[1]
elif len(func_def) == 0:
name, default = '', ''
else:
name, default = func_def[0], '*Object*'
func_parm_list.append((name, default))
func_parm_dict[func_str] = func_parm_list
sg.theme('black')
sg.theme_background_color('#131314')
sg.theme_text_element_background_color('#131314')
sg.theme_input_background_color('#131314')
ml = sg.Multiline(size=(35, 20), key='-ML-', write_only=True, reroute_stdout=False, expand_y=True, expand_x=True)
layout = [
[sg.Titlebar('Func Parm Viewer', background_color='#131314', text_color='white')],
# [sg.Combo([e for e in sorted(func_names_str)],background_color='#131314', size=(25,30), enable_events=True, key='-COMBO-'), sg.T(' '*6, grab=True)],
[sg.Combo([e for e in sorted([f[0] for f in functions if f[0][0].islower()])],background_color='#131314', size=(25,30), enable_events=True, readonly=True, expand_x=True, key='-COMBO-', tooltip='Right click for more options')],
sg.vtop([ml], expand_x=True, expand_y=True)] + [[sg.Sizegrip()]]
window = sg.Window('Func Parms', layout, use_default_focus=False, keep_on_top=True, no_titlebar=True, margins=(0,0), right_click_menu=[[],['Edit Me', 'Upper Case Too', 'Lower Case Only', 'Exit']], resizable=True)
while True: # Event Loop
event, values = window.read()
if event in (sg.WIN_CLOSED, 'Exit'):
break
if event == 'Edit Me':
sg.execute_editor(__file__)
continue
elif event.startswith('Upper'):
window['-COMBO-'].update(values=[f[0] for f in functions if not f[0][0].startswith('_')])
elif event.startswith('Lower'):
window['-COMBO-'].update(values=[f[0] for f in functions if f[0][0].islower()])
else:
# ml.print(event, values)
if event == '-COMBO-':
func_chosen = values[event]
else:
func_chosen = None
window['-ML-'].update('')
ml.print(f'= {func_chosen} =', background_color='#FFFF00', text_color='black')
func_parms = func_parm_dict[func_chosen]
# print(func_parms)
for parm, default in func_parms:
ml.print(f'{parm:18}', text_color='green yellow', end=' = ')
if default != inspect._empty:
if isinstance(default, str):
if default in ('None', '(None, None)', '(None,None)'):
color = 'pink'
elif default in ('False', 'True'):
color = '#00FF7F'
else:
color = None
ml.print(f'{default}', end='\n', text_color=color)
else:
ml.print(default, end='\n')
else:
ml.print(f'{default}', end='\n')
ml.set_vscroll_position(0)
window.close()
if __name__ == '__main__':
main()

View File

@ -0,0 +1,96 @@
import PySimpleGUI as sg
import inspect
"""
Displays a window showing the Init and Update parms for the PySimpleGUI Elements
The Function based elements (e.g. Push) can be found using the Demo Program Demo_SDK_Help_Func_Parms.py
If you've seen the Udemy course, then you saw this tool used in the element lessons
The Pink parameter signify the "Common" paramters that are shared among many of the elements
Copyright 2022 PySimpleGUI Project
"""
def main():
"""
Display a window that displays the parms for the init and update methods for each element
"""
common_parms = ['key','k','font','pad','p', 'visible','size','s', 'change_submits', 'enable_events','right_click_menu','tooltip','metadata', 'expand_x', 'expand_y']
element_classes = sg.Element.__subclasses__()
element_names = {element.__name__: element for element in element_classes}
element_names['Window'] = sg.Window
element_names['SystemTray'] = sg.SystemTray
# vars3 = [m for m in inspect.getmembers(sys.modules[__name__])]
element_arg_default_dict = {}
element_arg_default_dict_update = {}
for element in element_classes:
# Build info about init method
args = inspect.getfullargspec(element.__init__).args[1:]
defaults = inspect.getfullargspec(element.__init__).defaults
if len(args) != len(defaults):
diff = len(args) - len(defaults)
defaults = ('NO DEFAULT',)*diff + defaults
args_defaults = []
for i, a in enumerate(args):
args_defaults.append((a, defaults[i]))
element_arg_default_dict[element.__name__] = args_defaults
# Build info about update method
args = inspect.getfullargspec(element.update).args[1:]
defaults = inspect.getfullargspec(element.update).defaults
if args is None or defaults is None:
element_arg_default_dict_update[element.__name__] = (('',''),)
continue
if len(args) != len(defaults):
diff = len(args) - len(defaults)
defaults = ('NO DEFAULT',)*diff + defaults
args_defaults = []
for i, a in enumerate(args):
args_defaults.append((a, defaults[i]))
element_arg_default_dict_update[element.__name__] = args_defaults if len(args_defaults) else (('',''),)
sg.theme('black')
sg.theme_background_color('#131314')
sg.theme_text_element_background_color('#131314')
sg.theme_input_background_color('#131314')
ml = sg.Multiline(size=(40, 30), key='-ML-', write_only=True, reroute_stdout=True, expand_y=True, expand_x=True)
layout = [ [sg.Titlebar('Element Init & Update Parm Viewer', background_color='#131314', text_color='white')],
[sg.Combo([e for e in sorted(element_names.keys())],background_color='#131314', size=(25,30), enable_events=True, readonly=True, expand_x=True, key='-COMBO-')],
sg.vtop([ml], expand_y=True, expand_x=True) ] + [[sg.Sizegrip()]]
# layout += [[Button('Exit', size=(15, 1))]]
window = sg.Window('Init & Update Parms', layout, use_default_focus=False, keep_on_top=True, no_titlebar=True, margins=(0,0),font='Courier 12', right_click_menu=sg.MENU_RIGHT_CLICK_EDITME_EXIT, resizable=True)
# ml = window['-ML-'] # type: sg.MLine
while True: # Event Loop
event, values = window.read()
if event in (sg.WIN_CLOSED, 'Exit'):
break
# ml.print(event, values)
if event == '-COMBO-':
element_chosen = values[event]
else:
element_chosen = None
if element_chosen in element_arg_default_dict:
window['-ML-'].update('')
ml.print('========== Init Parms ==========', background_color='#FFFF00', text_color='black')
for parm, default in element_arg_default_dict[element_chosen]:
ml.print(f'{parm:18}', text_color='hot pink' if parm in common_parms else 'green yellow', end=' = ')
ml.print(default, text_color='hot pink' if parm in common_parms else 'white', end = ',\n')
ml.print('========== Update Parms ==========', background_color='#FFFF00', text_color='black')
# print(element_arg_default_dict_update[element_chosen])
for parm, default in element_arg_default_dict_update[element_chosen]:
ml.print(f'{parm:18}', text_color='hot pink' if parm in common_parms else 'green yellow', end=' = ')
ml.print(default, text_color='hot pink' if parm in common_parms else 'white', end = ',\n')
elif event == 'Edit Me':
sg.execute_editor(__file__)
window.close()
if __name__ == '__main__':
main()