我正在學習Python中Tkinter的包,我不明白下面的代碼:Python的 - 引用變量不存在
import tkinter as tk
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.pack()
self.create_widgets()
def create_widgets(self):
self.hi_there = tk.Button(self)
self.hi_there["text"] = "Hello World\n(click me)"
self.hi_there["command"] = self.say_hi
self.hi_there.pack(side="top")
self.quit = tk.Button(self, text="QUIT", fg="red", command=root.destroy)
self.quit.pack(side="bottom")
def say_hi(self):
print("hi there, everyone!")
root = tk.Tk()
app = Application(master=root)
app.mainloop()
據我瞭解self
指的是類代碼時說: self.hi_there
我期望這個類中的全局變量必須先聲明。 hi_there
如何創建?
在__init__
方法中有什麼「master = None」的用法?如果我跳過=None
部分,那麼它會不會是相同的,因爲我做的是app = Application(master=root)
?
不知道你爲什麼認爲它們不存在。 –
它在'self.hi_there = tk.Button(self)'創建。 – Ryan
它被創建爲一個全局變量?這裏的自我意味着什麼?爲什麼不簡單地把「hi_there = tk.Button(self)」 – akerbeltz