Providing input
Objectives
- Be able to construct a simple Graphical User Interface to receive input at the beginning of program execution.
Typically when we run an experiment we would like it to be as close to identical for all participants (within a common group, at least) as possible. However, there are often aspects that are specific to a particular time at which you are executing your program. For example, you might want to provide a participant identifier in order to save the data in a particular location, or you might want to give a repeat number, or perhaps a condition number, etc.
A straightforward way to do this in psychopy is to build a simple GUI (Graphical User Interface) that is displayed when the program starts. The user can then input the required information, which is then available to the rest of the program.
We begin creating such a GUI by using psychopy.gui.Dlg()
.
After creating it, we add various elements to it before using its show
function.
Without any elements yet, it would look something like:
import psychopy.gui
gui = psychopy.gui.Dlg()
gui.show()
Let’s add a field so that the user can provide a subject ID and a run number. I’ll enter “p1000” as the subject ID and “1” as the run number.
import psychopy.gui
gui = psychopy.gui.Dlg()
gui.addField("Subject ID:")
gui.addField("Condition Num:")
gui.show()
print gui.data
[u'p1000', u'1']
As you can see, the gui.data
property holds a list where the items are the GUI elements, in the order they were added.
Tip
Don’t worry about the “u” characters in front of the quotation marks in the output. You can just treat them like regular strings.
Also note that information like the condition number is represented as a string, whereas we may want it as an integer.
It is straightforward to convert a string to an integer by using the int
function.
For example:
import psychopy.gui
gui = psychopy.gui.Dlg()
gui.addField("Subject ID:")
gui.addField("Condition Num:")
gui.show()
subj_id = gui.data[0]
cond_num = int(gui.data[1])
print subj_id, cond_num
p1000 1