I was learning the tkinter package in Python and I didn't understand next code:
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()
As I understand self
refers to the class and when the code says self.hi_there
I expect a global variable in that class that has to be declared previously. How is hi_there
created?
Also what is the use of having "master=None" in the __init__
method? Wouldn't it be the same if I skip =None
part since I do app = Application(master=root)
?