From 92bac182bd67c0b9b43993bc463a887efd1638c0 Mon Sep 17 00:00:00 2001 From: PySimpleGUI Date: Thu, 22 Sep 2022 01:41:18 -0400 Subject: [PATCH] Changed keys into tuples. Made Text AND Image clickable so will be like a normal checkbox element --- DemoPrograms/Demo_Checkboxes_Custom.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/DemoPrograms/Demo_Checkboxes_Custom.py b/DemoPrograms/Demo_Checkboxes_Custom.py index 871a3555..cca79bf0 100644 --- a/DemoPrograms/Demo_Checkboxes_Custom.py +++ b/DemoPrograms/Demo_Checkboxes_Custom.py @@ -18,8 +18,8 @@ import PySimpleGUI as sg def main(): layout = [[sg.Text('Fancy Checkboxes... Simply')], - [sg.Image(checked, key='-IMAGE1-', metadata=True, enable_events=True), sg.Text(True, k='-VAL1-')], - [sg.Image(unchecked, key='-IMAGE2-', metadata=False, enable_events=True), sg.Text(False, k='-VAL2-')], + [sg.Image(checked, key=('-IMAGE-', 1), metadata=True, enable_events=True), sg.Text(True, enable_events=True, k=('-TEXT-', 1))], + [sg.Image(unchecked, key=('-IMAGE-', 2), metadata=False, enable_events=True), sg.Text(False, enable_events=True, k=('-TEXT-', 2))], [sg.Button('Go'), sg.Button('Exit')]] window = sg.Window('Custom Checkboxes', layout, font="_ 14") @@ -28,12 +28,13 @@ def main(): if event == sg.WIN_CLOSED or event == 'Exit': break # if a checkbox is clicked, flip the vale and the image - if '-IMAGE' in event: - window[event].metadata = not window[event].metadata - window[event].update(checked if window[event].metadata else unchecked) - # For fun, update the string next to the checkbox - window['-VAL1-'].update(window['-IMAGE1-'].metadata) - window['-VAL2-'].update(window['-IMAGE2-'].metadata) + if event[0].startswith(('-IMAGE', '-TEXT')): + cbox_key = ('-IMAGE-', event[1]) + window[cbox_key].metadata = not window[cbox_key].metadata + window[cbox_key].update(checked if window[cbox_key].metadata else unchecked) + # Update the string next to the checkbox + window[('-TEXT-', 1)].update(window[('-IMAGE-', 1)].metadata) + window[('-TEXT-', 2)].update(window[('-IMAGE-', 2)].metadata) window.close()