What is the purpose of master and master=none in an init function in python?
25,104
Your Application
class is a subclass of tk.Frame
. The master
parameter is the parent widget and it is an optional parameter (by default it is none
) that will be passed to the new instance of Application
class when it is initialized.
Take a look here for more info.
Author by
fozbstuios
Updated on July 09, 2022Comments
-
fozbstuios almost 2 years
I would assume the the reason is similar to self which i get.Why is master there? Also, why is it sometimes master and sometimes master = none?
e.g
class Application(tk.Frame): def __init__(self, master=None): tk.Frame.__init__(self, master) self.grid() self.createWidgets()
from Tkinter 8.5 reference: a GUI for Python by John W. Shipman. Also, the doc uses python2 while I will be using python3. Do I need master? More problems I tried adding custom arg after master and it said I can't add a non-default arg after a default, how should I fix? doesn't the default arg self have to be first?
def __init__(self, master=None,inputDict): tk.Frame.__init__(self, master) self.grid(sticky=tk.N+tk.S+tk.E+tk.W) self.createWidgets() def createWidgets(self,inputDict): top=self.winfo_toplevel() top.rowconfigure(0, weight=1) top.columnconfigure(0, weight=1) self.rowconfigure(0, weight=1) self.columnconfigure(0, weight=1) tempDict = {} for k,v in inputDict.items(): if 1<=v[0]<=3: tempDict[k] = static_keys(*v[1:]) elif v[0] ==4: tempDict[k] = dynamic_keys(*v[1:]) elif v[0]==5: tempDict[k] = key_labels(*v[1:]) return tempDict
-
fozbstuios over 10 yearsIt should be called parent. I will except as soon as SO lets me.
-
fozbstuios over 10 yearsalso when calling an init func to do I need to specify a master if the def does not have =0 behind it? what about self?
-
Saullo G. P. Castro over 10 years@fozbstuios sorry for the late answer...
self
is a pointer to the variable storing the new instance. So ina=Application()
, self will point toa
. Ifdef __init__()
does not have=None
, themaster
argument will become mandatory -
Sandeep Prasad Kushwaha over 4 yearscan i use parent instead of master=None?
-
Saullo G. P. Castro over 4 years@SandeepPrasadKushwaha If I understood you correctly, you want to put your
Application
object under another parent object that you have, right? In that case just indicate that my doingmaster=your_parent_object