I have a PySimpleGUI application and I need to check a checkbox programmatically. I have looked at the cookbook, but couldn't find anything useful to help me. I have tried:
window["-CHECKBOX-"].update(checked=True)
window["-CHECKBOX-"}.update(default=True)
but both my attempts have given me a TypeError, saying that the update received an unexpected keyword argument.I have no idea if this is possible in PySimpleGUI, it is a possibility that it simply is not possible. I am using the Tkinter port, so I could use the underlying tkinter module if need be. Thanks in advance! Note: I am using Python 3.11
Full relevant code:
import pySimpleGUI as sglayout = [[sg.Button("Check Checkbox", key="-CHECK-", enable_events=True)],[sg.Checkbox("Check me!", key="-CHECKBOX-", enable_events=True)]]window = sg.Window("Application", margins=(40,40), layout=layout, finalize=True, resizable=True) while True:event, values = window.read()if event == "-CHECK-":pass # This would be where the code to check the checkbox would bewindow.close()
Best Answer
There's no option for checked
or default
in method update
. It should be
window["-CHECKBOX-"].update(value=True)
, orwindow["-CHECKBOX-"].update(True)
.
You can find it in https://www.pysimplegui.org/en/latest/call%20reference/#checkbox-element
Checkbox.update
Changes some of the settings for the Checkbox Element. Must call Window.Read or Window.Finalize prior. Note that changing visibility may cause element to change locations when made visible after invisible
Changes will not be visible in your window until you call window.read or window.refresh.
If you change visibility, your element may MOVE. If you want it to remain stationary, use the "layout helper" function "pin" to ensure your element is "pinned" to that location in your layout so that it returns there when made visible.
update(value = None,text = None,background_color = None,text_color = None,checkbox_color = None,disabled = None,visible = None)
Parameter Descriptions:
TypeNameMeaningboolvalueif True checks the checkbox, False clears itstrtextText to display next to checkboxstrbackground_colorcolor of backgroundstrtext_colorcolor of the text. Note this also changes the color of the checkmarkbooldisableddisable or enable elementboolvisiblecontrol visibility of element