Perl/Tk Associating HLists with other widgets
Anonymous Monk
created: 2004-06-15 09:21:22
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
Re: Perl/Tk Associating HLists with other widgets
created: 2004-06-15 09:48:24
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
created: 2004-06-15 10:04:35
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
created: 2004-06-15 10:07:22
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
created: 2004-06-16 08:36:33
You might want to check out node 338291 for some tricks on how to do an HList in a browser style program which incorporates sql. A simpler version using Storable is here: node 327499

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

perlmonks.org content © perlmonks.org and Anonymous Monk, davidj, eserte, gri6507, zentara

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03