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

Hello Monks,

I'm writing a small Tk app to list an MP3 collection and allow searching. I'm doing this to learn some Tk.

I am displaying the results in a Tk::Table, but the problem is that I'm not finding anywhere in the documentation how to reset the table to display new results.

The approach I'm using now is that I'm placing empty strings in the fields, and then filling in the results. But that's plain wrong. Because, say after a search that displayed 20 results, the next search that will display for example 4 results, will display 4 rows of results, and 16 empty rows.

my $table = $window->Table( -rows => 20, -columns=> 4, -fixedrows=>1, -takefocus=>1)->pack; $table->put(0,0,'Title'); $table->put(0,1,'Artist'); $table->put(0,2,'Album'); $table->put(0,3,'CD'); # later on... sub show_results { # clean up table. for my $i (1..$table->totalRows) { for my $j (0..3) { $table->put($i,$j,''); } } my $b = $window->Balloon(); # fill the new data. my $i = 0; foreach (keys %db) { if ($db{$_} =~ /$searchpattern/i) { my ($file, $location, $title, $artist, $album, $cd) = split /\n/, $db{$_}; my $l = $table->Label(-text=>$title); $b->attach($l, -balloonmsg=>$_); $table->put($i, 0, $l); $table->put($i, 1, $artist); $table->put($i, 2, $album); $table->put($i, 3, $cd); $i++; } } }

Is there a way to remove rows from a Tk::Table? Should I be deleting the table altogether and redrawing it? Or should I be using something completely other than the Tk::Table to display the data?

Replies are listed 'Best First'.
Re: removing rows from a Tk::Table
by PodMaster (Abbot) on Sep 12, 2004 at 10:56 UTC
    Sometimes when RTFM-ing is not enough, you need to UTSA(Use The Source Luke), and this is quite often the case with perkTk.

    from site/lib/Tk/Table.pm:

    sub clear { my $self = shift; my $rows = $self->cget(-rows); my $cols = $self->cget(-columns); foreach my $r (1 .. $rows) { foreach my $c (1 .. $cols) { my $old = $self->get( $r, $c ); next unless $old; $self->LostSlave($old); $old->destroy; } } $self->_init; $self->QueueLayout(_SlaveSize); }
    I hope that clears up what you need to do.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      My copy of the Tk::Table docs contain this:

      The table can be emptied using $table->clear the widgets which were in the table are destroyed.
      :)

      For the OP, you can also use a Listbox or an HList.

      Why not using abbreviation "RTFS", which means "Read This Fine Source"?
      IMHO it is a bit clearer than UTSA...