【发布时间】:2014-10-31 21:48:47
【问题描述】:
class AppetiserClass():
root = Tk()
root.title("Appetiser Page")
root.geometry("1920x1080")
meal1 = 0
def plus1():
global meal1
meal1 = meal1 + 1
DisplayButton["text"]=str(meal1)
return
def neg1():
global meal1
meal1 = meal1 + 1
DisplayButton["text"]=str(meal1)
return
app = Frame(root)
app.grid()
Label(app, text = "", width = 75, height = 20).grid(row = 1, column = 0, sticky = N)
DisplayButton = Button(app, text = meal1)
DisplayButton.grid(column = 1, row = 2, sticky = W)
DisplayButton.config(height = 10, width = 10 )
Plus1Button = Button(app, text = "+1", command=plus1, bg="green")
Plus1Button.grid(column = 2, row = 2, sticky = W)
Plus1Button.config(height = 10, width = 10 )
Neg1Button = Button(app, text = "-1", command=neg1, bg="green")
Neg1Button.grid(column = 3, row = 2, sticky = W)
Neg1Button.config(height = 10, width = 10 )
root.mainloop()
我遇到的问题是我已经为我的全局变量(meal1,为 0)设置了一个值,但是当我按下 +1 或 -1 按钮时,“DislpayButton”上没有显示一个值,并且我收到这条消息: "NameError: 全局名称 'DisplayButton' 未定义"
“DisplayButton”,是我放置的一个按钮,用于显示一个值。仅此而已,但我收到此错误消息。
如果我删除类,然后只在单个窗口中运行这段代码,代码就可以正常工作。
任何帮助将不胜感激!
【问题讨论】:
标签: python class tkinter counter increment