Question

I was following the tutorial example here: http://www.youtube.com/watch?v=YCLTv6wh3jE

I modified the source code a bit (basically import Tkinter instead of from Tkinter import *)

#more complexed gui

import Tkinter

class Application(Tkinter.Frame):
    """a gui application with 3 buttons"""

    def __init__(self,master):
        """Initialize the Frame"""
        Tkinter.Frame.__init__(self,master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        """create 3 buttons that do nothing"""
        self.button1 = Tkinter.Button(self, text="this is the 1st button")
        self.button1.grid()

        self.button2 = Tkinter.Button(self)
        self.button2.grid()
        self.button2.configure(text="this is 2nd butt")

        self.button3 = Tkinter.Button(self)
        self.button3.grid()
        self.button3["text"] = "this is 3rd butt"

root = Tkinter.Tk()
root.title("some GUI")
root.geometry("1024x768")

app = Application(root)

root.mainloop()

I realize that if I take out the master from Tkinter.Frame.__init__(self,master), the code still runs, not only that, I can run app=Application(root) and create multiple triple buttons.

So my question is:

  1. what exactly does 'master' do?

  2. if I take out the master from __init__(self,master), it will gives me an error about not having enough arguments

  3. also, I don't need to use app=Application(root), I can just run Application(root) and that still makes root.mainloop() to work, so what's the purpose of that app = Application(root)?

Thanks

Was it helpful?

Solution

To answer your questions in order

  1. The master argument is the "parent widget" for each widget, and defaults to None for Tkinter.Frame. Note you pass self (i.e. the Frame) as master to each Button;

  2. That's not really a question, but if you change the definition to def __init__(self) then call app = Application(root) you will get a TypeError, as the root argument has nowhere to go; and

  3. The point of setting app = Application(root) is simply that you can then access it later. Your example is simple, with no instance methods to call outside __init__, but that may not always be the case.

To clarify on 1., note that

class Application(Tkinter.Frame):

means that all Application class instances inherit methods and attributes from Tkinter.Frame. This is not the same as the function arguments in e.g.

def __init__(self, master):

which means that Application.__init__ takes two arguments (the first, called self by convention in all instance methods, is the class instance itself and is usually passed implicitly). When you call

app = Application(root)

this relates to the __init__ definition, and (roughly) means

Application.__init__(app, root)

If __init__ doesn't have two arguments (self and master), you will therefore get a TypeError, as there are two arguments (the implicit instance self and explicit parent widget master) and Python only knows what to do with one of them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top