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

Hi Monks,
I have been looking at Mastering Perl/TK for the last couple of days and need a little help figuring this out.

In the main window I want two widgets, a Tree/HList on the left and a Frame on the right which will have other widgets in it.

This is what I want to happen:

Firstly the HList on the left should be scrollable, -Scrolled doesn't seem to work?
When a node in the tree is selected, it generates some widgets in the frame on the right displaying some kind of information. An example in Windows could be Explorer or the Admin tools.

Any pointers, code snippets etc is greatly appreciated.
Cheers, Steve
  • Comment on Perl/Tk Associating HLists with other widgets

Replies are listed 'Best First'.
Re: Perl/Tk Associating HLists with other widgets
by davidj (Priest) on Jun 15, 2004 at 14:04 UTC
    I modified the following from the tk::hlist manpage.

    I admit that it is a weak hack, but it will give you an idea of how to accomplish what you want.
    In sub pw() it simply destroys the current label widget (if it exists) and puts a new label widget in the frame. I could have just reconfigured the widget, but I did it this way to show a way to remove one widget from the frame and create a new one.
    Run it, follow the code, and you will get the idea

    use strict; use Tk; use Tk::Label; use Tk::HList; my $mw = MainWindow->new(); my $widget = 0; my $frame = $mw->Frame(); # my $hlist = $mw->HList( #uncomment this line and comment the next + if Scrolled does not work my $hlist = $mw->Scrolled(qw/HList/, -itemtype => 'text', -separator => '/', -selectmode => 'single', -browsecmd => sub { my $file = shift; &pw($file); } ); foreach ( qw(/ /home /home/ioi /home/foo /usr /usr/lib) ) { $hlist->add($_, -text=>$_); } $hlist->pack; $frame->pack; MainLoop; sub pw() { my $txt = shift; $widget->destroy if $widget != 0; $widget = $frame->Label(-text => $txt)->pack; }

    Hope this helps,

    davidj
Re: Perl/Tk Associating HLists with other widgets
by eserte (Deacon) on Jun 15, 2004 at 13:48 UTC
    You have to use $mw->Scrolled("HList", ...) to create a scrolled HList. Depending on your needs, you can then use -browsecommand or -command options to specify a callback which may populate the frame. An alternative is to bind the <1> event directly to this callback.
Re: Perl/Tk Associating HLists with other widgets
by gri6507 (Deacon) on Jun 15, 2004 at 14:07 UTC
    You could always resort to old school scroll bar creation. Note, the code below is for a Tree, but HList is very close.

    my $CalTreeScrollBarY = $leftFrame->Scrollbar(); my $CalTreeScrollBarX = $leftFrame->Scrollbar(-orient=>"horiz"); $CalTree = $leftFrame->Tree(-yscrollcommand=>['set',$CalTreeScrollBarY +], -xscrollcommand=>['set',$CalTreeScrollBarX +],); $CalTreeScrollBarY->configure(-command=> [yview=>$CalTree]); $CalTreeScrollBarX->configure(-command=> [xview=>$CalTree]); $CalTreeScrollBarX->pack(-side=>"bottom",-fill=>"x"); $CalTree->pack(-side=>"left",-fill=>"both"); $CalTreeScrollBarY->pack(-side=>"left",-fill=>"y");
Re: Perl/Tk Associating HLists with other widgets
by zentara (Cardinal) on Jun 16, 2004 at 12:36 UTC
    You might want to check out ztkdb-sql for some tricks on how to do an HList in a browser style program which incorporates sql. A simpler version using Storable is here: ztkdb

    I'm not really a human, but I play one on earth. flash japh