Simplified logic for checkbox and text click handling/updating

This commit is contained in:
PySimpleGUI 2022-09-22 02:04:05 -04:00
parent 92bac182bd
commit 4ebc727679
1 changed files with 4 additions and 4 deletions

View File

@ -15,7 +15,6 @@ import PySimpleGUI as sg
Copyright 2022 PySimpleGUI Copyright 2022 PySimpleGUI
""" """
def main(): def main():
layout = [[sg.Text('Fancy Checkboxes... Simply')], layout = [[sg.Text('Fancy Checkboxes... Simply')],
[sg.Image(checked, key=('-IMAGE-', 1), metadata=True, enable_events=True), sg.Text(True, enable_events=True, k=('-TEXT-', 1))], [sg.Image(checked, key=('-IMAGE-', 1), metadata=True, enable_events=True), sg.Text(True, enable_events=True, k=('-TEXT-', 1))],
@ -25,16 +24,17 @@ def main():
window = sg.Window('Custom Checkboxes', layout, font="_ 14") window = sg.Window('Custom Checkboxes', layout, font="_ 14")
while True: while True:
event, values = window.read() event, values = window.read()
print(event, values)
if event == sg.WIN_CLOSED or event == 'Exit': if event == sg.WIN_CLOSED or event == 'Exit':
break break
# if a checkbox is clicked, flip the vale and the image # if a checkbox is clicked, flip the vale and the image
if event[0].startswith(('-IMAGE', '-TEXT')): if event[0] in ('-IMAGE-', '-TEXT-'):
cbox_key = ('-IMAGE-', event[1]) cbox_key = ('-IMAGE-', event[1])
text_key = ('-TEXT-', event[1])
window[cbox_key].metadata = not window[cbox_key].metadata window[cbox_key].metadata = not window[cbox_key].metadata
window[cbox_key].update(checked if window[cbox_key].metadata else unchecked) window[cbox_key].update(checked if window[cbox_key].metadata else unchecked)
# Update the string next to the checkbox # Update the string next to the checkbox
window[('-TEXT-', 1)].update(window[('-IMAGE-', 1)].metadata) window[text_key].update(window[cbox_key].metadata)
window[('-TEXT-', 2)].update(window[('-IMAGE-', 2)].metadata)
window.close() window.close()