2016-05-09 4 views
-4

Hallo, ich arbeite an einer Arbeit über eine Farbe stroop, kann ich das Wort und die Farbe des Wortes mit meinem Code ändern, aber ich kann nicht nur das Wort ändern wenn meine Funktion "next_selected" aufgerufen wird, kann mir bitte jemand helfen?Die zufällige Wahl für ein Wort aus einer Liste funktioniert nicht richtig (Python/Tkinter)

def tick(): 
    global doTick 
    global sec 
    if not doTick: 
     return 
    sec += 0.1 
    sec = round(sec, 1) 
    ftest1.after(100, tick) 
    time2Label.configure(text=sec) 
    if sec == 60.0: 
     doTick = False 
     time2Label.config(text=sec) 
     label1.config(text=score, fg='black') 

def start(): 
    global doTick 
    doTick = True 
    label1.pack() 
    tick() 
    startbutton1.destroy() 

COLORS = ['blue','green','yellow','red'] 

def stimulus(same): 

    global word 
    colors = list(COLORS) 

    if same: 
     return (word) 

    colors.remove(word) 

    return (word) 

def next_selected(): 
    global word 
    word = stimulus(choice((True,False))) 

    label1.config(text=word) 
    label1.update() 
+1

Warum können Sie es nicht ändern? Was passiert, wenn Sie es versuchen? –

+0

Nichts, das Wort erscheint nicht. Die Konsole weiß, ob es das richtige Wort ist, aber die Funktion dafür erscheint das nächste zufällige Wort funktioniert nicht (Entschuldigung für mein Englisch) –

+0

Ich weiß einfach nicht, wie man es macht, ich bin neu auf Python –

Antwort

0

stimulus() versagt eine neue Farbe, wenn same=False, holen noch die neue Wahl im globalen word speichern. Probieren Sie es aus:

COLORS = ['blue', 'green', 'yellow', 'red'] 

word = COLORS[0] 

def stimulus(same): 

    global word 

    if same: # return the previous color 
     return (word) 

    colors = list(COLORS) 

    colors.remove(word) 

    word = choice(colors) # chose a different color & remember it 

    return word 

def next_selected(): 
    global word 
    word = stimulus(choice((True, False))) 

    label1.config(text=word) 
    label1.update() 
+0

Vielen Dank, das hat perfekt funktioniert! Meine Farbstroop-Test funktioniert weiß :) –

Verwandte Themen