Turned demo into a usable function with a tert harness that's run when exeuting it

This commit is contained in:
PySimpleGUI 2019-11-30 01:50:39 -05:00
parent 5a966a28fa
commit 7d08d07d9f
1 changed files with 699 additions and 671 deletions

View File

@ -1,7 +1,11 @@
import PySimpleGUI as sg import PySimpleGUI as sg
def color_chooser():
"""
color_map = { :return: Any(str, None) Returns hex string of color chosen or None if nothing was chosen
"""
color_map = {
'alice blue': '#F0F8FF', 'alice blue': '#F0F8FF',
'AliceBlue': '#F0F8FF', 'AliceBlue': '#F0F8FF',
'antique white': '#FAEBD7', 'antique white': '#FAEBD7',
@ -652,31 +656,55 @@ color_map = {
'yellow3': '#CDCD00', 'yellow3': '#CDCD00',
'yellow4': '#8B8B00', 'yellow4': '#8B8B00',
'YellowGreen': '#9ACD32', 'YellowGreen': '#9ACD32',
} }
sg.change_look_and_feel('Dark Blue 3') sg.change_look_and_feel('Dark Blue 3')
button_size = (1,1) button_size = (1,1)
# button_size = (None,None) # for very compact buttons # button_size = (None,None) # for very compact buttons
def ColorButton(color): def ColorButton(color):
"""
A User Defined Element - returns a Button that configured in a certain way.
:param color: Tuple[str, str] ( color name, hex string)
:return: sg.Button object
"""
return sg.B(button_color=('white', color[1]), pad=(0,0), size=button_size,key=color, tooltip=f'{color[0]}:{color[1]}', border_width=0) return sg.B(button_color=('white', color[1]), pad=(0,0), size=button_size,key=color, tooltip=f'{color[0]}:{color[1]}', border_width=0)
num_colors = len(list(color_map.keys())) num_colors = len(list(color_map.keys()))
row_len=40 row_len=40
grid = [[ColorButton(list(color_map.items())[c+j*row_len]) for c in range(0,row_len)] for j in range(0,num_colors//row_len)] grid = [[ColorButton(list(color_map.items())[c+j*row_len]) for c in range(0,row_len)] for j in range(0,num_colors//row_len)]
grid += [[ColorButton(list(color_map.items())[c+num_colors-num_colors%row_len]) for c in range(0,num_colors%row_len)]] grid += [[ColorButton(list(color_map.items())[c+num_colors-num_colors%row_len]) for c in range(0,num_colors%row_len)]]
layout = [ [sg.Text('Pick a color', font='Def 18')]] + grid + \ layout = [ [sg.Text('Pick a color', font='Def 18')]] + grid + \
[[sg.Button('Exit'), sg.T(size=(30,1), key='-OUT-')]] [[sg.Button('OK'), sg.T(size=(30,1), key='-OUT-')]]
window = sg.Window('Window Title', layout, no_titlebar=True, grab_anywhere=True, keep_on_top=True, use_ttk_buttons=True) window = sg.Window('Window Title', layout, no_titlebar=True, grab_anywhere=True, keep_on_top=True, use_ttk_buttons=True)
color_chosen = None
while True: # Event Loop while True: # Event Loop
event, values = window.read() event, values = window.read()
print(event, values) if event in (None, 'OK'):
if event in (None, 'Exit'): if event is None:
color_chosen = None
break break
window['-OUT-'](f'You chose {event[0]} : {event[1]}') window['-OUT-'](f'You chose {event[0]} : {event[1]}')
window.close() color_chosen = event[1]
window.close()
return color_chosen
if __name__ == '__main__':
sg.change_look_and_feel('Light Brown 4')
layout = [[sg.In(key='-CHOICE-'), sg.B('Color Picker')],
[sg.Ok(), sg.Cancel()]]
window = sg.Window('My application', layout)
while True:
event, values = window.read()
if event in (None, 'Cancel'):
break
if event.startswith('Color'):
window.hide()
color_chosen = color_chooser()
window['-CHOICE-'].update(color_chosen)
window.un_hide()