我正在尝试制作一个GUI。我需要向所有新选项卡添加搜索功能,但我不明白如何在 PySimpleGUI 中完成此操作。
这是我到目前为止的代码:
import keyword
import PySimpleGUI as sg
keywords = ['Azocompound','Aloin',"Quercetin"]
font = ('Helvetica', 16)
keys = []
def tab(i):
return [[sg.Text(f'Choose ligand',font=font)], [sg.Input('', size=(20, 1), font=font, enable_events=True, key=f'-IN-{i}')],
[sg.Listbox(values=[], size=(20, 3), enable_events=True, key=f'-BOX-{i}', font=font, select_mode=sg.LISTBOX_SELECT_MODE_SINGLE)]]
index=5
tabgroup = [[sg.Tab(f'Tab {i}' if i < index -1 else '+', tab(i), key=f'Ligand {i}') for i in range(index)]]
layout = [[sg.Text('Input Python keyword:', font=font)],
[sg.TabGroup(tabgroup, key='TabGroup', font=font)],
[sg.Submit('Start', font=font),sg.Cancel(font=font)]
]
window = sg.Window('AutoComplete', layout, finalize=True)
window['TabGroup'].bind('<<NotebookTabChanged>>', '+Switch')
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
elif event == 'TabGroup+Switch':
i = int(values['TabGroup'].split(' ')[1])
if i == index-1:
window[f'Tab {i}'].update(title=f'Tab {i}')
window['TabGroup'].add_tab(sg.Tab('+', tab(index), key=f'Tab {index}'))
index += 1
for j in range(index):
if event == f'-IN-{j}':
text = values[f'-IN-{j}']
lst = []
if text:
lst = [item for item in keywords if item.startswith(text)]
window[f'-BOX-{j}'].update(values=lst)
elif event == f'-BOX-{j}':
window[f'-IN-{j}'].update(value=values[f'-BOX-{j}'])
window.close()
我尝试在索引物种上添加迭代,但是当我添加新选项卡时,GUI 崩溃了。
您的脚本中存在错误,在这里,我通过您的代码发现了一个 PySimpleGUI 错误。
IMO,这个 PySimpleGUI 错误很快就会修复,请参阅 https://github.com/PySimpleGUI/PySimpleGUI/issues/6645
更新了代码 - 查找错误的注释。
elif event == 'TabGroup+Switch':
i = int(values['TabGroup'].split(' ')[1])
if i == index-1:
# Wrong Tab key used
window[f'Ligand {i}'].update(title=f'Tab {i}')
# Wrong Tab key setting
window['TabGroup'].add_tab(sg.Tab('+', tab(index), key=f'Ligand {index}'))
# Add this line to avoid the PySimpleGUI bug
window['TabGroup'].tab_index_to_key[index] = f'Ligand {index}'
index += 1