Home > Software engineering >  how to return value inside a dictionary which is changed by a radio button
how to return value inside a dictionary which is changed by a radio button

Time:11-28

I created a dictionary with two keys, when selecting one of the keys, the dictionary items are updated, the problem is that I am not returning the selected value within the updated list. for example, when selecting 'male', and then 'Executed', I would like to receive 'Executed' as a value

import PySimpleGUI as sg

genero = {
    'male': ['Required','Executed'],
    'female': ['Required', 'Performed']
  }

layout = [
          [sg.Radio('male', "RADIO1", default=False, key="-IN1-")],
          [sg.Radio('female', "RADIO1", default=False, key="-IN2-")],
          [sg.Listbox(genero.keys(), size=(30, 3), enable_events=True, key='-PART-')],
          [sg.Push(),sg.Button('GENERATE'), sg.Exit("Exit")]                 
            ]


window = sg.Window("GENERATE PETITION", layout)


while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == "Exit":
        break
    elif values["-IN1-"] == True:
        window['-PART-'].update(genero['male'])
    elif values["-IN2-"] == True:
        window['-PART-'].update(genero['female'])
    elif event == 'GENERATE':
        print('-PART-')
window.close()

print(event,values)

atualmente está retornando assim: Exit {'-IN1-': True, '-IN2-': False, '-PART-': []}

CodePudding user response:

There's programming logic issue in the event loop, it will be better for all the cases starts with the event decision, not the value(s) decision. In your code, the case for the event GENERATE will be never executed after any one of the Radio element clicked.

import PySimpleGUI as sg

genero = {
    'male': ['Required','Executed'],
    'female': ['Required', 'Performed']
  }

layout = [
          [sg.Radio('male', "RADIO1", default=False, enable_events=True, key="-IN1-")],
          [sg.Radio('female', "RADIO1", default=False, enable_events=True, key="-IN2-")],
          [sg.Listbox(genero.keys(), size=(30, 3), enable_events=True, key='-PART-')],
          [sg.Push(),sg.Button('GENERATE'), sg.Exit("Exit")]
            ]


window = sg.Window("GENERATE PETITION", layout)


while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED or event == "Exit":
        break

    elif event in ('-IN1-', '-IN2-'):
        if values["-IN1-"] == True:
            window['-PART-'].update(genero['male'])
        else:
            window['-PART-'].update(genero['female'])

    elif event == 'GENERATE':
        selections = values['-PART-']
        if selections:
            print(selections[0])
        else:
            print('Nothing selected !')

window.close()

  • Related