cadphile has asked for the wisdom of the Perl Monks concerning the following question:

My perl Tk application uses a couple of tearoff menus. When the menu has been torn off, it becomes a new Toplevel widget. I haven't been able to determine how one can get a handle, or reference to that Toplevel (tornoff) widget, in order to destroy it. Is it a child of the mainwindow?

Thanks for any help.

-cadphile

Replies are listed 'Best First'.
Re: Tk tearoff toplevel IDs
by BlueBlazerRegular (Friar) on Jun 07, 2002 at 20:10 UTC
    cadphile,

    Yes, the tearoff item is child of the mainwindow and will be destroyed when you kill its parent. I don't know of any way of getting its handle so you can destroy it in the program, but I have to ask: Why do you want to? Isn't the user the one 'tearing-off' the menu? Therefore shouldn't they be the one to destroy it?


    Pat
      The reason I want to destroy it in the program is that the menu that it's holding is repeatedly updated with new items, or with different items, and there are times when it must be completed cleared of all items. When it's been torn off, then after all the menu items are deleted, you just have this tiny little window with the Title Banner shrunk down to minimum size. It all pops back up when new items are added to the menu, but in the mean time, it looks ugly -- kind of like a shrivelled up old balloon.
Re: Tk tearoff toplevel IDs
by {NULE} (Hermit) on Jun 07, 2002 at 21:21 UTC
    Hey cadphile,

    Regardless of why you want to be able to find children of a given widget the children method of a widget can provide interesting information about how Tk's heirarchies work.

    Included here is an example of how to tell exactly how a tear off menu is parented. You can see that when you have the menu torn off an additional child of the main window becomes available that is of type Tk::Menu.

    #! /usr/bin/perl -w use strict; use Tk; # Create the main window my $w; $w->{main} = MainWindow->new; $w->{mb} = $w->{main}->Menubutton( -text => 'Menu' )->pack; $w->{mb}->command( -label => 'print kids', -command => [ \&find_kids, +$w ] ); $w->{main}->Button( -text => 'print kids', -command => [ \&find_kids, +$w ] )->pack; sub find_kids { my $w = shift; # Find all the children of main. print "***** new report *****\n"; foreach ($w->{main}->children) { print "1) $_\n"; # find all the grandchildren foreach ($_->children) { print "2)\t$_\n"; # find the great grand kids... foreach ($_->children) { print "3)\t\t$_\n"; } } } } MainLoop;
    Good luck,
    {NULE}
    --
    http://www.nule.org