Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Usefulness of -in option in Geometry Management (Perl/Tk)

by Anonymous Monk
on Nov 24, 2014 at 05:07 UTC ( [id://1108222]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks,

    The Geometry Manager docs (say, for example that of: pack) state the following
-in => *$master* insert the slave(s) at the end of the packing order for the master window.
This means,
  1. packs $widget inside of $master rather than the parent of $widget, which is the default. (Chapter 2: Mastering Perl/Tk -- Oreilly Press).

  2. Technically, any widget can be a parent of another widget, but life is easier when widgets are children of a Frame, or else we have to use the -in option with pack. (Chapter 11: Frames, MainWindow...)
    I'm interested to understand the usefulness of this option with respect to Tk::NoteBook. In Tk::NoteBook at a time there is only one active page. i.e. only one page is in a raised state. Therefore, if each page is to have same set of widgets displayed inside it, then instead of creating the widgets for each page in the Tk::NoteBook it is simply enough to create the widgets only once and then re-use the same widgets for all the other pages, after all only one page gets displayed at a time.

Here's a small program that attempts at this BUT FAILS.
use strict; use Tk; use Tk::NoteBook; my $mw = MainWindow->new; $mw->geometry('100x200'); my $nb = $mw->NoteBook()->pack(-fill => 'both', -expand => 1); my $page_tab_01=$nb->add ("TAB-1", -label=>'TAB-1', -raisecmd=>\&callback_01); my $page_tab_02=$nb->add ("TAB-2", -label=>'TAB-2', -raisecmd=>\&callback_02); my $t_button = $page_tab_01->Button(-text=>'OK', -command=>sub{exit}); MainLoop; sub callback_01 { $t_button->packForget; $t_button->pack(-in=>$page_tab_01); } sub callback_02 { $t_button->packForget; $t_button->pack(-in=>$page_tab_02); #<---ERROR HERE }

    I get the following error
error:can't pack .notebook.tAB_1.button inside .notebook.tAB_2

    Please let me know as to what mistake I'm doing in the above prgram and is there any other method besides this to accomplish the re-use of widgets in different page-tabs.

Thank you very much

Replies are listed 'Best First'.
Re: Usefulness of -in option in Geometry Management (Perl/Tk)
by Loops (Curate) on Nov 24, 2014 at 09:06 UTC

    Tk apparently has two separate hierarchies, parent/child and master/slave. And in my vague and limited understanding, the first controls stacking order and the second controls geometry (screen space). So taking a wild guess here, trying to repack a button with first tab parent into a second tab, changes the geometry but not the parent/child relationship -- meaning the stacking wouldn't work. Take that with a grain of salt, but if you change your button to be a child of the main window, your code works:

    my $t_button = $mw->Button(-text=>'OK', -command=>sub{exit});
      Hello Loops,

            Thanks a lot for your swift response and insight. I also read through another response from AM. Just to make sure I understand this correctly,
      1. A parent/child hierarchy is established at the creation phase of objects. For ex:
        my $t_button = $mw->Button(-text=>'OK', -command=>sub{exit});
        This makes the object pointed to by $t_button a child of the object pointed to by $mw.

      2. A master/slave hierarchy is established by the geometry manager used(such as grid, pack, form, place).

      3. When displaying a widget, if the -in option is not at all used then the master/slave hierarchy is exactly the same as parent/child hierarchy.

      If this understanding of mine is correct, then I proceed to a portion of your next statement:
      • ....trying to repack a button with first tab parent into a second tab, changes the geometry...

      • This, I believe, you meant that the pack geometry manager attempts to pack first tab inside the second tab of the same NoteBook. This of-course is not permitted, after all every tab of any given NoteBook must NOT ONLY be a direct child BUT ALSO a direct slave under that NoteBook
      • ...but not the parent/child relationship -- meaning the stacking wouldn't work.
        This I didn't understand, honestly. So please let me know.

      Moving on...Yes, after you said that making button a child of main window, I tried it and it works. But if I change the button to be a child of NoteBook i.e.
      my $t_button = $nb->Button(-text=>'OK', -command=>sub{exit});
      Then not only does the button not show up, I don't get any error from the execution of pack statement inside the callbacks either when the callbacks are executed. What's going on ? Can you please tell me what's the problem here?

      Hey, Thank you for your patience in reading my query.
      Hello Loops,

          I just chanced to see some details on this.
      1. http://stackoverflow.com/questions/18106377/cant-pack-a-widget-inside-a-sibling-toplevel

      2. the first para of response from Donal Fellows and the last response dated Aug 7 '13 at 17:37 from Elchonon Edelson.

      3. http://stackoverflow.com/questions/21467113/is-there-a-limit-on-the-length-or-nesting-of-tcl-tk-widget-pathnames/21468948#21468948

      4. the response dated Jan 30 at 22:39 from Donal Fellows.

          Those being the responses about pure Tcl/Tk I didn't quite understand what they said. Do you have any explanation/thoughts from your side ?

      Thank you
Re: Usefulness of -in option in Geometry Management (Perl/Tk)
by Anonymous Monk on Nov 24, 2014 at 10:26 UTC

    I get the following error error:can't pack .notebook.tAB_1.button inside .notebook.tAB_2 Please let me know as to what mistake I'm doing

    Look at it this way :) its a path

    error:can't pack .notebook.tAB_1.button inside .notebook.tAB_2
    its a path
    .notebook.tAB_1.button .notebook.tAB_2
    its a parent child relationship
    .grandpa.daddy.baby .grandpa.uncle

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1108222]
Approved by vinoth.ree
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-19 06:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found