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

Re-sizable table with left-justified columns

69 views
Skip to first unread message

Henry Law

unread,
Nov 13, 2009, 10:30:39 AM11/13/09
to
I'm trying to code a simple "display properties" application in Perl::Tk
and cannot find how to do what ought to be perfectly straightforward.

I want to display a list of properties of a file in a tabular form like
this (warning: fixed-pitch font assumed):
|<--- RH edge of window
Prop 1 Value of prop 1 |
Property 2 Val 2 |
P3 Value of this long p|
Propy 4 4 |
|

In such a way that if the form is resized the left-alignment will be
maintained, thus:

|
Prop 1 Value of prop 1 |
Property 2 Val 2 |
P3 Value of this long property |
Propy 4 4 |
|

Whereas what I get tends to be some variant of roughly this:

|
Prop 1 Value of prop 1 |
Property 2 Val 2 |
P3 Value of this long property |
Propy 4 4 |
|

I've Googled diligently, and tried the suggestion of putting each label
inside a frame, but not got it right. I've tried a vertical stack of
frames, each with two frames packed horizontally and labels inside them.
I've tried Tk::Table. I've tried grid instead of pack. My least
failed attempts tend to look OK as the window comes up, but go out of
whack when I re-size it.

I can post code if it helps you to help me, but right now there are so
many failed variants that I'd not know which one to post.

Why is this so difficult? I could have had it done in V**ual B**ic but
I don't want to.

--

Henry Law Manchester, England

Steve C

unread,
Nov 13, 2009, 1:06:54 PM11/13/09
to

Have you tried a single Frame containing a grid of Label widgets?
Specify -sticky=>"w" for each one, since the default is to center.
You can use gridColumnconfigure -weight=>0 to prevent the left
column from resizing when the Frame size is changed.

Something like:

#!/usr/bin/perl
use strict;
use warnings;
use Tk;
use Tk::widgets qw/Label/;

my $mw = new Tk::MainWindow;
$mw->title('Grid test');

my $val='This';
my $f = $mw->Frame()->pack();

foreach (1..3) {
$f->Label( -text=>'Left')->grid(
$f->Label( -text=>$val),
-sticky => 'w');
$val .= ' more';
}

$f->gridColumnconfigure(0, -weight=>0);
$f->gridColumnconfigure(1, -weight=>1);

Tk::MainLoop;

Henry Law

unread,
Nov 14, 2009, 12:58:01 PM11/14/09
to
Steve C wrote:

> Henry Law wrote:
>> I want to display a list of properties of a file in a tabular form

> Have you tried a single Frame containing a grid of Label widgets?


> Specify -sticky=>"w" for each one, since the default is to center.
> You can use gridColumnconfigure -weight=>0 to prevent the left
> column from resizing when the Frame size is changed.

<snipped example>

No, I hadn't thought of that and I like the idea. I need to tweak your
sample code a little because at present it still suffers from my
previous bugbear that odd things happen when it's resized (see
http://www.lawshouse.org/download/original.jpg and
http://www.lawshouse.org/download/resized.jpg ) but they're much closer
to what I want and I'm sure I'll be able to solve the problem with this
new line of approach.

Thanks, Steve.

Oliver 'ojo' Bedford

unread,
Nov 15, 2009, 1:07:12 PM11/15/09
to
Am Fri, 13 Nov 2009 15:30:39 +0000 schrieb Henry Law:

> I'm trying to code a simple "display properties" application in Perl::Tk
> and cannot find how to do what ought to be perfectly straightforward.

What about

my $f1 = $mw->Frame()->pack(
-fill => 'x',
-side => 'top');
$f1->Label(
-text => 'Left',
-justify => 'left'
)->pack(
-side => 'left');
$f1->Label(
-text => 'Too long text put here',
-justify => 'left',
-anchor => 'w',
-width => 5
)->pack(
-side => 'left',
-fill => 'x',
-expand => 1);

Change the -width option to the value you want.

Add a similar frame for every property to be displayed (order does
matter).

Oliver

Henry Law

unread,
Nov 24, 2009, 3:10:14 PM11/24/09
to
Oliver 'ojo' Bedford wrote:
> Am Fri, 13 Nov 2009 15:30:39 +0000 schrieb Henry Law:
>
>> I'm trying to code a simple "display properties" application in Perl::Tk
>> and cannot find how to do what ought to be perfectly straightforward.
>
> What about
>
> my $f1 = $mw->Frame()->pack(
> -fill => 'x',
> -side => 'top');
> $f1->Label(

... etc

Worked really well, thank you.

Now a related question, though perhaps ought to be a new topic: how can
I compute the width of the string in a label? Rather than set the width
to some fixed value it would be nice to do something like this

my $max_width = label_width( $longest_string );
$mw->label( -text=>$string, -width=>$max_width );

Mr G Oogle couldn't find anything useful, though structuring a query was
a challenge.

Oliver 'ojo' Bedford

unread,
Nov 24, 2009, 4:44:50 PM11/24/09
to
Am Tue, 24 Nov 2009 20:10:14 +0000 schrieb Henry Law:

>
> Now a related question, though perhaps ought to be a new topic: how can
> I compute the width of the string in a label? Rather than set the width
> to some fixed value it would be nice to do something like this
>
> my $max_width = label_width( $longest_string ); $mw->label(
> -text=>$string, -width=>$max_width );
>

I'm not sure, but I think the problem lies in the way the -width is
handled by Tk. If the Label contains a string the width is interpreted as
number of characters. This is of course not a valid measure for the
geometrical dimensions of a non-proportional font (compare 'iii' with
'mmm' or 'WWW'.

I haven't found a way to submit a 'real' screen dimension to the
Label widget when using text.

You can measure the screen dimensions of a text in a specific
font, see Tk::font.

Oliver

Henry Law

unread,
Nov 25, 2009, 3:16:41 AM11/25/09
to
Oliver 'ojo' Bedford wrote:
> Am Tue, 24 Nov 2009 20:10:14 +0000 schrieb Henry Law:
>
>> Now a related question, though perhaps ought to be a new topic: how can
>> I compute the width of the string in a label? Rather than set the width
>> to some fixed value it would be nice to do something like this

>

> You can measure the screen dimensions of a text in a specific
> font, see Tk::font.

Oh, OK - didn't find that. I expect I can lash something up using that,
perhaps with a fixed factor found by trial and error. Thank you again.

0 new messages