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