2016-03-28 23 views
0

これは宿題です:PythonでStroopテストを作成しようとしています。私はすでにコードのほとんどを書いていますが、一致する刺激を作る際に問題があります。 「次の」ボタンを押すと、ランダムに異なる刺激を切り替えます。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() 

答えて

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() 
+1

何かすべてのヘルプのために、cdlane! – Mardux

関連する問題