根据按键进行显示文本(提供用户实时反馈)

时间:2015-11-17 21:50:14

标签: python psychopy

我在心理上有一个刺激循环,显示4秒的图像,受试者做出二元决定。我想就他们做出的选择向他们提供反馈。

即:图像显示4秒,白色'是',并且在其两侧显示“否”。当用户按下键时,相应的字变为红色。如果他们按下另一把钥匙,它会切换。 4秒后,下一个图像显示白色字。

有谁知道怎么做这个?非常感谢任何建议。

1 个答案:

答案 0 :(得分:2)

您可以使用自定义代码组件执行此操作。将代码组件添加到例程中。

在"每个框架"选项卡添加以下代码:

private final DoubleProperty x1 = new SimpleDoubleProperty();
public final double getX1() { return x1.get(); }
public final void setX1(double value) { x1.set(value); }
public final DoubleProperty x1Property() { return x1; }

private final DoubleProperty x2 = new SimpleDoubleProperty();
public final double getX2() { return x2.get(); }
public final void setX2(double value) { x2.set(value); }
public final DoubleProperty x2Property() { return x2; }

private final DoubleProperty y1 = new SimpleDoubleProperty();
public final double getY1() { return y1.get(); }
public final void setY1(double value) { y1.set(value); }
public final DoubleProperty y1Property() { return y1; }

private final DoubleProperty y2 = new SimpleDoubleProperty();
public final double getY2() { return y2.get(); }
public final void setY2(double value) { y2.set(value); }
public final DoubleProperty y2Property() { return y2; }

private Line line;

@Override
public void start(Stage primaryStage) {
    Group group = new Group();
    primaryStage.setScene(new Scene(group, 400, 400));

    x1.set(100);
    y1.set(50);
    x2.set(200);
    y2.set(300);

    line = new Line(x1.get(), y1.get(), x2.get(), y2.get());
    line.startXProperty().bind(x1);
    line.startYProperty().bind(y1);
    line.endXProperty().bind(x2);
    line.endYProperty().bind(y2);
    line.setStrokeWidth(12); 
    line.setMouseTransparent(true);

    Anchor start = new Anchor(Color.BLUE, x1, y1);
    Anchor end = new Anchor(Color.YELLOW, x2, y2);

    group.getChildren().setAll(line, start, end);

    x1Property().addListener(o -> updateLine());
    x2Property().addListener(o -> updateLine());
    y1Property().addListener(o -> updateLine());
    y2Property().addListener(o -> updateLine());
    updateLine();

    primaryStage.show();
}

private void updateLine() {
    LinearGradient linearGradient = new LinearGradient(x1.get(), y1.get(), x2.get(), y2.get(), false, CycleMethod.REFLECT, new Stop(0,Color.RED),new Stop(1,Color.GREEN));
    line.setStroke(linearGradient);
}

private class Anchor extends Circle { 

    Anchor(Color color, DoubleProperty x, DoubleProperty y) {
        super(x.get(), y.get(), 10);
        setFill(color.deriveColor(1, 1, 1, 0.5));
        setStroke(color);
        setStrokeWidth(2);
        setStrokeType(StrokeType.OUTSIDE);

        x.bind(centerXProperty());
        y.bind(centerYProperty());
        enableDrag();
    }

    // make a node movable by dragging it around with the mouse.
    private void enableDrag() {
        final Delta dragDelta = new Delta();
        setOnMousePressed(mouseEvent -> {
            // record a delta distance for the drag and drop operation.
            dragDelta.x = getCenterX() - mouseEvent.getX();
            dragDelta.y = getCenterY() - mouseEvent.getY();
            getScene().setCursor(Cursor.MOVE);
        });
        setOnMouseReleased(mouseEvent -> {
            getScene().setCursor(Cursor.HAND);
        });
        setOnMouseDragged(mouseEvent -> {
            double newX = mouseEvent.getX() + dragDelta.x;
            if (newX > 0 && newX < getScene().getWidth()) {
                setCenterX(newX);
            }
            double newY = mouseEvent.getY() + dragDelta.y;
            if (newY > 0 && newY < getScene().getHeight()) {
                setCenterY(newY);
            }
        });
        setOnMouseEntered(mouseEvent -> {
            if (!mouseEvent.isPrimaryButtonDown()) {
                getScene().setCursor(Cursor.HAND);
            }
        });
        setOnMouseExited(mouseEvent -> {
            if (!mouseEvent.isPrimaryButtonDown()) {
                getScene().setCursor(Cursor.DEFAULT);
            }
        });
    }

    // records relative x and y co-ordinates.
    private class Delta { double x, y; }
}  

在&#34;开始实验&#34;选项卡添加以下代码:

if (t >=4) and (t < 8):
    if clear_keys:
        event.getKeys()
        clear_keys = False
    else:
        theseKeys = event.getKeys(keyList=['y', 'n'])
        if 'y' in theseKeys:
            Yes.color = 'red'
            No.color = 'white'
        elif 'n' in theseKeys:
            Yes.color = 'white'
            No.color = 'red'

您需要将脚本中的Yes和No对象更改为文本组件的名称。您还需要将数字4更改为图片的开始时间,将数字8更改为结束时间。

Here is a picture of my trial as an example.