2016-03-28 9 views
0

Das sind Hausaufgaben: Versuchen, einen Stroop-Test in Python zu erstellen. Ich habe den größten Teil des Codes bereits geschrieben, aber ich habe Probleme, die passenden Reize nach dem Zufallsprinzip zwischen verschiedenen und gleichen Reizen zu wechseln, wenn ich den "nächsten" Knopf drücke.Stroop-Test in Python funktioniert nicht richtig.

Hier ist mein Code so weit:

# 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() 

Antwort

1

Der Schlüssel zu Ihrem Problem wird diese Zeile in der Prozedur nächsten Schaltfläche Ändern:

word, color = stimulus(random.choice((True, False))) 

Allerdings gibt es einige Fehler in Ihrem Programm damit es nicht richtig funktioniert. (Auch neu definiert einige Einbauten.) Ich habe es unten überarbeitet:

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

Vielen Dank für irgendwelche und alle Hilfe, cdlane! – Mardux

Verwandte Themen