Pysimplegui Combo动态刷新

Pysimplegui Combo dynamically refresh

大家好。 当第一个 Combo 中的 json 元素被选中时,有没有办法在第二个 Combo 中动态添加 json 值?

import json
import PySimpleGUI as sg


file = {"item 1": [{"name": "title 1"}, {"name": "title 2"}], "item 2": [{"name": "title 3"}, {"name": "title 4"}]}
j_file = json.dumps(file)

layout = [[sg.Text('items'), sg.Combo(j_file, key='some_key', size=(30, 0))],  # there must be items
          [sg.Text('titles'), sg.Combo(j_file, key='other_key', size=(30, 0))]]  # there must be values of selected item
window = sg.Window('testing', layout)
while True:
    event, value = window.read()
    if event == sg.WINDOW_CLOSED:
        break

这里不是直接使用json数据j_file,而是字典file.

选择第一个组合后,生成一个事件,然后用新列表更新第二个组合。

import json
import PySimpleGUI as sg


file = {"item 1": [{"name": "title 1"}, {"name": "title 2"}], "item 2": [{"name": "title 3"}, {"name": "title 4"}]}
j_file = json.dumps(file)

layout = [[sg.Text('items'), sg.Combo(list(file.keys()), enable_events=True, key='some_key', size=(30, 0))],  # there must be items
          [sg.Text('titles'), sg.Combo([], key='other_key', size=(30, 0))]]  # there must be values of selected item
window = sg.Window('testing', layout)

while True:
    event, values = window.read()
    if event == sg.WINDOW_CLOSED:
        break
    elif event =='some_key':
        item = values[event]
        title_list = [i["name"] for i in file[item]]
        window['other_key'].update(value='', values=title_list)

window.close()

我在尝试更新 default_value 时遇到了类似的问题,@JasonYang 的这一行修复了它:

window['other_key'].update(value='', values=title_list)

我在做:

window['other_key'].update(values=new_val_list)
window['other_key'].update(value=auto_complete_sugg)
#window['other_key'].update(default_value=auto_complete_sugg)