从列表中随机选择一个单词不能正常工作(Python/Tkinter)

2024-04-19 20:58:14 发布

您现在位置:Python中文网/ 问答频道 /正文

你好,我正在做一个彩色屏幕的工作,我可以用我的代码改变单词和单词的颜色,但是我不能只在调用我的函数“next\u selected”时改变单词,有人能帮我吗?你知道吗

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

Tags: textconfigreturnifdefsec单词global
1条回答
网友
1楼 · 发布于 2024-04-19 20:58:14

stimulus()无法在same=False时选择新颜色,也无法在全局word中保存该新选择。试一试:

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

相关问题 更多 >