从列表中随机选择一个单词不会';t正常工作(Python/Tkinter)
您好,我正在处理一个颜色stroop,我可以用我的代码更改单词和单词的颜色,但我不能只在调用函数“next_selected”时更改单词,有人能帮我吗从列表中随机选择一个单词不会';t正常工作(Python/Tkinter),python,random,tkinter,Python,Random,Tkinter,您好,我正在处理一个颜色stroop,我可以用我的代码更改单词和单词的颜色,但我不能只在调用函数“next_selected”时更改单词,有人能帮我吗 def tick(): global doTick global sec if not doTick: return sec += 0.1 sec = round(sec, 1) ftest1.after(100, tick) time2Label.configure(tex
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()
stimultium()
在same=False
时无法选择新颜色,也无法将新选择保存在全局单词中。尝试一下:
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()
为什么你不能改变它?当你尝试时会发生什么?没什么,这个词不会出现。控制台知道它是否是正确的单词,但下一个随机单词的功能不起作用(对不起,我的英语)我只是不知道如何使用它,我是Python新手,你希望刺激能做什么我看这里是定义,你能展示你如何调用你的函数,你得到了什么,那你想要什么呢?非常感谢这非常有效!我的颜色stroop测试工作正常(知道:)