Using the labels parameter in gui.DlgFromDict

Hi there, I am running the following code to create a dialog box:

#present a dialogue to change params
dlg = gui.DlgFromDict(dictionary=expInfo, 
    title = 'Gabor Motion Contrast',
    order = ['participantID', 'condition', 'practice'],
    labels = {'participantID': 'Participant ID', 'condition': 'Condition','practice': 'Practice'},
    tip = {'participantID': 'The unique identifer for each participant.', 'condition': 'The current experimental condition.'})

if dlg.OK:
    toFile('lastParams.pickle', expInfo) 
else:
    core.quit() 

I am receiving the following error:

Traceback (most recent call last):
  File "C:\XXXXXXXXX\GaborContrast.py", line 90, in <module>
    tip = {'participantID': 'The unique identifer for each participant.', 'condition': 'The current experimental condition.'})
  File "C:\Program Files\PsychoPy3\lib\site-packages\psychopy\gui\qtgui.py", line 489, in __init__
    self.show()
  File "C:\Program Files\PsychoPy3\lib\site-packages\psychopy\gui\qtgui.py", line 498, in show
    self.dictionary[thisKey] = self.inputFieldTypes[thisKey](self.data[n])
KeyError: 'participantID'

##### Experiment ended. #####

The dialog box opens, and all the fields are rendered correctly, including the labels. Once I click “OK” this exception is thrown.

If I do not include the labels argument the programme works without error.

Has anyone successfully used the labels argument before? I cannot find an example of how to do this successfully.

TIA

Hi @CatK2020 , I think the issue is that “participantID” does not exist in the dictionary expInfo that has been passed to the gui function. Is this a separate GUI that you have coded yourself, as opposed to one presented using Builder? If so, create a new dictionary to store your extra info:

newInfo = {"participantID": 'Participant ID', 'condition': 'Condition','practice': 'Practice'}
dlg = gui.DlgFromDict(dictionary=newInfo, 
    title = 'Gabor Motion Contrast',
    order = ["participantID", 'condition', 'practice'],
    labels = {"participantID": 'Participant ID', 'condition': 'Condition','practice': 'Practice'},
    tip = {"participantID": 'The unique identifer for each participant.', 'condition': 'The current experimental condition.'})

if dlg.OK:
    toFile('lastParams.pickle', newInfo) 
else:
    core.quit() 

Hi @dvbridges thanks for your reply!

Yes this is a separate script, not created via builder.

If I do rename the newInfo dictionary to expInfo, the same error occurs.

“participantID” is defined and instantiate here:

expInfo = {“participantID”: ‘Participant ID’, ‘condition’: ‘Condition’,‘practice’: ‘Practice’}

and then passed in to the gui.DlgFromDict method here:

dlg = gui.DlgFromDict(dictionary=expInfo,

Do you have a working example I could follow?

@CatK2020 yes if I run the code above on its own, it runs fine. Just make sure you are passing newInfo as your dictionary to the dictionary parameter.