Misc Tkinter questions

Fredrik Lundh fredrik at effbot.org
Sun Jan 7 17:37:51 EST 2001


David Allen wrote:
> 1.) How do you hide widgets?  (Is this wid.withdraw())

"withdraw" removes toplevel widgets, including the root
window (it's documented under "Toplevel Window Methods,
Visibility Methods")

To remove child widgets, use the geometry manager's "forget"
method ("pack_forget", "grid_forget", etc).

> 2.) How do you show widgets?  (undo 1.)

"deiconify" for toplevel widgets.

Repack/regrid for child widgets.

> 3.) How do you fix the size of a window?  I'm using
> a window that's a subclass of Tk, and I would like
> to know how to make it so that if the widgets within
> the window shrink, the window stays the same size.
> (i.e. put a solid minimum size on the window, which
> can grow)

"minsize" (see "Toplevel Window Methods, Window
Geometry Methods")

> 4.) Is it kosher to wid.destroy() something that's
> packed into something else and then pack something
> else in?

Yes (at least on the Tkinter level)

> 5.) Why can't you mix grid() and pack()?   (I ask
> because I like grid layouts in most situations, but
> I can't use fill=1, expand='x' when using grid)

"grid" and "pack" define what geometry manager algorithm
to use for the child widgets, and how to calculate the size
of the parent widget based on all child widgets.  If you can
figure out what Tkinter should do if you try to use multiple
layout algorithms in the same parent widget, let me know...

If your problem is the lack of a fill option for grid, it's much
easier: just use the "sticky" option instead:

    sticky="ew" is the same thing as fill="x"
    sticky="ns" is the same thing as fill="y"
    sticky="nsew" is the same thing as fill="both"

To make the grid widget expand, you need to set "weight"
values for the rows/columns you want to resize.  To do this,
use "grid_rowconfigure" and "grid_columnconfigure" (see
"The Grid Geometry Manager, Manager Methods")

> 6.) Is it better to create all of your widgets,
> then pack them in going from the inside out, or
> is it better to pack them as you create them?

It usually doesn't matter -- no matter what you do,
the geometry management algorithm won't run until
you get back to the main event loop.

However, when you use the pack manager, you should
pack important things first.  If the pack manager cannot
make the parent widget large enough to hold all child
widgets, it'll assign space in the packing order.

(for example, if you're packing a scrollable widgets and
a vertical scrollbar, you should pack the scrollbar first,
using side=RIGHT).

Hope this helps!

Cheers /F





More information about the Python-list mailing list