psychopy.gui - create dialogue boxes

DlgFromDict

class psychopy.gui.DlgFromDict(dictionary, title='', fixed=None, order=None, tip=None, screen=-1, sortKeys=True, copyDict=False, labels=None, show=True, sort_keys=None, copy_dict=None)[source]

Creates a dialogue box that represents a dictionary of values. Any values changed by the user are change (in-place) by this dialogue box.

Parameters:
  • dictionary (dict) – A dictionary defining the input fields (keys) and pre-filled values (values) for the user dialog

  • title (str) – The title of the dialog window

  • labels (dict) – A dictionary defining labels (values) to be displayed instead of key strings (keys) defined in dictionary. Not all keys in dictionary need to be contained in labels.

  • fixed (list) – A list of keys for which the values shall be displayed in non-editable fields

  • order (list) – A list of keys defining the display order of keys in dictionary. If not all keys in dictionary` are contained in order, those will appear in random order after all ordered keys.

  • tip (list) – A dictionary assigning tooltips to the keys

  • screen (int) – Screen number where the Dialog is displayed. If -1, the Dialog will be displayed on the primary screen.

  • sortKeys (bool) – A boolean flag indicating that keys are to be sorted alphabetically.

  • copyDict (bool) – If False, modify dictionary in-place. If True, a copy of the dictionary is created, and the altered version (after user interaction) can be retrieved from :attr:~`psychopy.gui.DlgFromDict.dictionary`.

  • labels – A dictionary defining labels (dict values) to be displayed instead of key strings (dict keys) defined in dictionary. Not all keys in `dictionary´ need to be contained in labels.

  • show (bool) – Whether to immediately display the dialog upon instantiation. If False, it can be displayed at a later time by calling its show() method.

  • e.g.

  • ::

    info = {‘Observer’:’jwp’, ‘GratingOri’:45,

    ’ExpVersion’: 1.1, ‘Group’: [‘Test’, ‘Control’]}

    infoDlg = gui.DlgFromDict(dictionary=info,

    title=’TestExperiment’, fixed=[‘ExpVersion’])

    if infoDlg.OK:

    print(info)

    else:

    print(‘User Cancelled’)

  • above (In the code) –

  • values (the contents of info will be updated to the) –

  • box. (returned by the dialogue) –

  • OK) (If the user cancels (rather than pressing) –

:param : :param then the dictionary remains unchanged. If you want to check whether: :param the user hit OK: :param then check whether DlgFromDict.OK equals: :param True or False: :param See GUI.py for a usage demo: :param including order and tip (tooltip).:

show()[source]

Display the dialog.

Dlg

class psychopy.gui.Dlg(title='PsychoPy Dialog', pos=None, size=None, style=None, labelButtonOK=' OK ', labelButtonCancel=' Cancel ', screen=-1)[source]

A simple dialogue box. You can add text or input boxes (sequentially) and then retrieve the values.

see also the function dlgFromDict for an even simpler version

Example

from psychopy import gui

myDlg = gui.Dlg(title="JWP's experiment")
myDlg.addText('Subject info')
myDlg.addField('Name:')
myDlg.addField('Age:', 21)
myDlg.addText('Experiment Info')
myDlg.addField('Grating Ori:',45)
myDlg.addField('Group:', choices=["Test", "Control"])
ok_data = myDlg.show()  # show dialog and wait for OK or Cancel
if myDlg.OK:  # or if ok_data is not None
    print(ok_data)
else:
    print('user cancelled')
addField(label='', initial='', color='', choices=None, tip='', required=False, enabled=True)[source]

Adds a (labelled) input field to the dialogue box, optional text color and tooltip.

If ‘initial’ is a bool, a checkbox will be created. If ‘choices’ is a list or tuple, a dropdown selector is created. Otherwise, a text line entry box is created.

Returns a handle to the field (but not to the label).

addFixedField(label='', initial='', color='', choices=None, tip='')[source]

Adds a field to the dialog box (like addField) but the field cannot be edited. e.g. Display experiment version.

show()[source]

Presents the dialog and waits for the user to press OK or CANCEL.

If user presses OK button, function returns a list containing the updated values coming from each of the input fields created. Otherwise, None is returned.

Returns:

self.data

validate()[source]

Make sure that required fields have a value.

fileOpenDlg()

psychopy.gui.fileOpenDlg(tryFilePath='', tryFileName='', prompt='Select file to open', allowed=None)[source]

A simple dialogue allowing read access to the file system.

Parameters:
tryFilePath: string

default file path on which to open the dialog

tryFileName: string

default file name, as suggested file

prompt: string (default “Select file to open”)

can be set to custom prompts

allowed: string (available since v1.62.01)

a string to specify file filters. e.g. “Text files (*.txt) ;; Image files (*.bmp *.gif)” See https://www.riverbankcomputing.com/static/Docs/PyQt4/qfiledialog.html #getOpenFileNames for further details

If tryFilePath or tryFileName are empty or invalid then current path and empty names are used to start search.

If user cancels, then None is returned.

fileSaveDlg()

psychopy.gui.fileSaveDlg(initFilePath='', initFileName='', prompt='Select file to save', allowed=None)[source]

A simple dialogue allowing write access to the file system. (Useful in case you collect an hour of data and then try to save to a non-existent directory!!)

Parameters:
initFilePath: string

default file path on which to open the dialog

initFileName: string

default file name, as suggested file

prompt: string (default “Select file to open”)

can be set to custom prompts

allowed: string

a string to specify file filters. e.g. “Text files (*.txt) ;; Image files (*.bmp *.gif)” See https://www.riverbankcomputing.com/static/Docs/PyQt4/qfiledialog.html #getSaveFileName for further details

If initFilePath or initFileName are empty or invalid then current path and empty names are used to start search.

If user cancels the None is returned.


Back to top