simple listbox with binding question 
Author Message
 simple listbox with binding question

Hi All,

I'm trying to implement a simple listbox which will pass the selected
item to another routine.  I've attempted to simplify the problem into
the example below, which merely presents the user with 3 selections and
is supposed to
print the selection to STDOUT:

#!/usr/local/bin/perl -w
use Tk;
use subs qw/Init/;

use strict;

$MW = MainWindow->new();
Init();
MainLoop();
####################################################################
# subroutines
####################################################################
sub Init{

    $MW->title("Simple Listbox");
    my $frame = $MW->Frame(-width      => 280,
      -height     => 500,
      -background => 'skyblue')->pack();


    my $slb = $frame->Scrolled('Listbox',
          -height => '1',
          -selectbackground => 'darkgreen',
          -selectforeground => 'yellow',
          -scrollbars => "osoe",
          -selectmode => "single" )->
      pack(qw/-side left -expand yes -fill both -padx 10 -pady 10/);


    $slb->bind('<Button-1>',
#   [ \&Print_Message_To_Stdout, $slb->get(0,$slb->curselection()) ]);
   [ \&Print_Message_To_Stdout, $slb->get($slb->curselection()) ]);

Quote:
} # end Init

sub Print_Message_To_Stdout{

  print "The selected stooge is $the_stooge\n";

Quote:
} # end sub Print_Message_Into_Textbook

Here's my problem. If I try the code as is, I get the following error
message:
wrong # args: should be ".frame.frame.listbox get first ?last?" at
/usr/local/lib/perl5/site_perl/Tk.pm line 217.

If I try the OTHER call to Print_Message_To_Stdout shown above
(commented out), then I get:
The selected stooge is Tk::Listbox=HASH(0x31e230).

Frankly, I'm stuck.  Can anybody point out what I'm doing wrong?

tia,
tony



Fri, 11 Oct 2002 03:00:00 GMT  
 simple listbox with binding question
Things fail when $slb->curselection is undefined.
The slight changes below fix the problem.


says...

Quote:

> #!/usr/local/bin/perl -w
> use Tk;
> use subs qw/Init/;

> use strict;

> $MW = MainWindow->new();
> Init();
> MainLoop();
> ####################################################################
> # subroutines
> ####################################################################
> sub Init{

>     $MW->title("Simple Listbox");
>     my $frame = $MW->Frame(-width      => 280,
>       -height     => 500,
>       -background => 'skyblue')->pack();


>     my $slb = $frame->Scrolled('Listbox',
>           -height => '1',
>           -selectbackground => 'darkgreen',
>           -selectforeground => 'yellow',
>           -scrollbars => "osoe",
>           -selectmode => "single" )->
>       pack(qw/-side left -expand yes -fill both -padx 10 -pady 10/);


>     $slb->bind('<Button-1>',
> #   [ \&Print_Message_To_Stdout, $slb->get(0,$slb->curselection()) ]);
>    [ \&Print_Message_To_Stdout, $slb->get($slb->curselection()) ]);

    $slb->bind('<Button-1>', [\&Nyuk, \$slb]);

Quote:

> } # end Init

sub Nyuk {

    my $sel = $lb->curselection;
    if (defined($sel)) {
        Print_Message_To_Stdout($lb->get($sel));
    }

- Show quoted text -

Quote:
}
> sub Print_Message_To_Stdout{

>   print "The selected stooge is $the_stooge\n";
> } # end sub Print_Message_Into_Textbook

> Here's my problem. If I try the code as is, I get the following error
> message:
> wrong # args: should be ".frame.frame.listbox get first ?last?" at
> /usr/local/lib/perl5/site_perl/Tk.pm line 217.

> If I try the OTHER call to Print_Message_To_Stdout shown above
> (commented out), then I get:
> The selected stooge is Tk::Listbox=HASH(0x31e230).

> Frankly, I'm stuck.  Can anybody point out what I'm doing wrong?

--
Marc Dashevsky  (remove "_" from address to reply by e-mail)


Fri, 11 Oct 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Listbox-Bind question

2. simple question - listbox

3. How to bind a scroll bar to more than one listbox

4. bind Ev problem with listbox widget

5. listbox default bindings

6. Key Bindings for Listbox

7. Key Bindings for Listbox

8. Listbox binding problem

9. HList with Listbox-like bindings

10. binding <Key> to Listbox

11. Help for listbox->bind() needed

12. bind to listbox selection

 

 
Powered by phpBB® Forum Software