问题描述
我正在与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")