http://qs321.pair.com?node_id=11118765


in reply to Re^2: Writing my first PERL/Tk megawidgit
in thread Writing my first PERL/Tk megawidgit

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11118320 use warnings; use Tk; { # for package package Tk::ListBox3; use List::Util qw( first ); use base qw/ Tk::Frame /; # Frame-based composite Construct Tk::Widget 'ListBox3'; # install MyNewWidget in pTk namespac +e sub ClassInit # called once to initialize new class { my($class, $mw) = @_; $class->SUPER::ClassInit($mw); } sub Populate # called to build each widget instance { my($self, $args) = @_; $self->SUPER::Populate($args); my $search = ''; my $frame = $self->Frame( -borderwidth => 5, -relief => 'ridge', )->pack(-fill=> 'both', -expand=> 1); my $label = $frame->Label( -fg => 'blue', -font => 30, )->pack(-fill => 'x'); my $lbox = $frame->Scrolled(Listbox => -scrollbars => 'se', -height => 20, -selectforeground => 'orange', -selectbackground => 'steelblue4', -exportselection => 0, )->pack(-side => 'bottom', -fill => 'both', -expand=> 1); my $entry = $frame->Entry(-textvariable => \$search, -validate => 'key', -validatecommand => sub { my ($want) = @_; length $want or return 1; my @list = $lbox->get(0 , "end"); my $item = first { $list[$_] =~ /^\Q$want\E/ } 0 .. $#list; defined $item or return 0; $lbox->selectionClear( 0 , "end" ); $lbox->selectionSet($item); $lbox->see($item); 1 # to allow }, )->pack(-fill => 'x'); $self->ConfigSpecs( DEFAULT => [$lbox], text => [$label] ); $self->Delegates( Construct => $lbox, insert => $lbox ); } } # end package my @choices = sort qw /alpha beta charlie delta echo foxtrot hotel ind +ia juliet kilo lima motel nancy oscar papa quebec radio sierra tango uniform v +ictor whiskey xray yankee zulu oin gloin beorn gandalf elr ond eowyn/; my $header_msg = "ENTER a KEY name: "; my $mw = MainWindow->new; $mw->geometry( '+900+250' ); $mw->title( 'Listbox' ); my $lb = $mw->ListBox3( -text => $header_msg, )->pack(-fill => 'both', -expand => 1, -side => 'left'); $lb->insert( 'end', @choices ); my $lb2 = $mw->ListBox3( -text => $header_msg, )->pack(-fill => 'both', -expand => 1, -side => 'left'); $lb2->insert( 'end', @choices ); MainLoop;

Demos two of them to show no interference.

Replies are listed 'Best First'.
Re^4: Writing my first PERL/Tk megawidgit
by Anonymous Monk on Jul 01, 2020 at 23:39 UTC

    Hey -- thanks. The stuff after " } # end package " made a lot of sense to me, since it is straight pTk code. You forgot the button on the bottom. BUT at this point I understand the code enough to do it myself. Whats the best way to deal with the "sub ClassInit" and "sub Populate" magic?