Merge pull request #2280 from PySimpleGUI/Dev-latest

Turned demo into a usable function with a test  harness that's run whe…
This commit is contained in:
PySimpleGUI 2019-11-30 01:51:05 -05:00 committed by GitHub
commit ad2b6b1500
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 699 additions and 671 deletions

View File

@ -1,6 +1,10 @@
import PySimpleGUI as sg import PySimpleGUI as sg
def color_chooser():
"""
:return: Any(str, None) Returns hex string of color chosen or None if nothing was chosen
"""
color_map = { color_map = {
'alice blue': '#F0F8FF', 'alice blue': '#F0F8FF',
'AliceBlue': '#F0F8FF', 'AliceBlue': '#F0F8FF',
@ -660,6 +664,11 @@ 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()))
@ -669,14 +678,33 @@ grid = [[ColorButton(list(color_map.items())[c+j*row_len]) for c in range(0,row_
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]}')
color_chosen = event[1]
window.close() 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()