Basic Tkinter/GUI Questions
Question: I'm dirtying my hands with Python's Tkinter, specifically learning OOP for
GUIs and have a few questions.

  1. When constructing a class like so:
    
        class Keylogger(Frame):
        def __init__(self, master=None):
            frame = Frame.__init__(self, master)
            text = Text(frame, width=20, height=5)
            ...
    
    test = KeyLogger()
    test.mainloop()
    

     * What's the purpose of the default master argument being `None`? Normally I've been learning to specify a master widget like `root = Tk()`. Are there times a master widget is not needed/wanted?

     * I also noticed that I did not need to `pack()` my KeyLogger object, regardless if it had a master or not. I did not even pack the my Frame object. Why is that?

  2. I then noticed that if I changed the code in my `__init__` method like so:
    
        def __init__(self, master=None):
        Frame.__init__(self, master) # no more assignment statement
        text = Text(self, width=20, height=5) # first constructor arg. changed
        ...
    
    test = KeyLogger()
    test.mainloop()
    

     * I now have to `pack()` my KeyLogger object, regardless if a master widget was present. What changed when I removed the assignment statement and just called `Frame.__init__()`?
     * Should I call the `pack()` method inside `__init__()` or outside the class? It works both ways but I'm guessing there are some pros/cons.

**Thank you!**


Answer: 1\. In this code, since **init** returns nothing, you just initialize a Text
with None, like

    
    
     text = Text(None, width=20, height=5)
    

And you need to use `text.pack()` inside `__init__()` to make it take effect.

The master is used as master widget. The widget without it is its own master
widget. That's why there are times we don't give it a master.

2.

In this code, you initialize a Text with a Frame instance by giving it a self
parameter. You need to pack both inside `__init__()` and in the main process
to make them both take effect. Like

    
    
    from Tkinter import *
    
    class KeyLogger(Frame):
        def __init__(self, master=None):
    #         frame = Frame.__init__(self, master)
            Frame.__init__(self, master)
    #         text = Text(frame, width=20, height=5)
            text = Text(self, width=20, height=5)
            text.pack()
    
    test = KeyLogger()
    test.pack()
    test.mainloop()
    

If this helps, please confirm it as an answer so that someone else could find
it quickly. Thanks!

