2

I want to have a select all checkbox in my PySimpleGUI. When the select all checkbox is selected, all other checkboxes should change to True, and if any other box is unchecked, the select all checkbox should change to false state?

I can do it through button clicks, but I couldn't find a way to update checkboxes based on values selected in an another checkbox?

import PySimpleGUI as sg
layout = [
    [sg.Checkbox ('select all', key = 'checkbox')],
    [sg.Checkbox ('value 1', key='check_value1')],
    [sg.Checkbox ('value 2',key='check_value2')],
    [sg.Button ('ON', key = 'ON')],
    [sg.Button ('OFF', key = 'OFF')]
]
window = sg.Window ('Sample GUI', layout) .Finalize ()
while True: # Event Loop
    event,values=window.read()
    if event in (None, 'Exit'):
        break
    elif event == 'ON':
        window ['checkbox']. Update (value = True)
    elif event == 'OFF':
        window ['checkbox']. Update (value = False)
    print(event,values)
window.close ()

Is there any way to implement this?

Yash Sharma
  • 324
  • 6
  • 25

2 Answers2

2

Option value or first argument in method upgrade if True checks the checkbox, False clears it,

Demo code,

enter image description here

import PySimpleGUI as sg

sg.theme('DarkBlue')

layout = [
    [sg.Checkbox('All checked',   enable_events=True, key='Check_All'),
     sg.Checkbox('All unchecked', enable_events=True, key='Uncheck_All')],
    [sg.HorizontalSeparator()]] + [
    [sg.Checkbox(f'check ({j}, {i})', enable_events=True, key=f'check{j}{i}')
        for i in range(5)] for j in range(4)
]

window = sg.Window ('Sample GUI', layout, finalize=True)

while True: # Event Loop
    event, values = window.read (timeout = 100)
    if event == sg.WINDOW_CLOSED:
        break
    elif event == 'Check_All':
        for j in range(4):
            for i in range(5):
                window[f'check{j}{i}'].update(True)
        window['Uncheck_All'].update(False)
    elif event == 'Uncheck_All':
        for j in range(4):
            for i in range(5):
                window[f'check{j}{i}'].update(False)
        window['Check_All'].update(False)
    elif event.startswith('check'):
        if not values[event]:
            window['Check_All'].update(False)
        else:
            window['Uncheck_All'].update(False)

window.close ()
Jason Yang
  • 11,284
  • 2
  • 9
  • 23
  • Maybe use a tuple for the key instead of a string. key=(i,j)? The values variable has the checkbox values and will save a lookup and call to get() if you use the values dictionary instead of window[key].get(). It's still a good answer though. – Mike from PSG Jan 12 '21 at 21:35
  • Yes, `window[event].get()` updated by `values[event]` now. – Jason Yang Jan 13 '21 at 07:45
1

Update Of the method Value Switch parameters. In the example below, if you press the ON button, a check will be entered, and if you press the OFF button, the check will be removed.

import PySimpleGUI as sg
from PySimpleGUI import Checkbox, Button
layout = [
    [sg.Checkbox ('checkbox', key = 'checkbox')],
    [sg.Button ('ON', key = 'ON')],
    [sg.Button ('OFF', key = 'OFF')]
]
window = sg.Window ('Sample GUI', layout) .Finalize ()
while True: # Event Loop
    event, values ​​= window.read (timeout = 100)
    if event in (None, 'Exit'):
        break
    elif event == 'ON':
        window ['checkbox']. Update (value = True)
    elif event == 'OFF':
        window ['checkbox']. Update (value = False)
window.close ()

For more you can refer to this docs also https://pysimplegui.readthedocs.io/en/latest/call%20reference/#checkbox-element

Zeeking786
  • 165
  • 1
  • 27
  • I am using the code from cookbook as well. Here, value of the checkbox is being changed on a button event. I require that when select all checkbox is checked in my example code, value 1 and value 2 checkboxes should get checked as well. And if either value 1 or value 2 is unchecked, select all should get unchecked as well. – Yash Sharma Jan 12 '21 at 07:57
  • Once see the docs or you can refer https://github.com/PySimpleGUI/PySimpleGUI/tree/master/DemoPrograms – Zeeking786 Jan 12 '21 at 08:11