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'未定义“Tkinter - Python 3 - 将增量计数器添加到类窗口中?
”DisplayButton“,是我放置的按钮,用于显示值。没有更多,但我收到此错误消息。
如果我删除了类,只需运行该代码,与单一窗口,该代码工作正常。
任何帮助将不胜感激!
@ Pat6089一秒 – furkle 2014-10-31 22:37:42
@ Pat6089我上面更新了 - 一切正常,因为它应该,因此,它是为你的类的作品相互沟通更容易了很多,我做到了。请让我知道,如果你有任何问题。 – furkle 2014-10-31 22:56:29
@ pat6089如果这是有帮助的,请不要忘记使用复选标记选择这个答案。 – furkle 2014-11-01 03:48:44