FAQ
The compact form of pack behaves differently (and I believe incorrectly)
compared to the long form. The following two scripts demonstrate it, at
least on this interpreter:
Python 2.3.2 (#1, Oct 9 2003, 12:03:29)
[GCC 3.3.1 (cygming special)] on cygwin


# First script, no bug

# I want a canvas inside a frame inside a toplevel, with the canvas
# filling the toplevel even as I resize the window. This does it.

from Tkinter import *

root = Tk()
frame = Frame(root, bg="lightBlue")
frame.pack(side=TOP, expand=1, fill=BOTH)
canvas = Canvas(frame, bg="lightGreen")
canvas.pack(side=TOP, expand=1, fill=BOTH)

mainloop()





# Second script, bug

# I want a canvas inside a frame inside a toplevel, with the canvas
# filling the toplevel even as I resize the window. This doesn't do
# it, and instead makes the canvas a sibling of the frame, instead of
# a child. (Try resizing the toplevel and you'll see.) Why?

from Tkinter import *

root = Tk()
frame = Frame(root, bg="lightBlue").pack(side=TOP, expand=1, fill=BOTH)
canvas = Canvas(frame, bg="lightGreen").pack(side=TOP, expand=1, fill=BOTH)

mainloop()



Frank (filologo disneyano) http://www-lce.eng.cam.ac.uk/~fms27/

Search Discussions

  • Peter Otten at Nov 2, 2003 at 1:23 pm

    Frank Stajano wrote:
    The compact form of pack behaves differently (and I believe incorrectly)
    compared to the long form. The following two scripts demonstrate it, at
    There is no "compact form", see below.

    [...]
    # Second script, bug

    # I want a canvas inside a frame inside a toplevel, with the canvas
    # filling the toplevel even as I resize the window. This doesn't do
    # it, and instead makes the canvas a sibling of the frame, instead of
    # a child. (Try resizing the toplevel and you'll see.) Why?

    from Tkinter import *

    root = Tk()
    frame = Frame(root, bg="lightBlue").pack(side=TOP, expand=1, fill=BOTH)
    You are assigning the result of the pack() method, i. e. None, to frame
    here, and consequently Canvas is constructed with None instead of a Frame
    instance as the first argument.
    canvas = Canvas(frame, bg="lightGreen").pack(side=TOP, expand=1,
    fill=BOTH)

    mainloop()
    Peter
  • Michael Peuser at Nov 2, 2003 at 2:25 pm
    "Frank Stajano" <fms27 at cam.ac.uk

    Old Perl programmer, hmm... ;-)

    Kindly
    MichaelP
  • Frank Stajano at Nov 3, 2003 at 6:33 pm
    frame = Frame(root, bg="lightBlue").pack(side=TOP, expand=1, fill=BOTH)
    >
    You are assigning the result of the pack() method, i. e. None, to frame
    here, and consequently Canvas is constructed with None instead of a Frame
    instance as the first argument.
    Thanks for the explanation.

    I was under the obviously mistaken impression that the result returned by
    the pack method was the widget being packed, precisely so as to make this
    kind of compact invocation chaining possible. If this was never so, I am
    not sure where I got this idea from!

    One thing I know, it certainly was not from Perl, as the next poster
    insinuated. I wouldn't touch that with a barge pole.

    Perhaps it was from misreading other people's code. In the now distant past
    I did several fun years of Tcl/Tk/[incr Tcl] before switching to Python,
    but these are my first steps with Tkinter.

    Frank (filologo disneyano) http://www-lce.eng.cam.ac.uk/~fms27/

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedNov 2, '03 at 12:30p
activeNov 3, '03 at 6:33p
posts4
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase