Autoresize in Tkinter

Joseph Andrew Knapka jknapka at earthlink.net
Thu Aug 30 00:05:11 CEST 2001


Martin Franklin wrote:
> 
> Jonas Bengtsson wrote:
> >
> > Why doesn't the text resize with the frame in this code example?
> >
> > import Tkinter as Tk
> > root = Tk.Tk()
> > t=Tk.Text(root)
> > t.grid(sticky=Tk.NW+Tk.SE)
> > t.insert(Tk.END, "test")
> > root.mainloop()
> >
> > /Jonas B
> 
> I don't use the Tk gridder much perhaps someone else can answer the
> question...

An unfortunate "feature" of the grid manager is that
expansion is controlled on a per-column/per-row basis,
not on a per-component basis as in Java's GridBagLayout.
The "sticky" option says, "IF you have extra room in
your cell, expand to fill it." However, in order for
the cell to be able to expand -at all-, the -parent-
component must be configured to allow the cell's
row and column to expand. This is done by using
grid_rowconfigure() and grid_columnconfigure() on
the parent window to set the "weights" of the
rows and columns in the grid:

import Tkinter as Tk
root = Tk.Tk()

root.grid_rowconfigure(0,weight=1.0)
root.grid_columnconfigure(0,weight=1.0)

t=Tk.Text(root)
t.grid(sticky=Tk.NW+Tk.SE)
t.insert(Tk.END, "test")
root.mainloop()

Weights are floating-point values, and control the
proportion of extra horizontal or vertical space
allocated to a row or column. For example, if row 0
has weight 1.0 and row 1 has weight 0.5, row 0 will
expand twice as fast as row 1 as the parent window
stretches vertically. A row or column with a weight
of 0 (the default) -never- expands.

Personally, I think it would be nice if the weights
defaulted to 1.0 :)

Cheers,

-- 
# Joe Knapka
# "You know how many remote castles there are along the
#  gorges? You can't MOVE for remote castles!" - Lu Tze re. Uberwald
# Linux MM docs:
http://home.earthlink.net/~jknapka/linux-mm/vmoutline.html



More information about the Python-list mailing list