CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Tk - Changing the dimensions for a table.

 



PapaGeek
User

Mar 22, 2014, 10:48 AM

Post #1 of 11 (8768 views)
Tk - Changing the dimensions for a table. Can't Post

I found some code on line that allows me to create a Tk table in a frame and make it scrollable. The code I am writing can create a number of user definable different tables of various sizes. I basically want the user to be about to select what table they wish to see and have that table appear in the frame/table with the column headers and the first two columns fixed and the rest scrollable.

One option might be to create the table way larger than I need and just blank out the extra rows and columns as I update the table content for each request, but it would be nice if the scroll bars showed the bottom of the table when pulled to the bottom, etc.


How do I write the second and third lines of the pseudo code at the bottom of this example?

When I run the program with the two lines before the pseudo code un-commented, I get the following error:

Code
 Tk::Error: Can't modify non-lvalue subroutine call at …tKTable.pl line 53.

What is the proper syntax for what I want to do?

Code
 #!/usr/bin/perl -w 

use Tk;
use Tk::Table;
use strict;

my $mw = MainWindow->new;
$mw->geometry("475x225");
#$mw->resizable(0,0);
$mw->title("Table Example");
$mw->Label( -text=>"Select what report to display" )->pack();
$mw->Button( -text=>"show table 1", -command =>[\&displayTable, 1 ])->pack();
$mw->Button( -text=>"show table 2", -command =>[\&displayTable, 2 ])->pack();

my $table_frame = $mw->Frame()->pack();
my $table = $table_frame->Table(-columns => 18,
-rows => 19,
-fixedrows => 1,
-fixedcolumns => 1,
-scrollbars => 'se',
-relief => 'raised');

foreach my $col (1 .. 18)
{
my $tmp_label = $table->Label(-text => "COL " . $col, -width => 8, -relief =>'raised');
$table->put(0, $col, $tmp_label);
}

foreach my $row (1 .. 18)
{
foreach my $col (1 .. 18)
{
my $tmp_label = $table->Label(-text => $row . "," . $col,
-padx => 2,
-anchor => 'w',
-background => 'white',
-relief => "groove");
$table->put($row, $col, $tmp_label);
}
}
$table->pack();

my $button_frame = $mw->Frame( -borderwidth => 4 )->pack();
$button_frame->Button(-text => "Exit", -command => sub {exit})->pack();

MainLoop;

sub displayTable
{
my ($tableID) = @_;
print "displaying table $tableID\n";
#my $rows = 25;
#$table->rows = $rows;
=speudoCode
my (@tableData,$rows,$columns) = getTableData($tableID);

$table->columns = $columns;
$table->rows = $rows + 1; # create extra row for headers

foreach row
for each column
my $tmp_label = $table->Label(-text => "get data from my table ($row, $col)",
-padx => 2,
-anchor => 'w',
-background => 'white',
-relief => "groove");
$table->put($row, $col, $tmp_label);

=cut
}



FishMonger
Veteran / Moderator

Mar 22, 2014, 12:18 PM

Post #2 of 11 (8765 views)
Re: [PapaGeek] Tk - Changing the dimensions for a table. [In reply to] Can't Post


Code
#!/usr/bin/perl 

use Tk;
use Tk::Table;
use strict;
use warnings;

my $mw = MainWindow->new;
$mw->geometry("475x225");
#$mw->resizable(0,0);
$mw->title("Table Example");
$mw->Label( -text=>"Select what report to display" )->pack();
$mw->Button( -text=>"show table 1", -command =>[\&displayTable, 1 ])->pack();
$mw->Button( -text=>"show table 2", -command =>[\&displayTable, 2 ])->pack();

my $table_frame = $mw->Frame()->pack();
my %table;

for my $num (1..2) {
$table{$num} = $table_frame->Table(-columns => 18,
-rows => 19,
-fixedrows => 1,
-fixedcolumns => 1,
-scrollbars => 'se',
-relief => 'raised');

foreach my $col (1 .. 18)
{
my $tmp_label = $table{$num}->Label(-text => "TBL $num/COL $col",
-width => 10,
-relief =>'raised');

$table{$num}->put(0, $col, $tmp_label);
}

foreach my $row (1 .. 18)
{
foreach my $col (1 .. 18)
{
my $tmp_label = $table{$num}->Label(-text => "$row,$col",
-padx => 2,
-anchor => 'w',
-background => 'white',
-relief => "groove");

$table{$num}->put($row, $col, $tmp_label);
}
}
#$table{$num}->pack();
}

my $button_frame = $mw->Frame( -borderwidth => 4 )->pack();
$button_frame->Button(-text => "Exit", -command => sub {exit})->pack();

MainLoop;

sub displayTable
{
my ($tableID) = @_;
print "displaying table $tableID\n";

$table{$_}->packForget for keys %table;
$table{$tableID}->pack;



#my $rows = 25;
#$table->rows = $rows;
=speudoCode
my (@tableData,$rows,$columns) = getTableData($tableID);

$table->columns = $columns;
$table->rows = $rows + 1; # create extra row for headers

foreach row
for each column
my $tmp_label = $table->Label(-text => "get data from my table ($row, $col)",
-padx => 2,
-anchor => 'w',
-background => 'white',
-relief => "groove");
$table->put($row, $col, $tmp_label);

=cut
}



FishMonger
Veteran / Moderator

Mar 22, 2014, 12:57 PM

Post #3 of 11 (8762 views)
Re: [PapaGeek] Tk - Changing the dimensions for a table. [In reply to] Can't Post

As I previously mentioned, IMO menu items should be in a menu bar instead of buttons. With that in mind, I made a few adjustments to your code showing the direction I'd take.


Code
#!/usr/bin/perl 

use Tk;
use Tk::Table;
use strict;
use warnings;

my $mw = MainWindow->new;
$mw->geometry("475x225");
$mw->title("Table Example");

create_menubar();

# I'd normally handle this informational label differently
# but kept it in for this example
$mw->Label(-text => 'Select what report to display from the "Reports" menu')->pack();

my $table_frame = $mw->Frame()->pack();
my %table;

for my $id (1..2) {
$table{$id} = $table_frame->Table(
-columns => 18,
-rows => 19,
-fixedrows => 1,
-fixedcolumns => 1,
-scrollbars => 'se',
-relief => 'raised'
);

foreach my $col (1 .. 18)
{
my $tmp_label = $table{$id}->Label(
-text => "TBL $id/COL $col",
-width => 12,
-relief =>'raised'
);

$table{$id}->put(0, $col, $tmp_label);
}

for my $row (1 .. 18)
{
for my $col (1 .. 18)
{
my $tmp_label = $table{$id}->Label(
-text => "$row,$col",
-padx => 2,
-anchor => 'w',
-background => 'white',
-relief => "groove"
);

$table{$id}->put($row, $col, $tmp_label);
}
}
}


MainLoop;

sub create_menubar {
my $main_menu = $mw->Menu();
$mw->configure(-menu => $main_menu);

my $report_menu = $main_menu->cascade(
-label => "Reports",
-underline => 0,
-tearoff => 0
);
$report_menu->command(
-label => "Report 1",
-underline => 0,
-command => [\&displayTable, 1]
);
$report_menu->command(
-label => "Report 2",
-underline => 0,
-command => [\&displayTable, 2]
);
$main_menu->command(
-label => "Exit",
-underline => 0,
-command => sub{exit}
);
}

sub displayTable
{
my ($tableID) = @_;
print "displaying table $tableID\n";

$table{$_}->packForget for keys %table;
$table{$tableID}->pack;
}



PapaGeek
User

Mar 22, 2014, 1:21 PM

Post #4 of 11 (8761 views)
Re: [FishMonger] Tk - Changing the dimensions for a table. [In reply to] Can't Post

Thanks FishMonger, I’ve played with your new code, took out the “for my $num (1..2)” and replace it with separate code to create both tables different sizes. By also inserting a sleep(3) line I can see how the “$table{$_}->packForget for keys %table;” line removes that table from the window and the “$table{$tableID}->pack;” line displays the alternate table.

I also added a third button that calls a single line sub “$table{1}->put(3, 5, "test");” that shows that the content of the table can be changed on the fly for both the displayed table and the hidden one.

I can use what you gave me but creating a series of tables in all of the “expected” sizes and switch to the one that will fit the required data in the smallest space, but that solution isn’t perfect.

What I really want to do (in the terms you just provided to me) is to build the initial table at some random size and don’t display it, then when the user needs to see a given table at some random size, “packForget” whatever might currently be display, (back to my original request) change the dimensions of the table to the exact size I need, and the re-“pack” to table to make it visible.

Basically a single table where the number of rows and columns can be changed.

Another option would be, if this is possible, if a table is visible, “packForget” it and delete it, and then create a new table in the exact dimensions required, add it to the frame, and re-“pack” it for display.

Can a table be deleted and recreated on the fly that way?


FishMonger
Veteran / Moderator

Mar 22, 2014, 1:22 PM

Post #5 of 11 (8759 views)
Re: [PapaGeek] Tk - Changing the dimensions for a table. [In reply to] Can't Post

I see that I forgot about your main question of re-dimensioning. I was focused on fixing it to display different tables based on the button selection.

I'm not exactly sure that I understand your question regarding the dimensions. As I see it, the tables should already be fully built but not shown until the user makes their menu selection. The both version that I posted will do that, but if that's not what you're needing, please clarify your question.


PapaGeek
User

Mar 22, 2014, 1:41 PM

Post #6 of 11 (8755 views)
Re: [FishMonger] Tk - Changing the dimensions for a table. [In reply to] Can't Post

The purpose of the program is to display a person’s 401K/ IRA holdings in a sorted order with the best possible investments at the top of the list. The 401K at my company may allow me to invest in 20 funds while my wife’s 401K allows her to invest in a different 15 funds, so each of the tables would have a different number of rows.

My 401K performance page shows me how the funds are doing for YTD, the last 6 months, 1 year, and 3 years. My wife’s performance page does not include YTD but does include 3 months and 5 years. So, the number of columns and the column headings would have to change for each table/chart that I want to display.

Once I get the program working, without knowing how much each person has invested, I may wind up producing results for my neighbor, her brother and my sister, all of which may have a different number of rows and columns.

There is an alternative to the table. I can easily produce the tables as Intranet HTML pages. Does Perl have the ability to display a widget that is say 700 pixels wide and 500 pixels high into which I can display an HTML file that is scrollable in both directions? And of course, the widget has to be able to be given a different local path to display a different file upon a user’s request! In fact, this alternative is even better because it allows me more flexibility in formatting.


(This post was edited by PapaGeek on Mar 22, 2014, 1:46 PM)


FishMonger
Veteran / Moderator

Mar 22, 2014, 1:42 PM

Post #7 of 11 (8751 views)
Re: [PapaGeek] Tk - Changing the dimensions for a table. [In reply to] Can't Post


Quote
Basically a single table where the number of rows and columns can be changed.

Adding rows and columns is no problem as that's what your code is already doing.

I'm not sure about deleting individual rows and columns. I'd need to read the related docs and run a few tests.


Quote
Can a table be deleted and recreated on the fly

Yes, but instead of deleting the table, you might want to use the $table->clear method, which I believe removes the content and leaves an empty table to be rebuilt.


PapaGeek
User

Mar 22, 2014, 1:45 PM

Post #8 of 11 (8749 views)
Re: [FishMonger] Tk - Changing the dimensions for a table. [In reply to] Can't Post

We posted at the same time, please read my last reply, can I display an HTML file in a widget easily?


FishMonger
Veteran / Moderator

Mar 22, 2014, 1:47 PM

Post #9 of 11 (8745 views)
Re: [PapaGeek] Tk - Changing the dimensions for a table. [In reply to] Can't Post

Here are 3 table related modules you should look over.

TableMatrix - Create and manipulate tables
http://search.cpan.org/~cerney/Tk-TableMatrix-1.23/pod/TableMatrix.pod

Tk::TableMatrix::Spreadsheet - Table Display with Spreadsheet-like bindings.
http://search.cpan.org/~cerney/Tk-TableMatrix-1.23/TableMatrix/Spreadsheet.pm

Tk::TableMatrix::SpreadsheetHideRows - Table Display with selectable hide/un-hide of rows
http://search.cpan.org/~cerney/Tk-TableMatrix-1.23/TableMatrix/SpreadsheetHideRows.pm


(This post was edited by FishMonger on Mar 22, 2014, 1:48 PM)


FishMonger
Veteran / Moderator

Mar 22, 2014, 1:50 PM

Post #10 of 11 (8743 views)
Re: [PapaGeek] Tk - Changing the dimensions for a table. [In reply to] Can't Post


In Reply To
We posted at the same time, please read my last reply, can I display an HTML file in a widget easily?


Yes, you can display html content, but I haven't done it myself. There are examples in the Mastering/Tk book, but I can't find my copy.

Tk::HyperText - An ROText widget which renders HTML code.
http://search.cpan.org/~kirsle/Tk-HyperText-0.09/lib/Tk/HyperText.pm


(This post was edited by FishMonger on Mar 22, 2014, 1:52 PM)


PapaGeek
User

Mar 22, 2014, 1:55 PM

Post #11 of 11 (8740 views)
Re: [FishMonger] Tk - Changing the dimensions for a table. [In reply to] Can't Post

Thanks, time for dinner, but I will try the hypertext after I'm done eating.

Again thanks so much for all the help you have given me in learning this new language!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives