Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Change a widget parent

284 views
Skip to first unread message

Eric Brunel

unread,
Jan 18, 2011, 5:28:54 AM1/18/11
to
Hello all,

For an application I'm writing, I would need a way to change the parent
of a widget from one toplevel to another. The main aim would be to have
an editor window showing different files in tabs, and be able to move a
tab to its own window, or to move tabs around different windows. For
this, it would be much easier to have the whole contents of the tab as a
set of widgets in a frame, and be able to move the frame across the
different windows, instead of having to recreate the whole frame each
time a tab is moved.
So I investigated on the -in option in grid or pack, only to find out it
didn't work across windows: If I'm trying to grid or pack a label
created as a child of . in a toplevel .wdw for example, an error is
raised.
But I could see however that it works with frames created as children of
the root window. And since frames and toplevels can now be converted to
each other using the wm manage / wm forget commands, I figured out the
following could work:

------------------------------------
label .lbl -text "Moving..."

set windows [list [toplevel .wdw1] [toplevel .wdw2] [toplevel .wdw3]]
set label_window_index -1

proc move_label {} {
global label_window_index
global windows
pack forget .lbl
set label_window_index [expr ($label_window_index + 1) % [llength
$windows]]
set window [lindex $windows $label_window_index]
set geometry [wm geometry $window]
wm forget $window
pack .lbl -in $window
wm manage $window
wm geometry $window $geometry
}

move_label

pack [button .btn -text "Move label" -command move_label]
------------------------------------

What I expected is to see my "Moving..." label going from one window to
the other. But it doesn't work: No error is raised, but the label never
appears in any window. I tried it both on Linux and Windows with tcl/tk
8.5.5, the result is the same: no error, but the label is not showing.

So is this supposed to work, or am I doing something wrong?
- Eric -

tomas

unread,
Jan 18, 2011, 7:01:27 AM1/18/11
to

Hi,

as far as I know, you can't really "reparent" a widget, since the
hierarchy is given by the widget's name (and changing its name will
bring along other "interesting" issues (see [TIP # 125] for some
discussion).

But you can create your "moving" widget as a toplevel and use the "-use"
option (heh) to *embed* it in another widget, the id of which you can
gather with [winfo id] -- see the [toplevel] manpage

[TIP #125] <http://www.tcl.tk/cgi-bin/tct/tip/125.txt>
[toplevel] <http://www.tcl.tk/man/tcl8.5/TkCmd/toplevel.htm>

Regards
-- tomás

Eric Brunel

unread,
Jan 18, 2011, 8:21:07 AM1/18/11
to
In article <87ipxmi...@floh.bas23>, tomas <to...@floh.bas23> wrote:

> Hi,
>
> as far as I know, you can't really "reparent" a widget, since the
> hierarchy is given by the widget's name (and changing its name will
> bring along other "interesting" issues (see [TIP # 125] for some
> discussion).
>
> But you can create your "moving" widget as a toplevel and use the "-use"
> option (heh) to *embed* it in another widget, the id of which you can
> gather with [winfo id] -- see the [toplevel] manpage

Not sure how it would help me in this case: The manpage says the -use
option cannot be changed via the configure command, so how would I be
able to fake a widget "reparenting"? If I understand correctly, once the
-use option has been set to the [winfo id] of a container frame, it
can't be changed anymore. Or am I missing something?

Thanks anyway.
- Eric -

jr

unread,
Jan 18, 2011, 9:09:14 AM1/18/11
to
On Jan 18, 10:28 am, Eric Brunel <eric.bru...@pragmadev.nospam.com>
wrote:

> For an application I'm writing, I would need a way to change the parent
> of a widget from one toplevel to another. The main aim would be to have
> an editor window showing different files in tabs, and be able to move a
> tab to its own window, or to move tabs around different windows. For
> this, it would be much easier to have the whole contents of the tab as a
> set of widgets in a frame, and be able to move the frame across ...

as a workaround, make each of the frames a toplevel and embed those in
the editor tab(s)?

tomas

unread,
Jan 18, 2011, 9:32:33 AM1/18/11
to
Eric Brunel <eric....@pragmadev.nospam.com> writes:

> In article <87ipxmi...@floh.bas23>, tomas <to...@floh.bas23> wrote:

[-use...]

> Not sure how it would help me in this case: The manpage says the -use
> option cannot be changed via the configure command, so how would I be
> able to fake a widget "reparenting"? If I understand correctly, once the
> -use option has been set to the [winfo id] of a container frame, it
> can't be changed anymore. Or am I missing something?

Ouch. You are right (tested it -- no, it isn't an error in th man
page). I must defer to more knowledgeable folks :-(

Regards
-- tomás

hae

unread,
Jan 18, 2011, 12:48:39 PM1/18/11
to
On 18 Jan., 11:28, Eric Brunel <eric.bru...@pragmadev.nospam.com>
wrote:

Hello Eric,

have a look at this http://wiki.tcl.tk/21846

It might help you.

Rüdiger

Donal K. Fellows

unread,
Jan 19, 2011, 6:22:33 AM1/19/11
to
On Jan 18, 10:28 am, Eric Brunel <eric.bru...@pragmadev.nospam.com>
wrote:
> So is this supposed to work, or am I doing something wrong?

It's not supposed to work. Tk widgets are *very* attached to their
hierarchy; there are masses of places in there that assume that a
parent is always the same (as opposed to the container from a pack/
grid perspective). You *can* make a standard frame into a toplevel in
8.5 (not so sure about vice versa :-)) but that's not really changing
what its parent is.

Can you make do with having two widgets showing the same content? That
is, share the model (Tcl variables, etc.) instead of moving the widget
itself about. It's even relatively easy to do with a text widget these
days (you can clone it; the clone shares the complex model but can be
elsewhere in the widget hierarchy just fine) so it's only a big
problem if you're using canvases.

Donal.

Eric Brunel

unread,
Jan 20, 2011, 4:48:11 AM1/20/11
to
In article
<ce458e41-5e1b-440b...@o9g2000pre.googlegroups.com>,

"Donal K. Fellows" <donal.k...@manchester.ac.uk> wrote:

> On Jan 18, 10:28 am, Eric Brunel <eric.bru...@pragmadev.nospam.com>
> wrote:
> > So is this supposed to work, or am I doing something wrong?
>
> It's not supposed to work. Tk widgets are *very* attached to their
> hierarchy; there are masses of places in there that assume that a
> parent is always the same (as opposed to the container from a pack/
> grid perspective). You *can* make a standard frame into a toplevel in
> 8.5 (not so sure about vice versa :-)) but that's not really changing
> what its parent is.

I tried to do that indeed, and I confirm it doesn't work well. As long
as frames are actually frames in the same container, everything works
fine: a widget created in one of their common ancestors can be moved
from frame to frame. But once you turn such a frame into a toplevel, the
widget disappears. So it's obvioulsy not the way to go.

> Can you make do with having two widgets showing the same content? That
> is, share the model (Tcl variables, etc.) instead of moving the widget
> itself about. It's even relatively easy to do with a text widget these
> days (you can clone it; the clone shares the complex model but can be
> elsewhere in the widget hierarchy just fine) so it's only a big
> problem if you're using canvases.

For most of the displayed information, I can do that indeed. What bugged
me was indeed text widgets, which could display a lot of lines with a
lot of tags, and I feared recreating the whole thing could take a
noticeable time. But I tried to create peers of the text widget into the
new window, and it does work great indeed. So thanks a lot for the
trick, it seems this will be the way to go for me.

Unfortunately, I will have to do the same with canvases quite soon. But
the amount of information they display might be much less than for the
texts, so I guess recreating everything will probably be acceptable in
this case.

Thanks again!
- Eric -

Uwe Klein

unread,
Jan 21, 2011, 5:40:04 PM1/21/11
to
blt tabset used to do reparenting.
http://pdqi.com/man/mann/tabset.html

uwe

0 new messages