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.

Share:
25,104
fozbstuios
Author by

fozbstuios

Updated on July 09, 2022

Comments

  • fozbstuios
    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
    fozbstuios over 10 years
    It should be called parent. I will except as soon as SO lets me.
  • fozbstuios
    fozbstuios over 10 years
    also 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
    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 in a=Application(), self will point to a. If def __init__() does not have =None, the master argument will become mandatory
  • Sandeep Prasad Kushwaha
    Sandeep Prasad Kushwaha over 4 years
    can i use parent instead of master=None?
  • Saullo G. P. Castro
    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 doing master=your_parent_object