Tkinter. Why the Need for a Frame, or no Frame?

W. Watson wolf_tracks at invalid.com
Sun Feb 17 07:31:47 EST 2008


Thanks very much. I'm somewhat new to this, but I would think that Frame 
might carry some properties not available to the root. If so, then there 
might be some advantage to it.

7stud wrote:
> On Feb 16, 8:40 pm, "W. Watson" <wolf_tra... at invalid.com> wrote:
>> The following two examples are from Grayson's book on Tkinter. He's making a
>> simple dialog with three buttons. In the first example, he does not use the
>> Frame class, but in the second he does. Doesn't the first example need a
>> container? What's the difference here?
>>
... snip
> 
> 
> Every Tkinter program is required to have a 'root window'.  A root
> window is a container in its own right.  To create a root window, you
> write:
> 
> root = Tk()
> 
> Then what's the point of using a frame in the second example?  None
> really--except to demonstrate that a frame is a container.  A frame is
> used to organize a group of widgets.  If you only have one frame, then
> that's not much different than having no frame.  However, if you have
> two frames, each frame can organize its widgets differently.
> 
> Note that you can write an even simpler Tkinter program:
> 
> import Tkinter as tk
> 
> b1 = tk.Button(text='Left')
> b2 = tk.Button(text='Center')
> b3 = tk.Button(text='Right')
> 
> b1.pack(side=tk.LEFT)
> b2.pack(side=tk.LEFT)
> b3.pack(side=tk.LEFT)
> 
> tk.mainloop()
> 
> Note that the program doesn't explicitly create a root window or a
> frame.  The program works because if you don't explicitly create a
> root window, Tkinter automatically creates a root window for you.
> Subsequently, if you create a widget and don't specify a parent
> container, Tkinter automatically adds the widget to the root window.
> 
> 
> On Feb 17, 1:29 am, Francesco Bochicchio <bock... at virgilio.it> wrote:
>> Anyway, Tk() already opens a frame, so in the first example the buttons
>> are created inside that frame, while in the second example two frames
>> are created: the one creaded by Tk() il left empty but you should see it
>> (maybe very small in a corner) if you run the program.
>>
> 
> That's incorrect.  In the second example, the frame specifies the root
> window as its parent, and the buttons specify the frame as their
> parent, so the buttons are inside the frame which is inside the root
> window.  You can easily prove that there's only one window by setting
> root's size to something large and specifying its background color as
> red--that way if root is a separate window hiding somewhere it will no
> longer go unnoticed:
> 
> from Tkinter import *
> 
> class App:
>      def __init__(self, master):
>          fm = Frame(master)
>          Button(fm, text='Left').pack(side=LEFT)
>          Button(fm, text='This is the Center button').pack(side=LEFT)
>          Button(fm, text='Right').pack(side=LEFT)
>          fm.pack()
> 
> root = Tk()
> 
> root.geometry('600x400')
> root.config(background='red')
> 
> root.option_add('*font', ('verdana', 12, 'bold'))
> root.title("Pack - Example 2")
> display = App(root)
> root.mainloop()
> 
> 

-- 
                          Wayne Watson (Nevada City, CA)

                        Web Page: <speckledwithStars.net>



More information about the Python-list mailing list