python - 如何使用 if 语句处理 pysimplegui 中的复选框
问题描述
我正在pysimplegui
努力处理checkboxes
用户选择的问题。我想知道为什么我if-statements
的工作与我设计它们的方式相反。我可能做错了什么。
请参阅以下重点领域:
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")
问题是程序没有按预期运行。如果我选择 HTML 和 EXCEL,它会显示不同的循环块。
这是 export_values 显示的输出类型{"pdf_export": True, "excel_export": False, "html_export": False}
我欢迎一种优雅的方式来重构我的代码以提高可读性并解决手头的问题。
解决方案
使用此代码更改您的代码,也许这对您有用:
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")