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,7 +1,11 @@
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',
'AliceBlue': '#F0F8FF',
'antique white': '#FAEBD7',
@ -652,31 +656,55 @@ color_map = {
'yellow3': '#CDCD00',
'yellow4': '#8B8B00',
'YellowGreen': '#9ACD32',
}
}
sg.change_look_and_feel('Dark Blue 3')
sg.change_look_and_feel('Dark Blue 3')
button_size = (1,1)
# button_size = (None,None) # for very compact buttons
button_size = (1,1)
# 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)
num_colors = len(list(color_map.keys()))
row_len=40
num_colors = len(list(color_map.keys()))
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+num_colors-num_colors%row_len]) for c 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)]]
layout = [ [sg.Text('Pick a color', font='Def 18')]] + grid + \
[[sg.Button('Exit'), sg.T(size=(30,1), key='-OUT-')]]
layout = [ [sg.Text('Pick a color', font='Def 18')]] + grid + \
[[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)
while True: # Event Loop
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
event, values = window.read()
print(event, values)
if event in (None, 'Exit'):
if event in (None, 'OK'):
if event is None:
color_chosen = None
break
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()