所以我在 Pysimplegui 的事件回圈中遇到了很多問題,我試圖根據代碼的另一部分中缺少輸入來禁用一個按鈕(在這種情況下,如果上面的單選按鈕缺少選擇至少一個復選框按下[但如果未選擇檔案夾,我應該應用相同的邏輯])。我試著到處尋找,要么我找不到解決方案,要么人們顯然設法根據食譜(我試過但不能)來解決。
我嘗試的代碼如下:
這是有關單選按鈕的部分:
layout_1 = [
# things before...
[sg.Radio('CSV/JSON:', "OUTPUT", default=False, key='-OutputFile-', enable_events=True)],
[sg.Checkbox('Output as CSV', key='-OutputCSV-', disabled=True)],
[sg.Checkbox('Output as JSON', key='-OutputJSON-', disabled=True)],
[sg.Text('Select a folder: ')], [sg.FolderBrowse(key='-Folder-')],
[sg.Push(), sg.Column([[sg.Button("Submit", disabled=False, key='-Submit-'), sg.Cancel()]], element_justification='c'), sg.Push()]
]
window_2 = sg.Window('Info', layout_1, finalize = True)
然后我有這個while回圈,理論上,如果'-OutputFile-'為False,則基本上應該禁用選擇輸出的能力,并且如果沒有選擇兩個可用輸出(CSV或JSON),請禁用提交按鈕。事情是禁用選擇有效,但提交按鈕永遠不會被禁用并且無法單擊。
while True:
event_2, values_table = window_2.read()
if event_2 in (sg.WIN_CLOSE, 'Cancel', 'Submit', None):
break
elif event_2 == '-OutputFile-':
window_2['-OutputJSON-'].update(disabled = False)
window_2['-OutputCSV-'].update(disabled = False)
if window_2['-OutputFile-'] and not window_2['-OutputCSV-'] and not window_2['-OutputJSON-']:
window_2['-Submit-'].update(disabled=True)
sg.popup('Select an output', keep_on_top=True)
elif event_2 == '-OutputNone-':
window_2['-OutputJSON-'].update(disabled=True)
window_2['-OutputCSV-'].update(disabled=True)
window_2.close()
我應該注意,代碼在沒有關于提交按鈕的回圈部分的情況下作業,但我也需要那部分。
我嘗試的另一件事是使用 False 而不是 not in the part not working。
這是視窗的樣子:

PS:代碼中可能有語法錯誤,不幸的是我不得不復制它,即使我檢查了,也可能有一些錯誤。
uj5u.com熱心網友回復:
您需要.Get()inwindow_2['-OutputCSV-'].Get()并window_2['-OutputJSON-'].Get()檢查是否選擇了元素。
if not window_2['-OutputCSV-'].Get() and not window_2['-OutputJSON-'].Get():
window_2['-Submit-'].update(disabled=True)
sg.popup('Select an output', keep_on_top=True)
您還需要Submit在選擇JSON或時激活代碼CSV,并Submit在取消選擇這兩個元素時停用。
elif event_2 in ('-OutputCSV-', '-OutputJSON-'):
if not window_2['-OutputCSV-'].Get() and not window_2['-OutputJSON-'].Get():
window_2['-Submit-'].update(disabled=True)
#sg.popup('Select an output', keep_on_top=True)
else:
window_2['-Submit-'].update(disabled=False)
完整的作業示例:
import PySimpleGUI as sg
layout_1 = [
# things before...
[sg.Radio('None:', "OUTPUT", default=False, key='-OutputNone-', enable_events=True)],
[sg.Radio('CSV/JSON:', "OUTPUT", default=False, key='-OutputFile-', enable_events=True)],
[sg.Checkbox('Output as CSV', key='-OutputCSV-', disabled=True, enable_events=True)],
[sg.Checkbox('Output as JSON', key='-OutputJSON-', disabled=True, enable_events=True)],
[sg.Text('Select a folder: ')], [sg.FolderBrowse(key='-Folder-')],
[sg.Push(), sg.Column([[sg.Button("Submit", disabled=False, key='-Submit-'), sg.Cancel()]], element_justification='c'), sg.Push()]
]
window_2 = sg.Window('Info', layout_1, finalize=True)
while True:
event_2, values_table = window_2.read()
print(event_2)
if event_2 in ('Cancel', 'Submit', None):
break
elif event_2 == '-OutputFile-':
window_2['-OutputJSON-'].update(disabled = False)
window_2['-OutputCSV-'].update(disabled = False)
if not window_2['-OutputCSV-'].Get() and not window_2['-OutputJSON-'].Get():
window_2['-Submit-'].update(disabled=True)
sg.popup('Select an output', keep_on_top=True)
elif event_2 == '-OutputNone-':
window_2['-OutputJSON-'].update(disabled=True)
window_2['-OutputCSV-'].update(disabled=True)
elif event_2 in ('-OutputCSV-', '-OutputJSON-'):
if not window_2['-OutputCSV-'].Get() and not window_2['-OutputJSON-'].Get():
window_2['-Submit-'].update(disabled=True)
#sg.popup('Select an output', keep_on_top=True)
else:
window_2['-Submit-'].update(disabled=False)
window_2.close()
uj5u.com熱心網友回復:
沒有仔細檢查代碼,但下面的說法是錯誤的。應該檢查它們values[...],而不是window_2[...]哪個是元素。
if window_2['-OutputFile-'] and not window_2['-OutputCSV-'] and not window_2['-OutputJSON-']
示例代碼:
import PySimpleGUI as sg
sg.theme('DarkBlue4')
layout = [
[sg.Radio('None', 'Group', default=True, key='R1', enable_events=True)],
[sg.Radio('CSV/JSON', 'Group', key='R2', enable_events=True),
sg.Checkbox('CSV', default=False, key='C1', enable_events=True, disabled=True),
sg.Checkbox('JSON', default=False, key='C2', enable_events=True, disabled=True)],
[sg.Push(), sg.Button('Submit', key='B1', disabled=True)]
]
window = sg.Window("Title", layout, finalize=True)
r1, r2, c1, c2, b1 = [window[key] for key in ('R1', 'R2', 'C1', 'C2', 'B1')]
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif event in ('R1', 'R2'):
disabled = False if values['R2'] else True
c1.update(disabled=disabled)
c2.update(disabled=disabled)
state = False if not disabled and values['C1'] and values['C2'] else True
b1.update(disabled=state)
elif event in ('C1', 'C2'):
state = False if values['C1'] and values['C2'] else True
b1.update(disabled=state)
window.close()

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/519338.html
