我试图让我的 GUI 根据组合框中选择的项目显示信息。 PySimpleGUI
食谱说我应该使用 GetSelectedItemsIndexes()
方法,但是当我尝试使用它时:
window.Element('_COMBOBOX_').GetSelectedItemsIndexes()
我明白了:
AttributeError: 'Combo' object has no attribute 'GetSelectedItemsIndexes'
我尝试在控制台中输入以下内容:
dir(window.Element('_COMBOBOX_'))
而且似乎 GetSelectedItemsIndexes
甚至不存在...那么我如何从组合框中获取所选值的索引?
最佳答案
这对我有用:
import PySimpleGUI as sg
layout = [[sg.Combo(['choice 1', 'choice 2', 'choice 3'], enable_events=True, key='combo')],
[sg.Button('Test'), sg.Exit()]
]
window = sg.Window('combo test', layout)
while True:
event, values = window.Read()
if event is None or event == 'Exit':
break
if event == 'Test':
combo = values['combo'] # use the combo key
print(combo)
window.Close()
关于python - 获取 Combobox 项的值或索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57088895/