Resizing widgets in text windows

D

deacon.sweeney

Hi, I've been searching for a .resize()-like function to overload much
like can be done for the delete window protocol as follows:

toplevel.protocol("WM_DELETE_WINDOW", callback)

I realize that the pack manager usually handles all of the resize
stuff, but I've found an arrangement that the pack manager fails for.
That is, if one embeds a canvas into a window created inside a text
widget, then resize the text widget (via its container), the canvas and
its container windows do not resize. So I need to resize the window
that the canvas is embedded in. The most obvious way of doing this
would be as above, but there does not seem to be an equivalent to the
"WM_DELETE_WINDOW" protocol for resizing.

Any help would be greatly appreciated.

Deacon Sweeney
 
J

James Stroud

Hi, I've been searching for a .resize()-like function to overload much
like can be done for the delete window protocol as follows:

toplevel.protocol("WM_DELETE_WINDOW", callback)

I realize that the pack manager usually handles all of the resize
stuff, but I've found an arrangement that the pack manager fails for.
That is, if one embeds a canvas into a window created inside a text
widget,

Your meaning here is unclear. How is it possible to have "a window
created inside a text widget"?
then resize the text widget (via its container), the canvas and
its container windows do not resize. So I need to resize the window
that the canvas is embedded in.

Try the Toplevel.wm_geometry() function.
The most obvious way of doing this
would be as above, but there does not seem to be an equivalent to the
"WM_DELETE_WINDOW" protocol for resizing.

Do you want to detect when a window is resized or do you want to resize
a window programatically.

If the former, bind the Toplevel to '<Configure>'.

E.g.

from Tkinter import *

def config(t):
def _r(e, t=t):
geom = e.widget.wm_geometry()
geom = geom.split('+')[0]
t.wm_geometry(geom)
print 'resized %s to %s' % (t, geom)
return _r

tk = Tk()
tk.title('resize me')
t2 = Toplevel(tk)
t2.title('I get resized')
tk.bind('<Configure>', config(t2))


Is that cool or what?

James
 
E

Eric Brunel

Hi, I've been searching for a .resize()-like function to overload much
like can be done for the delete window protocol as follows:

toplevel.protocol("WM_DELETE_WINDOW", callback)

I realize that the pack manager usually handles all of the resize
stuff, but I've found an arrangement that the pack manager fails for.
That is, if one embeds a canvas into a window created inside a text
widget, then resize the text widget (via its container), the canvas and
its container windows do not resize.

Supposing you call "embedding" inserting a widget in the text via the
window_create method, why should they? Embedding a window in a Text is
used to put some arbitrary widget in the middle of the text it contains.
So the embedded widget has no reason to grow or shrink with the parent
Text widget: it just moves with the text.
So I need to resize the window
that the canvas is embedded in. The most obvious way of doing this
would be as above, but there does not seem to be an equivalent to the
"WM_DELETE_WINDOW" protocol for resizing.

As James said, the <Configure> event is your friend. But I'm not sure I
understand your use case...

HTH
 
D

deacon.sweeney

Your meaning here is unclear. How is it possible to have "a window
created inside a text widget"?

using the create_window function, as below.
then resize the text widget (via its container), the canvas and
its container windows do not resize. So I need to resize the window
that the canvas is embedded in.

Try the Toplevel.wm_geometry() function.
The most obvious way of doing this
would be as above, but there does not seem to be an equivalent to the
"WM_DELETE_WINDOW" protocol for resizing.

Do you want to detect when a window is resized or do you want to resize
a window programatically.

If the former, bind the Toplevel to '<Configure>'.

E.g.

from Tkinter import *

def config(t):
def _r(e, t=t):
geom = e.widget.wm_geometry()
geom = geom.split('+')[0]
t.wm_geometry(geom)
print 'resized %s to %s' % (t, geom)
return _r

tk = Tk()
tk.title('resize me')
t2 = Toplevel(tk)
t2.title('I get resized')
tk.bind('<Configure>', config(t2))

Is that cool or what?

Yes, this is exactly what I was looking for. Thanks.
 
D

deacon.sweeney

Supposing you call "embedding" inserting a widget in the text via the
window_create method, why should they? Embedding a window in a Text is
used to put some arbitrary widget in the middle of the text it contains.
So the embedded widget has no reason to grow or shrink with the parent
Text widget: it just moves with the text.


As James said, the <Configure> event is your friend. But I'm not sure I
understand your use case...

HTH

I'm using a text widget to hold a set of plots, one plot per line,
such that the scrolling capability of the text widget can be taken
advantage of to display only a subset of the plots at any given time.
In the analyses my program automates, there are at least several plots
are typically loaded into the text widget. This works out splendidly,
but the width of the plots has thus far been a static thing. Now, I'll
be able to adjust the plots widths so that when the owner window is
resized, the width of each plot in the text widget is adjusted and the
plot continues to occupy the entire text widget but no more, making
for a much more professional looking product.

I didn't mean to imply that create_window widgets should automatically
resize with the toplevel... I just couldn't find any way to force it.

Muchas gracias.

Deacon
 
E

Eric Brunel

I'm using a text widget to hold a set of plots, one plot per line,
such that the scrolling capability of the text widget can be taken
advantage of to display only a subset of the plots at any given time.
In the analyses my program automates, there are at least several plots
are typically loaded into the text widget. This works out splendidly,
but the width of the plots has thus far been a static thing. Now, I'll
be able to adjust the plots widths so that when the owner window is
resized, the width of each plot in the text widget is adjusted and the
plot continues to occupy the entire text widget but no more, making
for a much more professional looking product.

IMHO, "abusing" the text widget to do that is quite likely to cause
problems in the future. For this use case, I would have used a Canvas with
scrollbars containing a Frame where the plots are packed or gridded
vertically. The Canvas's scrollregion should then be adjusted each time a
plot is added, removed or resized, and you'd still have to use the
<Configure> event to resize the Frame to the Canvas's width. But at least,
that's what Canvases and Frames are for; the Text widget is for... well,
displaying text. Also note that Pmw (http://pmw.sourceforge.net/) has a
ScrolledFrame megawidget that just does what you want.
Muchas gracias.

You're welcome.

HTH again...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top