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

W

W. Watson

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/>
 
F

Francesco Bochicchio

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

The obvious question is: why don't you run both and see what happens?

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.

Ciao
 
7

7stud

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/>


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.


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

petercable

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.

The container returned by Tk() (root) is passed into App and the frame
inside App (fm) is packed into it. It's not left empty and you won't
ever see it.

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?

For simple Tk applications, it's not necessary to create Frames to
pack inside the root container. However, more complicated GUIs will
often include multiple nested frames. Because each container can only
be managed by one geometry manager, multiple containers are the only
way to create complex interfaces.

Anyway, the answer is, it's not necessary to create a Frame in the
first example, as the initial call to Tkinter.Tk() will return the
root container, which can be used to pack any widget. In the first
example, the three buttons are packed directly into the root
container. In the second example, the three buttons are packed into a
frame which is in turn packed into the root container.

Happy to help,

Pete Cable
(formerly of Yuba City, CA)
 
W

W. Watson

I did run them both, but not simultaneously. They looked the same to me. I
should have probably captured both. I'll check for a small one somewhere.
 
W

W. Watson

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.
 
E

Eric Brunel

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.

(Please don't top-post... It makes the individual messages very hard to
read. Thanks.)

In fact, it's exactly the reverse: the root or the instances of Toplevel
have some properties that are not available to frames, for example the
ability to have a menu bar. As already said, frames are only used to group
widgets to do complicated layouts. In the example you gave, the frame is
unneeded, and I'd say it should not be there (the only thing it achieved
was just to confuse you...).

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top