python tkinter 編寫心理學試驗程序干擾任務之四則運算 psychopy

在訓練任務與回憶任務之間,要求被試作4分鐘的四則運算,包括20之內的加法、減法和乘法,被試須要輸入運算結果。不管被試是否計算正確,皆進入下一題。4分鐘的時間到後,結束干擾任務。python

考慮到要求被試輸入,用psychopy顯得繁瑣,使用了tkinter來作顯示界面。dom

tkinter是python的自帶模塊,安裝psychopy的計算機能夠直接import tkinter來使用它。oop

 

# -*- coding: utf-8 -*-
"""
Created on Sun Aug 28 13:51:46 2016

@author: zbg
4分鐘的四則運算
被試輸入運算結果
不管是否正確,則進入下一題
"""
from Tkinter import *  #引入模塊
import random
from time import time as gettime


#實驗階段
phase = "pre"
timestart = 0
timelimit = 240 
s = ''
ans = ''

def generate():
    a = random.randint(1, 20)
    b = random.randint(1, 20)
    op = ['+', '-', '*'][random.randint(0, 2)]
    s = `a` + op + `b`
    ans = `eval(s)`
    if ans < 0:
        s = `b` + op + `a`
        ans = `eval(s)`
    return (s + '=?', ans)
    
def submit():
    global timestart, s, ans, phase, timelimit
    
    #print gettime() - timestart
    if u.get() == '':
        return
    
    if phase == "pre":
        button.config(text = u"提交")
        s, ans = generate()
        label.config(text = s)
        u.set('')
        timestart = gettime()
        phase = "run"
        
    elif phase =="run":
        if gettime() - timestart > timelimit:
            top.destroy()
            return
            
        s, ans = generate()
        label.config(text = s)
        u.set('')

top = Tk()
top.update_idletasks()
top.geometry("800x600")
label=Label(top,text= u'請完成接下來出現的計算題', font='Helvetica -46 bold')
label.pack(fill=X, expand=1)

u = StringVar()
u.set(u"input answer here")
ent = Entry(top, textvariable=u, font='Helvetica -46 bold')
ent.pack()

button = Button(top, text=u"開始", command=submit, default='active', font='Helvetica -46 bold')
button.pack(expand=1)

#top.protocol("WM_DELETE_WINDOW", submit)

top.mainloop()

#實驗結束階段
top2 = Tk()
top2.geometry("600x400")
label=Label(top2,text= u'運算部分實驗結束了', font=u'微軟雅黑 -46 bold')
label.pack(fill=X, expand=1)
top2.mainloop()

 

psychopy 定作實驗程序 https://item.taobao.com/item.htm?spm=a230r.1.14.6.Q6E2OW&id=530690095131&ns=1&abbucket=15#detailcode

相關文章
相關標籤/搜索