[Tkinter-discuss] adjust the size of two canvas when I resize

Michael Lange klappnase at web.de
Sun Jun 20 11:25:36 CEST 2010


Hi Zizou,

On Sun, 20 Jun 2010 04:06:45 +0200
zizou afix <ziz.root at gmail.com> wrote:

> Hi,
> i'm a new user of tkinter,
> I would like to adjust my canevas to the main window when a resize it.
> i've just find this on the web :
> http://infohost.nmt.edu/tcc/help/pubs/tkinter/layout-mgt.html#root-resize
> but in my case that don't work
> 
> this is my code :
> 2 canvas and 2 scrollbars
> it runs but it does not fit at the window
> if someone can help me, very very thanks
> 
> #####################################################################################
> from Tkinter import *
> 
> class Application(Frame):
>     def __init__(self, master=None):

First we should find out which widget(s) actually does not expand when
the window is resized. To do so it may be helpful to change the
background color of the Frame surrounding the two Canvases :

>         Frame.__init__(self, master, bg='yellow')
>         self.grid(sticky=N+S+E+W)
>         self.canCompose()

When you run this slightly modified example you will see that it is
actually the Frame that does not get resized, not the Canvases.
When I change the line

        self.grid(sticky=N+S+E+W)

into

        self.pack(fill='both', expand=1)

the Frame will expand as expected when the window is resized. So we see
that the self.grid(...) command is the problematic part. In fact in
your example the Frame expands vertically, but not horizontally, but
why? The explanation is a few lines below:

> 
>     def canCompose(self):
>         top=self.winfo_toplevel()
>         top.rowconfigure(0, weight=1)
>         top.columnconfigure(1, weight=1)

Here you tell the application window to expand row 0 and column 1 when
the window is resized, but a few lines above you put your Frame into
row 0 and column 0 (yet not explicitely, but if you do not define grid
cells, the grid manager will begin automagically with row 0 and col. 0).
So the solution is to change either 

    self.grid(sticky=N+S+E+W)   into 
    self.grid(row=0, column=1, sticky=N +S +E +W)
or
    top.columnconfigure(1, weight=1)   into
    top.columnconfigure(0, weight=1)

I hope this helps

Michael




More information about the Tkinter-discuss mailing list