首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tkinter全局变量错误

Tkinter全局变量错误
EN

Stack Overflow用户
提问于 2021-08-10 01:21:36
回答 1查看 27关注 0票数 0

UnboundLocalError:赋值前引用的局部变量“”userScore“”

UnboundLocalError:赋值前引用的局部变量“”comScore“”

我已经尝试将它们声明为全局变量,并将它们作为参数传递(有效,但每次都会重置分数)。下面是我的代码:

代码语言:javascript
复制
userScore = 0
comScore = 0
options = [[comRock, 'Rock'], [comPaper, 'Paper'], [comScissors, 'Scissors']]

# def start():
#    startFrame.pack_forget()
#    visualFrame.pack()
#    controlFrame.pack()


def play(img, userIn):
    comIn = random.choice(options)
    Label(visualFrame, image=img, borderwidth=0, highlightthickness=0).grid(row=0, column=0)
    Label(visualFrame, image=comIn[0], borderwidth=0, highlightthickness=0).grid(row=0, column=2)
    Label(visualFrame, image=vsImg, borderwidth=0, highlightthickness=0).grid(row=0, column=1)

    if userIn == comIn[1]:
        Label(visualFrame, image=tieImg, borderwidth=0, highlightthickness=0).grid(row=0, column=0, columnspan=3)
    elif (userIn == "Rock" and comIn[1] == "Scissors") or (userIn == "Scissors" and comIn[1] == "Paper") \
            or (userIn == "Paper" and comIn[1] == "Rock"):
        Label(visualFrame, image=wonImg, borderwidth=0, highlightthickness=0).grid(row=0, column=0, columnspan=3)
        userScore += 1
    else:
        Label(visualFrame, image=lostImg, borderwidth=0, highlightthickness=0).grid(row=0, column=0, columnspan=3)
        comScore += 1

    Label(visualFrame, text=str(userScore) + ':' + str(comScore)).place(x=295, y=205)
EN

回答 1

Stack Overflow用户

发布于 2021-08-10 01:49:20

紧跟在函数定义之后声明userScore global

代码语言:javascript
复制
def play(img, userIn):
    global userScore
    ...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68720103

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档