这是作业:尝试在python中创建Stroop测试。我已经编写了大部分代码,但是我在制作匹配的刺激方面遇到了麻烦 当我点击“下一步”按钮时,在不同和相同的刺激之间随机切换。
到目前为止,这是我的代码:
# Button, Label, Frame
from Tkinter import *
import random
def stimulus(same):
colors = ['red', 'blue', 'green', 'yellow', 'orange', 'purple']
word = random.choice(colors)
if same == True:
return (word, word)
else:
colors.remove(word)
color = random.choice(colors)
return (word, color)
# create label using stimulus
s = stimulus(same=True)
word, color = stimulus(True)
root = Tk()
label = Label(root, text=word, fg=color)
label.pack()
#create the window
def quit():
root.destroy()
closebutton = Button(root, text = 'close', command=quit)
closebutton.pack(padx=50, pady=50)
def next():
word, color = stimulus(True)
label.congig(text=word, fg=color)
label.update()
nextbutton = Button(root, text='next', comand=next)
nextbutton.pack()
root.mainloop()
答案 0 :(得分:1)
问题的关键是在下一步按钮处理程序中更改此行:
word, color = stimulus(random.choice((True, False)))
但是,您的程序中存在一些错误,使其无法正常工作。 (还有一些重新定义的内置插件。)我在下面重写了它:
import Tkinter # Button, Label, Frame
import random
COLORS = ['red', 'blue', 'green', 'yellow', 'orange', 'purple']
def stimulus(same):
colors = list(COLORS)
word = random.choice(colors)
if same:
return (word, word)
colors.remove(word)
color = random.choice(colors)
return (word, color)
def next_selected():
word, color = stimulus(random.choice((True, False)))
label.config(text=word, fg=color)
label.update()
def quit_selected():
root.destroy()
root = Tkinter.Tk()
# create the window
# create label using stimulus
word, color = stimulus(True)
label = Tkinter.Label(root, text=word, fg=color)
label.pack()
closebutton = Tkinter.Button(root, text='close', command=quit_selected)
closebutton.pack(padx=50, pady=50)
nextbutton = Tkinter.Button(root, text='next', command=next_selected)
nextbutton.pack()
root.mainloop()