将新 window (pysimplegui) 中的列表框值更改为用户可以选择一个选项并暂停主执行
Change Listbox values in new window (pysimplegui) to users can choose an option and pause the main execution
我想要一个 un_hide 新 window 的应用程序并显示用户选择的选项,问题是值仅在主执行结束时填充到列表框中。
程序开始 -->> windows2 un_hide >> 程序不暂停且列表框未填充 >> 程序结束 >> 列表框填充了值 :(
我的目标是:
程序开始 -->> windows2 un_hide >> 程序暂停并填充列表框以供用户选择一个选项 >> 程序继续并结束
我该怎么做?
(...) #main execution
windows2.un_hide()
windows2.Element('listbox').Update(values=keys_list)
#Must Wait for user can choice options, and listbox must to be populated now, but that is happening only in the end of main executation
(...) #end main execution
layout2 = [[sg.Text('The second window')],
[sg.Listbox(values=["none"], select_mode='extended', key='listbox', size=(30, 6))],
[sg.Button('save', button_color=('black', 'white'), visible=True ), ]]
windows2 = sg.Window('Second Window', layout2, finalize=True)
windows2.hide()
下面是一个示例,我如何使用函数创建弹出窗口,return 选择主要 window。
import PySimpleGUI as sg
def popup(key):
sg.theme('DarkGreen3')
layout = [
[sg.Listbox(food[key], size=(30, 3), enable_events=True, key='-GOODS-')],
[sg.Button("Cancel")],
]
window = sg.Window(key, layout, modal=True)
while True:
event, values = window.read()
if event in (sg.WINDOW_CLOSED, 'Cancel'):
result = None
break
elif event == '-GOODS-':
result = values[event][0]
break
window.close()
return result
food = {
'Fruit' : ['Apple','Bananas','Oranges','Mangoes'],
'Vegetable' : ['Kale', 'Cabbage', 'Celery', 'Asparagus', 'Lettuce'],
'Meat' : ['Pork', 'Beef', 'Mutton', 'Veal', 'Vension'],
}
font = ("Courier New", 11)
sg.theme('DarkBlue3')
sg.set_options(font=font)
layout = [
[sg.Listbox(list(food.keys()), size=(30, 3), enable_events=True, key='-CATEGORY-')],
[sg.VPush()],
[sg.StatusBar('', size=(0, 1), key='-STATUS-')],
]
window = sg.Window('Food', layout, size=(640, 480))
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
elif event == '-CATEGORY-':
key = values[event][0]
goods = popup(key)
if goods:
window['-STATUS-'].update(f'{key} - {goods}')
else:
window['-STATUS-'].update('')
window.close()
我想要一个 un_hide 新 window 的应用程序并显示用户选择的选项,问题是值仅在主执行结束时填充到列表框中。
程序开始 -->> windows2 un_hide >> 程序不暂停且列表框未填充 >> 程序结束 >> 列表框填充了值 :(
我的目标是:
程序开始 -->> windows2 un_hide >> 程序暂停并填充列表框以供用户选择一个选项 >> 程序继续并结束
我该怎么做?
(...) #main execution
windows2.un_hide()
windows2.Element('listbox').Update(values=keys_list)
#Must Wait for user can choice options, and listbox must to be populated now, but that is happening only in the end of main executation
(...) #end main execution
layout2 = [[sg.Text('The second window')],
[sg.Listbox(values=["none"], select_mode='extended', key='listbox', size=(30, 6))],
[sg.Button('save', button_color=('black', 'white'), visible=True ), ]]
windows2 = sg.Window('Second Window', layout2, finalize=True)
windows2.hide()
下面是一个示例,我如何使用函数创建弹出窗口,return 选择主要 window。
import PySimpleGUI as sg
def popup(key):
sg.theme('DarkGreen3')
layout = [
[sg.Listbox(food[key], size=(30, 3), enable_events=True, key='-GOODS-')],
[sg.Button("Cancel")],
]
window = sg.Window(key, layout, modal=True)
while True:
event, values = window.read()
if event in (sg.WINDOW_CLOSED, 'Cancel'):
result = None
break
elif event == '-GOODS-':
result = values[event][0]
break
window.close()
return result
food = {
'Fruit' : ['Apple','Bananas','Oranges','Mangoes'],
'Vegetable' : ['Kale', 'Cabbage', 'Celery', 'Asparagus', 'Lettuce'],
'Meat' : ['Pork', 'Beef', 'Mutton', 'Veal', 'Vension'],
}
font = ("Courier New", 11)
sg.theme('DarkBlue3')
sg.set_options(font=font)
layout = [
[sg.Listbox(list(food.keys()), size=(30, 3), enable_events=True, key='-CATEGORY-')],
[sg.VPush()],
[sg.StatusBar('', size=(0, 1), key='-STATUS-')],
]
window = sg.Window('Food', layout, size=(640, 480))
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
elif event == '-CATEGORY-':
key = values[event][0]
goods = popup(key)
if goods:
window['-STATUS-'].update(f'{key} - {goods}')
else:
window['-STATUS-'].update('')
window.close()