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

7stud bbxx789_05ss at yahoo.com
Sun Feb 17 06:36:04 EST 2008


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?
>
> ==============5.1============
> from Tkinter import *
>
> class App:
>      def __init__(self, master):
>          Button(master, text='Left').pack(side=LEFT)
>          Button(master, text='Center').pack(side=LEFT)
>          Button(master, text='Right').pack(side=LEFT)
>
> root = Tk()
> root.option_add('*font', ('verdana', 12, 'bold'))
> root.title("Pack - Example 1")
> display = App(root)
> root.mainloop()
> ==============5.2==============
> 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.option_add('*font', ('verdana', 12, 'bold'))
> root.title("Pack - Example 2")
> display = App(root)
> root.mainloop()
> ===============================
>
> --
>             Wayne Watson (Watson Adventures, Prop., Nevada City, CA)
>
>               (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
>                Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet
>
>                "I know that this defies the law of gravity, but
>                 you see, I never studied law." -- Bugs Bunny
>
>                      Web Page: <www.speckledwithstars.net/>
>
> --
>                           Wayne Watson (Nevada City, CA)
>
>                         Web Page: <speckledwithStars.net>


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()





More information about the Python-list mailing list