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

I'm trying to build a table using Tk::MListbox for it's sorting and column dragging capabilities, but cannot get it to scroll. I tried using the Scrolled() shortcut method first, but it died, so I resorted to the 'long way'. This is what my code looks like:
my $table = $MainFrame-> MListbox( -selectmode => 'single', -columns=>[ [-text=>'Date', -sortable=>1, + -width => 0], [-text=>'Time', -sortable=>1, + -width => 0], [-text=>'Name', -sortable=>1, + -width => 0], [-text=>'Address', -sortable=>1, + -width => 0], [-text=>'Phone', -sortable=>1, + -width => 0] ] )->pack(-fill=> 'both' +, -expand =>1, -side=>'left', -ipadx => 5);; while($result->fetch()){ ### Results from DB query $table->insert('end', [$Date, $Time, $Name, $Address, $Phone]); } my $scroll = $MainFrame->Scrollbar(-command => ['yview', $table]); table->configure(-yscrollcommand => ['set', $scroll]); $scroll->pack(-side => 'right', -fill => 'y');


When I run this, the table and the scrollbar loads but it won't scroll. Sorting and dragging are also broken.

Here is what is dumped to the console:

error:Not a CODE reference at C:/Perl/site/lib/Tk/MListbox.pm line 72 +3. Tk::Error: Not a CODE reference at C:/Perl/site/lib/Tk/MListbox.pm lin +e 723. Tk::Widget::Callback at C:/Perl/site/lib/Tk/Widget.pm line 1149 Tk::CListbox::yview at C:/Perl/site/lib/Tk/MListbox.pm line 66 Tk::Derived::Delegate at C:/Perl/site/lib/Tk/Derived.pm line 463 Tk::Widget::__ANON__ at C:/Perl/site/lib/Tk/Widget.pm line 322 Tk::MListbox::yscrollCallback at C:/Perl/site/lib/Tk/MListbox.pm line + 700 <Configure> (command bound to event)
When you attempt to scroll you get:
error:Not a CODE reference at C:/Perl/site/lib/Tk/MListbox.pm line 720 +. error:Not a CODE reference at C:/Perl/site/lib/Tk/MListbox.pm line 72 +0. Tk::Error: Not a CODE reference at C:/Perl/site/lib/Tk/MListbox.pm lin +e 720. Tk::Widget::Callback at C:/Perl/site/lib/Tk/Widget.pm line 1149 Tk::CListbox::yview at C:/Perl/site/lib/Tk/MListbox.pm line 66 Tk::Derived::Delegate at C:/Perl/site/lib/Tk/Derived.pm line 469 Tk::Widget::__ANON__ at C:/Perl/site/lib/Tk/Widget.pm line 322 Tk::MListbox::yview at C:/Perl/site/lib/Tk/MListbox.pm line 911 Tk::Scrollbar::ScrlByUnits at ..\blib\lib\Tk\Scrollbar.pm (autosplit +into ..\blib\lib\auto\Tk\Scrollbar\ScrlByUnits.al) line 336 Tk::Scrollbar::Select at ..\blib\lib\Tk\Scrollbar.pm (autosplit into +..\blib\lib\auto\Tk\Scrollbar\Select.al) line 201 Tk::Scrollbar::ButtonDown at ..\blib\lib\Tk\Scrollbar.pm (autosplit i +nto ..\blib\lib\auto\Tk\Scrollbar\ButtonDown.al) line 159 <Button-1> (command bound to event)
If you replace the MListbox with a normal listbox, it works just fine.

Is this a limitation with MListbox? As useful as this sort of widget would be, I can't imagine there isn't an easy way to do it.

Replies are listed 'Best First'.
Re: scrolling MListbox in Perl/tk
by zentara (Cardinal) on Jul 16, 2008 at 19:13 UTC
    Always try to show a real working example. If we can't run it, we only can guess by looking at code fragments, and that is difficult. This works and inserts, and scrolling works.
    #!/usr/bin/perl use Tk; use Tk::MListbox; use strict; use warnings; my $mw = new MainWindow(); $mw->fontCreate('big', -family=>'arial', -weight=>'bold', -size=> 18 ); $mw->fontCreate('medium', -family=>'arial', -weight=>'bold', -size=> 14 ); my $frame = $mw->Frame(-bd => 2, -relief => 'raised')->pack(-expand => + 1, -fill => 'both'); my $scrolledMListbox = $frame->Scrolled( qw/MListbox -selectmode browse -scrollbars oe -font medium / )->pack(-expand => 1, -fill => 'both'); $scrolledMListbox->columnInsert('end', -text => "Header",-font => 'bi +g'); $scrolledMListbox->bindRows('<ButtonRelease-1>', sub { my ($w, $infoHR) = @_; print "@_\n"; print "You selected row: " . $infoHR->{-row} . " in column: " . $infoHR->{-column} . "\n"; print $scrolledMListbox->getRow( $scrolledMListbox->curselection() +->[0] ),"\n"; } ); my $timer = $mw->repeat(1000,sub{ $scrolledMListbox->insert('end', [int rand 20] ); }); MainLoop;

    I'm not really a human, but I play one on earth CandyGram for Mongo
      that example didn't work for me either. Sorry about not posting working code. Here is some simplified code that is based off your example, but still gives the exact behavior I described. The table loads fine, but the scroll bar doesn't work and when you sort by a column it empties the table.
      use warnings; use strict; use Tk; use Tk::Pane; use Tk::NoteBook; use DBI; use Tk::MListbox; ### Build Main Window # my $mainWindow = MainWindow->new; $mainWindow->geometry("800x600"); my $frame = $mainWindow->Frame(-bd => 2, -relief => 'raised')->pack(- +expand => 1, -fill => 'both'); my $scrolledMListbox = $frame->Scrolled( qw/ MListbox -selectmode browse -scrollbars oe / )->pack(-expand => 1, -fill => 'both'); $scrolledMListbox->columnInsert('end', -text=>'Date', -sortable=>1, +-width => 10); $scrolledMListbox->columnInsert('end', -text=>'Time', -sortable=>1, +-width => 10); $scrolledMListbox->columnInsert('end', -text=>'Property', -sortable= +>1, -width => 10); $scrolledMListbox->columnInsert('end', -text=>'Name', -sortable=>1, +-width => 10); $scrolledMListbox->columnInsert('end', -text=>'Address', -sortable=> +1, -width => 10); $scrolledMListbox->columnInsert('end', -text=>'Phone', -sortable=>1, + -width => 10); $scrolledMListbox->bindRows('<ButtonRelease-1>', sub { my ($w, $infoHR) = @_; print "@_\n"; print "You selected row: " . $infoHR->{-row} . " in column: " . $infoHR->{-column} . "\n"; print $scrolledMListbox->getRow( $scrolledMListbox->curselection +()->[0] ),"\n"; } ); for (my $count = 100; $count >= 1; $count--){ $scrolledMListbox->insert('end', [[int rand 20], [int rand 20], [ +int rand 20], [int rand 20], [int rand 20], [int rand 20]]); } MainLoop;
      It's basically the code I am using except I stripped out the database stuff and just went with a random number generator. I pulled the font stuff out because it was throwing an error and I didn't feel like debugging it.
        You probably don't want to hear this, but your code works fine for me on linux, with Tk-804.027 The column sorting works just fine. Maybe you need to upgrade?

        I'm not really a human, but I play one on earth CandyGram for Mongo