[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

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

wolf_tracks

2/17/2008 3:41:00 AM

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>
9 Answers

Francesco Bochicchio

2/17/2008 8:30:00 AM

0

On Sat, 16 Feb 2008 19:40:51 -0800, W. Watson wrote:

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

7stud --

2/17/2008 11:36:00 AM

0

On Feb 16, 8:40 pm, "W. Watson" <wolf_tra...@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...@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()


petercable@gmail.com

2/17/2008 11:47:00 AM

0

On Feb 17, 9:29 am, Francesco Bochicchio <bock...@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.

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.

On Sat, 16 Feb 2008 19:40:51 -0800, W. Watson 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?

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)

wolf_tracks

2/17/2008 12:25:00 PM

0

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.

Francesco Bochicchio wrote:
> On Sat, 16 Feb 2008 19:40:51 -0800, W. Watson wrote:
>
>> 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
> -----
> FB

--
Wayne Watson (Nevada City, CA)

Web Page: <speckledwithStars.net>

wolf_tracks

2/17/2008 12:32:00 PM

0

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

Eric Brunel

2/18/2008 10:53:00 AM

0

On Sun, 17 Feb 2008 13:31:47 +0100, W. Watson <wolf_tracks@invalid.com>
wrote:
> 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
--
python -c "print ''.join([chr(154 - ord(c)) for c in
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"

uNmaivirumbi

7/23/2010 8:51:00 PM

0

On Jul 23, 2:35 pm, arah <arah1...@gmail.com> wrote:
> My dear Doug,
>
> I didn't add anything, those are quoted directly from the bible.
>
> Why blame me, blame those who wrote the bible.

Wy blame people for showing Mohammad never heard anything from God
directly. It is fake. It was not even from an angel!

uNmaivirumbi

7/25/2010 3:30:00 PM

0

On Jul 25, 10:19 am, arah <arah1...@gmail.com> wrote:
> Baqarah 2:62
>


Mohammad spoke to the devil and not god. He brought piece (pieces of
bodies)! Not peace

koolfireiv

7/25/2010 7:00:00 PM

0

On Jul 24, 8:34 pm, arah <arah1...@gmail.com> wrote:
> My dear Doug
>
> Learn how Prophet Muhammad treated a blind beggar who is Jews
>
> In the corner of the marketplace of Madinah, was a blind Jewish
> beggar, who would cry out daily to all those who came near him," O my
> brothers, don't be near Muhammad! He's a lunatic, he's a liar, a
> sorcerer! If you are to be close to him, you will be influenced by
> him!" No matter whoever approached him, the blind Jewish beggar will
> definitely not waste his chance to incite them to hate the Prophet
> Muhammad, Sallalahu Alaihi Wassallam (peace be upon him)! All kinds of
> curses would spew forth from the beggar's lips even though he had
> never known the Prophet personally!

Any idea where blasphemy laws came from...the blind Jewish beggar must
be asking for alms,he is blind,you want us to believe he had no
brains.