Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I best display my array of arrays?

34 views
Skip to first unread message

Mazur

unread,
Mar 17, 2012, 3:53:32 AM3/17/12
to
I'm writing a 4-player card game in perl/Tk. A set would consist of
16 games, each with scores for both of the two teams of two, direct
points and honours. After each 4th game I would want to present the
running total, and after the 16th game, again the individual totals
for each of the four columns and then a grand total for each pair.

Now I'm new to perl/Tk, I have part of the overall GUI in place, but
this bit puzzles me: how would I best display the scores of the 16
games? I'd want lines drawn between the four columns, and below each
fourth game, and present the numbers as described.

Do I use 4*4 individual Listboxes? Should I concatenate the values
into a single line and display that? I'm sure there are wonderful
examples out there, somewhere, but the right one would be hard to find
out of the sheer number of them.

If any of you would have advice, it would be hugely appreciated.

$Bill

unread,
Mar 17, 2012, 8:17:26 AM3/17/12
to
Not sure I really follow what it looks like - maybe some ascii art
would have helped. :)

I'm thinking 'grid' at the moment, possibly a frame containing a
grid of text widgets would handle most of what you need.

I used something like that to make a calendar for the year (actually
a grid of 12 text widgets (with dynamic sizing of rows and columns
so you could make it taller or wider based on a commandline switch)
and each containing an 8x7 matrix of text (1 month):

2012 CALENDAR
JANUARY ...
S M T W T F S
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31

I had an entry widget below the calendar where you could change the
year and have it redraw the calendar for the new year.

Sample code for creating my calendar grid (won't work as is without
the rest of the code, but should get the idea across):

my $mw = new MainWindow;

# create calendar title hdr - eg: 2012 Calendar - text added later

$hdr = $mw->Label(
-justify => 'center',
-font => '-*-Arial-Medium-R-Normal--*-240-*-*-*-*-*-*',
-height => 1,
)->pack;

# create a frame with a grid of text widgets

my $lf1 = $mw->Frame(-relief => 'ridge')->pack(-fill => 'both', -expand => 1);

# create 12 'month' text boxes

for (my $ii = 0; $ii < 12; ++$ii) {

my $num = sprintf "%02u", $ii;
my $et = "\$t$num = \$lf1->Text(" .
"-font => '-*-Courier-Medium-R-Normal--*-120-*-*-*-*-*-*', " .
"-width => $line_len," .
"-height => $lines_per_month);";
eval $et; # dynamically size the widgets
DLOG "TextBox ($ii) '$et'\n";
if ($@) { LOG "Textbox eval failed ($@)\n"; die; }
}

# form into grid dynamically using length/width ratio argument $months_per_row
# eg: months_per_row = rows x cols - 6=6x2, 4=4x3, 3=3x4, 2=2x6

for (my $ii = 0; $ii < 12; ++$ii) {
my $n = sprintf "%02u", $ii; # grid square widget number
my $r = int ($ii / $months_per_row); # rows
my $c = $ii - ($r * $months_per_row); # cols
my $et = "Tk::grid(\$t$n, -row => $r, -column => $c, -sticky => 'e');";
eval $et; # dynamically create the grid
DLOG "Grid ($ii) '$et'\n";
if ($@) { LOG "Grid eval failed ($@)\n"; die; }
};

The week lines were just a matter of figuring out which day numbers go in
which columns and create each line and insert into the proper text widget
in the correct row (after the 2 header lines).

I'm no Tk expert, but hopefully it gets you going or gives you an idea.



Message has been deleted

Mazur

unread,
Mar 17, 2012, 11:30:31 AM3/17/12
to

I am hoping for a result not unlike this:

| Us | Them |
1. 20 | 123 | 546 | 0
2. 20 | 123 | 546 | 50
3. 20 | 123 | 546 | 0
4. 20 | 123 | 546 | 50
--------+-------+-------+----
80 | 492 | 2184 | 100
5. 0 | 300 | 0 | 20
6. | 111 | 111 | 0
7. | 111 | 111 | 0
8. | 111 | 111 | 0
--------+-------+-------+----
xxx | xxx | xxxx | xxx

etcetera.

Marc Dashevsky

unread,
Mar 17, 2012, 4:27:27 PM3/17/12
to
In article <8a7ed52d-c79b-499c...@p6g2000yqi.googlegroups.com>, loki...@gmail.com says...
Look at the grid() geometry manager. Example:

use strict;
use warnings;
use Tk;
my @columns = ('a' .. 'd');
my @colors = (qw/red blue yellow green white/);
my $mw = tkinit;

for my $r (0 .. 4) {
for my $c (0 .. $#columns) {
$mw->Label(
-text => "Row $r, Column $columns[$c]",
-bg => $colors[($c + $r) % scalar(@colors)],
)->grid(
-row => $r,
-column => $c,
);
}
}

MainLoop;


--
Go to http://MarcDashevsky.com to send me e-mail.

Mazur

unread,
Mar 17, 2012, 7:34:57 PM3/17/12
to
On Mar 17, 9:27 pm, Marc Dashevsky <use...@MarcDashevsky.com> wrote:
> In article <8a7ed52d-c79b-499c-9152-44b3c4e29...@p6g2000yqi.googlegroups.com>, lokima...@gmail.com says...
The placement is not my problem, I already used grid to place the many
option radiobuttons I require, it's purely the sensible way to display
the contents of the array of arrays. For which you suggest individual
labels, rather than one, four or sixteen Listbox(es).

$Bill

unread,
Mar 17, 2012, 9:55:48 PM3/17/12
to
Simple example using sprintf inserts to text widget line by line
(cut, paste and try):

use strict;
use warnings;
use Tk;

my $mw = tkinit;

# Assuming this data:
# | Us | Them |
# 1. 20 | 123 | 546 | 0
# 2. 20 | 123 | 546 | 50
# 3. 20 | 123 | 546 | 0
# 4. 20 | 123 | 546 | 50
# --------+-------+-------+----
# 80 | 492 | 2184 | 100
# 5. 0 | 300 | 0 | 20
# 6. | 111 | 111 | 0
# 7. | 111 | 111 | 0
# 8. | 111 | 111 | 0
# --------+-------+-------+----
# xxx | xxx | xxxx | xxx

# data array

my @A = ([20, 123, 546, 0], [20, 123, 546, 50], [20, 123, 546, 0],
[20, 123, 546, 50], [0, 300, 0, 20], [0, 111, 111, 0], [0, 111, 111, 0],
[0, 111, 111, 0], );

my $f1 = $mw->Frame->pack;
my $t1 = $f1->Text(-width => 32, -height => 20,
-font => '-*-Courier-Medium-R-Normal--*-120-*-*-*-*-*-*',
)->pack;

# sprintf fmt string

my $fmt = " %3.3s %4.4s | %4.4s | %4.4s | %4.4s \n";
my $sep = "------";

# output the hdr

my $txt = sprintf $fmt, '', '', 'Us', 'Them', '';
$t1->insert('1.0', $txt);

# output line by line

my @T = (0, 0, 0, 0);
for (my $ii = 0; $ii < @A; ++$ii) {

my $num = sprintf "%2u.", $ii+1;
$txt = sprintf $fmt, $num, $A[$ii][0], $A[$ii][1], $A[$ii][2],
$A[$ii][3];
$t1->insert('end', $txt);

$T[$_] += $A[$ii][$_] for (0 .. 3);

if ($ii % 4 == 3) {
$txt = sprintf $fmt, $sep, $sep, $sep, $sep, $sep;
$t1->insert('end', $txt);
$txt = sprintf $fmt, '', $T[0], $T[1], $T[2], $T[3];
$t1->insert('end', $txt);
}
}

MainLoop;

__END__

Mazur

unread,
Mar 18, 2012, 11:00:43 AM3/18/12
to
Again, thank you for your suggestion.
In the mean time I have found Tk::Hlist and TK::Tlist as possible
alternatives, and have been contemplating a canvas to simply place the
headers and scores where I want them, and draw lines.
I'm starting to think there's no obvious Tk widget that jumps out as
(one of the) best ways to do this, and that I simply will have to try
out and find what suits me best.

$Bill

unread,
Mar 18, 2012, 7:54:46 PM3/18/12
to
On 03/18/2012 08:00, Mazur wrote:
>
> Again, thank you for your suggestion.
> In the mean time I have found Tk::Hlist and TK::Tlist as possible
> alternatives, and have been contemplating a canvas to simply place the
> headers and scores where I want them, and draw lines.
> I'm starting to think there's no obvious Tk widget that jumps out as
> (one of the) best ways to do this, and that I simply will have to try
> out and find what suits me best.

One of the original premises (and the motto of Perl):

"There's more than one way to do it."

I might add the word 'always' before more. :)

Mazur

unread,
Mar 19, 2012, 11:21:26 PM3/19/12
to
I knew that already about perl, and am starting to see it about Tk.
0 new messages