简体   繁体   中英

If statement based on listbox with PySimpleGui

So i have been having some issues with a if statement based on the PySimpleGui listbox element. The code i have is:

layout = [[sg.Listbox(values=['Listbox 1', 'Listbox 2', 'Listbox 3'], size=(30, 6))],
          [sg.Button('Next'), sg.Button('Quit')]]

window = sg.Window('PyK Mn', layout)

#############
# MAIN LOOP #
#############
while True:
    event, values = window.read()
    print(event)
    if event == sg.WIN_CLOSED or event == 'Quit':
        quit()
    elif event == 'Next':
        if values[0] == 'Listbox 3':
            print('3')

When i run this it returns 'Next' but no '3'. What am i doing wrong?

Here valuse[0] just for selected list, like ['Listbox 3'] becasue 0 as key of listbox.

To check if 'Listbox 3' selected, one more list index, like values[0][0],

import PySimpleGUI as sg

layout = [[sg.Listbox(values=['Listbox 1', 'Listbox 2', 'Listbox 3'], size=(30, 6))],
          [sg.Button('Next'), sg.Button('Quit')]]

window = sg.Window('PyK Mn', layout)

#############
# MAIN LOOP #
#############
while True:
    event, values = window.read()
    print(event, values)
    if event == sg.WIN_CLOSED or event == 'Quit':
        window.close()
        break
    elif event == 'Next':
        if values[0][0] == 'Listbox 3':
            print('3')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM