2
I’m a beginner and I’m trying to learn the Tkinter framework, but I have a doubt now that is the use of super(), I know that the super is a way to inherit the class "main" to "subclasses", but in the example of Tkinter is used super() right in the class "main", follow the example code.
import tkinter as tk
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = 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=self.master.destroy)
self.quit.pack(side="bottom")
def say_hi(self):
print("hi there, everyone!")
root = tk.Tk()
app = Application(master=root)
app.mainloop()
Why use super() in this case?
I think that
Application
in this case uses as super classtk.Frame
. So the use of the wordsuper
within the class is always a reference totk.Frame
. When one class is not inheriting anything from another, the class declaration statement is justclass Nome:
– Lucas