Pysimplegui: [Bug/Question] Error in Changing Bool Values Using Multiple Checkboxes

Created on 26 Aug 2020  路  6Comments  路  Source: PySimpleGUI/PySimpleGUI

[Bug/Question] Error in Changing Bool Values Using Multiple Checkboxes

Operating System

MacOS Catalina: v10.15.3

Python version

Python 3.8.5

PySimpleGUI Port and Version

4.29.0 Released 25-Aug-2020

Your Experience Levels In Months or Years

2 months Python programming experience
4 months Programming experience overall
Yes Have used another Python GUI Framework (tkinter, Qt, etc) previously (yes/no is fine)?

You have completed these steps:

  • [Y ] Read instructions on how to file an Issue
  • [Y ] Searched through main docs http://www.PySimpleGUI.org for your problem
  • [Y ] Searched through the readme for your specific port if not PySimpleGUI (Qt, WX, Remi)
  • [ Y] Looked for Demo Programs that are similar to your goal http://www.PySimpleGUI.com
  • [Y ] Note that there are also Demo Programs under each port on GitHub
  • [ Y] Run your program outside of your debugger (from a command line)
  • [Y ] Searched through Issues (open and closed) to see if already reported
  • [ Y] Try again by upgrading your PySimpleGUI.py file to use the current one on GitHub. Your problem may have already been fixed but is not yet on PyPI.

Description of Problem / Question / Details

I have been making a search tool where you can fill-out check-boxes and based on the check-boxes selected it will output different information. The problem I have been having is when I select a checkbox the bool value becomes true but when I select it again (un-check it) it does not go back to false. I have tried many thing including setting the button default to true but it has made no difference

Code To Duplicate

import PySimpleGUI as sg
print(sg)
print(sg.version)

## Paste your code here
[sg.Checkbox('Title', change_submits = True, enable_events=True, default=False,key='Year'), sg.Checkbox('Plot', change_submits = True, enable_events=True, default=False,key='')]

def checkboxes1():
    if values['Title'] is True and title == 0:
        title +=1
        print(movie_title)
        checkboxes1()
    elif values['Plot'] is True and plot == 0:
        plot += 1
        print(plot)
        checkboxes1()

Most helpful comment

It would be great to see a fully working program.

import PySimpleGUI as sg

layout = [  [sg.Text('My Window')],
            [sg.CB('1', key='-CB1-', enable_events=True), sg.CB('2', key='-CB2-', enable_events=True)],
            [sg.Button('Go'), sg.Button('Exit')]  ]

window = sg.Window('Window Title', layout)

while True:             # Event Loop
    event, values = window.read()
    print(event, values)
    if event == sg.WIN_CLOSED or event == 'Exit':
        break
window.close()

image

image

When I click the checkboxes, I get this output. I checked and unchecked each of them.

-CB1- {'-CB1-': True, '-CB2-': False}
-CB1- {'-CB1-': False, '-CB2-': False}
-CB2- {'-CB1-': False, '-CB2-': True}
-CB2- {'-CB1-': False, '-CB2-': False}

All 6 comments

It would be great to see a fully working program.

import PySimpleGUI as sg

layout = [  [sg.Text('My Window')],
            [sg.CB('1', key='-CB1-', enable_events=True), sg.CB('2', key='-CB2-', enable_events=True)],
            [sg.Button('Go'), sg.Button('Exit')]  ]

window = sg.Window('Window Title', layout)

while True:             # Event Loop
    event, values = window.read()
    print(event, values)
    if event == sg.WIN_CLOSED or event == 'Exit':
        break
window.close()

image

image

When I click the checkboxes, I get this output. I checked and unchecked each of them.

-CB1- {'-CB1-': True, '-CB2-': False}
-CB1- {'-CB1-': False, '-CB2-': False}
-CB2- {'-CB1-': False, '-CB2-': True}
-CB2- {'-CB1-': False, '-CB2-': False}

You need to set valid keys on your checkboxes. It looks like you're comparing against the text of the checkbox, not the key. And your function is recursive? Doesn't look like a valid program. There's no need to set both change_submits and enable_events. I think you need to do a little more looking at the demos, documentation and call reference.

BTW, thank you for taking the time to submit an issue with all of the information requested filled in. 馃憤馃徎 I really appreciate it. If you can post a short program that can be run that demonstrates the problem that would be helpful. Perhaps the sample code I posted will help you figure out your problem.

I tried again and I still have the same problem. Below I will attach a simplified version of working code (I simplified it so you do not have to install other modules). Below are some smaller problems which I think lead to my main problem.

import PySimpleGUI as sg

title = 0
plot = 0

sg.theme('LightGrey1')
window = sg.FlexForm('IMDBPY Search')

layout = [
    [sg.Text('Please enter the name of a movie below')],
    [sg.Text('Movie Title', size=(15, 1)), sg.InputText()],
    [sg.Output(size=(59, 1))],
    [sg.Button('SEARCH'), sg.Button('EXIT')],
    [sg.Checkbox('Title', change_submits = True, enable_events=True, default=False,key='-Title-'),sg.Checkbox('Plot', change_submits = True, enable_events=True, default=False,key='-Plot-')],
    ]

button, values = window.Layout(layout).Read()

def run_code():
    global movie_title, movie, title, plot, year, director, cast, runtime
    while True:
        event, value = window.read()
        if event == 'SEARCH':
            print(values[0])

            print('Please wait while your results load...')

            movie_title = 'this is the title'

            print('-----------------------------')
            checkboxes1()

        elif event == 'EXIT':
            break

def checkboxes1():
    global movie_title, movie, title, plot, year, director, cast, runtime
    if values['-Title-'] is True and title == 0:
        title +=1
        print(movie_title)
        checkboxes1()
    elif values['-Plot-'] is True and plot == 0:
        plot += 1
        print('this is the plot')
        checkboxes1()
    else:
        print('****************** COMPLETE ******************')
        title = 0
        plot = 0

run_code()

Here are some smaller problems/examples which I believe leads to my problem:

  • When you start the program selecting both check-boxes only displays the information that the first checkbox provides (it only prints the title not the title and the plot if both are selected).

    • Also if you select the title checkbox and than de-select it it still prints the title though it should not as in theory the title check-box's Boolean value should be false meaning it should just print 'Complete' if no boxes are selected at the time the search button is clicked.

Update: I just realized I missed the part where you said that I didn't need to set both change_submits and enable_events. I'm not sure why but removing those allows me to check both check-boxes and it now properly displays both sets of info when both check-boxes are checked.

That pretty much solves my major problem. I just have the problem of If I select a check-box than deselect it it still prints what would print if the check-box was selected/if it was true but I think it might just be a logic error.

Edit: I actually think my problem was solved due to making proper keys for the check-boxes. Thanks for your help.
Edit 2: I just fixed all my problems. Thanks for your help and thanks for creating such a useful GUI module.

You're quite welcome. Glad you were able to figure it all out! Nicely done.

Was this page helpful?
0 / 5 - 0 ratings