tkinter, grid and resize

richard_chamberlain richard_chamberlain at ntlworld.com
Sat Jun 24 17:10:04 EDT 2000


Hi Bob,

Grid basically has an absence of fill, which is what pack uses to resize
widgets when their master resizes.

So you wouldn't use it to achieve what you want to do. The widgets fill
their allocated space according to the sticky options you pass but don't
resize when the master does. That is demonstrated in the following:

from Tkinter import *
root=Tk()
Text(root).grid(row=0,column=0)
Button(root,text='NS').grid(row=0,column=1,sticky=NS)
Button(root,text='N').grid(row=0,column=2,sticky=N)
Button(root,text='S').grid(row=0,column=3,sticky=S)
Button(root,text='EW').grid(row=1,column=0,sticky=EW)
Button(root,text='W').grid(row=2,column=0,sticky=W)
Button(root,text='E').grid(row=3,column=0,sticky=E)
root.mainloop()

You could rewrite your code in the following way:

from Tkinter import *
root=Tk()
frame=Frame(root)
frame2=Frame(root)
ys = Scrollbar(frame)
xs = Scrollbar(frame2)
dsp = Text(frame, yscrollcommand=ys.set, xscrollcommand=xs.set)
xs.config(orient='hor', command=dsp.xview)
ys.config(orient='vert', command=dsp.yview)
ys.pack(side=LEFT,expand=NO,fill=Y)
dsp.pack(side=LEFT,expand=YES,fill=BOTH)
frame.pack(side=TOP,expand=YES,fill=BOTH)
frame2.pack(side=TOP,fill=X)
Frame(frame2,width=16).pack(side=LEFT)
xs.pack(side=LEFT,expand=YES,fill=X)
root.mainloop()

achieves the same effect a little more verbosely. However I wrote this
pretty quickly so you should be able to rationalise it a little bit.

Richard

Bob van der Poel <bvdpoel at uniserve.com> wrote in message
news:39550101.AD8C7956 at uniserve.com...
>
> I'm having a problem getting widgets managed by grid to resize
> themselves when the screen size changes. This is easy to do using the
> pack manager and 'expand', however 'expand' doesn't exist in grid and
> the docs seem to indicate that the 'news' sticky option should do the
> same. The following snippet creates a text widget with scrollbars. If
> you resize the main window, the widgets do not resize....
>
> from Tkinter import *
>
> root=Tk()
>
> ys = Scrollbar(root)
> xs = Scrollbar(root)
>
> dsp = Text(root, yscrollcommand=ys.set, xscrollcommand=xs.set)
> xs.config(orient='hor', command=dsp.xview)
> ys.config(orient='vert', command=dsp.yview)
> xs.grid(row=1, column=1, sticky=E+W)
> ys.grid(row=0, column=0, sticky=N+S)
> dsp.grid(row=0, column=1, sticky=N+S+E+W)
>
> root.mainloop()
>
> What am I don't wrong? I've tried to insert columnspan stuff and played
> with the weight options--but I don't seem to be getting it.
>
> Thanks.
>
> --
>    __
>   /  )      /         Bob van der Poel
>  /--<  ____/__        bvdpoel at uniserve.com
> /___/_(_) /_)         http://users.uniserve.com/~bvdpoel
>





More information about the Python-list mailing list