UnboundLocalError:局部变量请告诉我我做错了什么,以及如何修复我的错误

2024-09-30 01:32:04 发布

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

我正在尝试编写我的游戏代码(我才刚开始使用Tkinter),但这一点不断出现

UnboundLocalError: local variable 'money' referenced before assignment

以下是我尝试的代码:

from tkinter import *

root = Tk()

money = 0

click = 1

def earnmoney():
    money = money + click

def showmonney():
    Total_money.configure(text = money)

root.title("MONEY")
root.geometry("1000x500")

app = Frame(root)
app.grid()

label = Label(app, text = "Press the button to earn money")
label.grid()

button1 = Button(app, text = "Earn money", command=earnmoney)
button1.grid()

button2 = Button(app, text = "Show money", command=showmonney)
button2.grid()

Total_money = Label(app)
Total_money.grid()  

root.mainloop()

Tags: 代码textappdefbuttonrootlabelgrid

热门问题