Reputation: 1323
I am working with pysimplegui
I am trying to handle the checkboxes
chosen by the users. I wonder why my if-statements
are working against what I designed them do. What could I possibly be doing wrong.
See the area of focus below:
if event == "Export Options":
try:
export_window_layout = [
[sg.Frame(
layout=[
[sg.Checkbox('PDF', default=True, key='pdf_export'),
sg.Checkbox('Excel', key='excel_export'),
sg.Checkbox('HTML file', key='html_export')],
],
title='Select file types',
title_color='white',
relief=sg.RELIEF_SUNKEN,
tooltip='Select the formats you would like to export')],
[sg.Button('Export'), sg.Button('Cancel')]]
export_window = sg.Window("Export Options", export_window_layout)
while True:
export_event, export_values = export_window.read()
print(f"export events: {export_event} \n export values: {export_values}")
if export_event in (None, 'Cancel'):
break
# if a user selects PDF format only
if not (export_values['excel_export'] and export_values['html_export']):
try:
print("You selected PDF only")
break
except:
sg.popup("There was an issue with the export!")
break
# if a user selects PDF and excel only
if not export_values['html_export']:
try:
sg.popup("You have selected pdf and excel")
except:
sg.popup_error("There was an issue with the export!")
break
# if a user selects PDF and HTML only
if not export_values['excel_export']:
try:
sg.popup("You have selected pdf and HTML")
except:
sg.popup_error("There was an issue with the export!")
break
# if a user selects ALL files
if (export_values['pdf_export'] and export_values['excel_export'] and export_values['html_export']):
try:
print("You are in the ALL files area")
sg.popup("You have selected ALL files")
except:
sg.popup_error("There was an issue with the export!")
break
# if a user selects excel only
if not (export_values['pdf_export'] and export_values['html_export']):
try:
sg.popup("You have selected Excel only")
except:
sg.popup_error("There was an issue with the export!")
break
# if a user selects HTML only
if not (export_values['pdf_export'] and export_values['excel_export']):
try:
sg.popup("You have selected HTML only")
except:
sg.popup_error("There was an issue with the export!")
break
# if a user selects excel and HTML
if not export_values['pdf_export']:
try:
sg.popup("You have selected Excel and HTML")
except:
sg.popup_error("There was an issue with the export!")
break
export_window.Close()
except:
sg.popup("You must pass STEP ONE & TWO before EXPORTING")
The problem is that the program is not behaving as intended. If I select HTML and EXCEL, it shows a different loop block.
Here is the kind of output the export_values shows {"pdf_export": True, "excel_export": False, "html_export": False}
I welcome an elegant way to refactor my code to improve readability as well as fixing the issue at hand.
Upvotes: 1
Views: 3243
Reputation: 1016
Change Your Code with this code, maybe this will work for you:
if event == "Export Options":
try:
export_window_layout = [
[sg.Frame(
layout=[
[sg.Checkbox('PDF', default=True, key='pdf_export'),
sg.Checkbox('Excel', key='excel_export'),
sg.Checkbox('HTML file', key='html_export')],
],
title='Select file types',
title_color='white',
relief=sg.RELIEF_SUNKEN,
tooltip='Select the formats you would like to export')],
[sg.Button('Export'), sg.Button('Cancel')]]
export_window = sg.Window("Export Options", export_window_layout)
while True:
export_event, export_values = export_window.read()
print(f"export events: {export_event} \n export values: {export_values}")
if export_event in (None, 'Cancel'):
break
# if a user selects PDF format only
if export_values['excel_export'] == False and export_values['html_export'] == False:
try:
sg.popup("You selected PDF only")
break
except:
sg.popup("There was an issue with the export!")
break
# if a user selects excel only
elif export_values['pdf_export'] == False and export_values['html_export'] == False:
try:
sg.popup("You have selected Excel only")
except:
sg.popup_error("There was an issue with the export!")
break
# if a user selects HTML only
elif export_values['pdf_export'] == False and export_values['excel_export'] == False:
try:
sg.popup("You have selected HTML only")
except:
sg.popup_error("There was an issue with the export!")
break
# if a user selects PDF and excel only
elif export_values['html_export'] == False:
try:
sg.popup("You have selected pdf and excel")
except:
sg.popup_error("There was an issue with the export!")
break
# if a user selects PDF and HTML only
elif export_values['excel_export'] == False:
try:
sg.popup("You have selected pdf and HTML")
except:
sg.popup_error("There was an issue with the export!")
break
# if a user selects excel and HTML
if export_values['pdf_export'] == False:
try:
sg.popup("You have selected Excel and HTML")
except:
sg.popup_error("There was an issue with the export!")
break
# if a user selects ALL files
else :
try:
print("You are in the ALL files area")
sg.popup("You have selected ALL files")
except:
sg.popup_error("There was an issue with the export!")
break
export_window.Close()
except:
sg.popup("You must pass STEP ONE & TWO before EXPORTING")
Upvotes: 1