Perl Tk TableMatrix Widget Help

I was told that perl/tk is a nice perl gui but is very badly documented. Well I can confirm I was told correctly.

Most recently I had some trouble with Tk:TableMatrix which is very poorly documented. The biggest annoyance I had was that TableMatrix is not compatible with ActivePerl 811 the most recent version. I found on a non english site that TableMatrix is compatible with 809. And I found that answer after a few hours of trying to figure out why my script would not run.

I also found out that when the documentation says to pass 4 values to -bd to describe {left right top bottom} border they really mean a refence to an array with four variables.

So I hope the posting of the following code will help others with TableMatrix.

Sample Tk::TableMatrix Code

#!/usr/bin/perl

use Tk;
use Tk::TableMatrix::Spreadsheet;

$mw = MainWindow->new;
$mw->configure(-title=>’ID3 Tag Genre and Year Fixer’);
$mw->minsize(qw(500 200));
$menu = $mw->Frame()->pack(-side=>’top’,-fill=>’x’);
$menu_file = $menu->Menubutton(-text=>’File’,-tearoff=>’false’)->pack(-side=>’left’);
$menu_file->command(-label=>’Exit’,-command=>sub{$mw->destroy});
$frame = $mw->Frame(-height=>’10’,-width=>’30’,-relief=>’groove’,-borderwidth=>’3′)->pack(-fill=>’x’,-pady=>’0′);

@border = (0,0,0,1);

$arrayVar->{"0,0"} = "%";
$arrayVar->{"0,1"} = "Artist";
$arrayVar->{"0,2"} = "Album";
$arrayVar->{"0,3"} = "Year";
$arrayVar->{"0,4"} = "Genre";
$table = $frame->Scrolled(‘Spreadsheet’,
?-cols => 5,
?-width => 5, -height => 6,
?-titlerows => 1,
?-variable => $arrayVar,
?-selectmode => ‘multiple’,
?-selecttype => ‘row’,
?-resizeborders => ‘col’,
?-bg => ‘white’,
?-rowheight => 2,
?-bd => \@border,
?-justify => ‘left’,
?-drawmode => ‘compatible’,
?-wrap => 0,
?-relief => ‘solid’
)->pack(-fill=>’both’);
$table->rowHeight(0,1);
$table->tagRow(‘title’,0);
$table->tagConfigure(‘title’, -bd=>2, -relief=>’raised’);
$table->colWidth(0,5,3,6,4,10);
MainLoop();

This entry was posted in Programming. Bookmark the permalink.

4 Responses to Perl Tk TableMatrix Widget Help

  1. Michael says:

    Hi Kevin,
    as a Perl/Tk novice I ran into same compatibility problems as you mentioned, but your advice to fetch ana adaequate compiled version form an other site was very helpful, now it works well…thank you!

    however, the documentation as you mentioned already is quite poor.

    I would like to ask you about a problem I figured out recently, maybe you have some advice concerning this issue as well 😉

    Problem:
    It is possible to define some kind of %titlehash with newline characters, assign it to variable option and text separated by newline is displayed correctly. Checkout:

    my $titleHash = {
    “0,0” => “abc”,
    “0,1” => “1234\nfoo”,
    };

    my $table;
    $table = $mw->Scrolled(‘TableMatrix’,
    -titlerows => 1,
    -roworigin => 0,
    -colorigin => 0,
    -cols => 2,
    -rowheight => 2,
    -variable => $titleHash,
    );

    $table->pack(-expand => 1, -fill => ‘both’);
    Mainloop;

    BUT if you try to do it for a single cell, it does not work!
    Check out this little subroutine

    PushRow(1);

    sub PushRow {
    my $row = shift;
    my $foo = ‘123456’;

    my %CellValue = ( 0 => “123”,
    1 => “abcd\n$foo”,
    );

    for $col (0..1) {
    $table->set(‘row’,”$row,$col”,”$CellValue{$col}”);
    }

    } # End PushRow

    Obviously, title and ordinary cells are handled in a different way. Maybe this reason is related to -colseparator/-rowseparater stuff, which are used for copy&paste actions. I don’t know.

    Do you maybe know maybe another forum where I could stress this issue?

    Anyway, many thanks in advance!

    Michael

  2. Doug Marsh says:

    Thanks for the TableMatrix tip. I couldn’t figure out why my code was not working anymore!

  3. Ed Lipson says:

    I like what you tried to do.

    FYI,
    c:\…\tk ==>perl -v

    This is perl, v5.8.8 built for MSWin32-x86-multi-thread
    (with 21 registered patches, see perl -V for more detail)

    Copyright 1987-2006, Larry Wall

    Binary build 816 [255195] provided by ActiveState http://www.ActiveState.com
    Built Mar 1 2006 18:00:52
    supports
    Tk-TableMatrix 1.21: up to date. from the ActiveState repositories.

    I just upgraded. I was again going to try TableMatrix, but thought I would google for examples. I found yours.

    I clipped and saved your sample, and I get this:
    c:\…\tk ==>tablematrixtest.pl
    Had to create Tk::XlibVtab unexpectedly at C:/Perl/lib/DynaLoader.pm line 253.
    2eb48f4 is not a hash at C:/Perl/site/lib/Tk/Widget.pm line 190.

    abnormal program termination

  4. prantanu says:

    Hi All,
    I am getting a strange problem with spreadsheet.
    When the number of rows is greater than 1000, then the program
    is being terminated on clicking the mouse on the title bar.
    But it is working fine with less than 1000 rows.

    My program is something like this:

    $customize_spreadsheet_New_Upper = $customize_dlg_topFrame_New->Scrolled(
    ‘Spreadsheet’,-rows=>21, -cols => 11,
    -width => 6, -height => 6,
    -titlerows => 1, -titlecols => 1,
    -variable => $arrayVar,
    -selectmode => ‘extended’,
    -resizeborders => ‘both’,
    -drawmode=> ‘compatible’,
    -titlerows => 1,
    -titlecols => 1,
    -bg => ‘white’,
    )->pack(qw/-expand 1 -fill both -padx 10 -pady 5/);

    $arrayVar->{“0,1”} = “Value”;

    Now I am inserting into it in the following way.
    my $used_rows_new_upper = 0;

    sub updateSpreadSheetData{
    for( 0..1000){
    insertIntoSpreadSheet();
    }
    }

    sub insertIntoSpreadSheet{
    $used_rows_new_upper++;
    $customize_spreadsheet_New_Upper->insertRows($used_rows_new_upper,1);
    $arrayVar->{“$used_rows_new_upper,1”} = “Value1”;
    }

    I don’t know whether it is a bug of TK or mine.
    Please let me know in case of any issue.
    I am waiting for your reply.

    Thanks & regards,
    Prantanu

Leave a Reply

Your email address will not be published. Required fields are marked *