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

Tk::Label autowrap

143 views
Skip to first unread message

Petr Vileta (fidokomik)

unread,
Oct 11, 2008, 12:27:44 PM10/11/08
to
I have placed few Labels into main window but need to change -wraplength option
for all Labels every time the mainwindow is resized. The program bellow do what
I need but have anybody a better idea to avoid too many binds?

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

our $mw = Tk::MainWindow->new();
my $lab1=$mw->Label(-text => 'abc abc abc abc abc abc abc abc abc abc abc abc',
-wraplength => 20)->pack;
$lab1->bind('<Configure>',\&rewrap);

my $lab2=$mw->Label(-text => 'abc abc abc abc abc abc abc abc abc abc abc abc',
-wraplength => 20)->pack;
$lab2->bind('<Configure>',\&rewrap);

MainLoop;

sub rewrap
{
$mw->Widget($_[0]->{_TkValue_})->configure(-wraplength => ($mw->width > 20 ?
$mw->width : 21) - 20);
}


--
Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail.
Send me your mail from another non-spammer site please.)
Please reply to <petr AT practisoft DOT cz>

zentara

unread,
Oct 12, 2008, 1:42:12 PM10/12/08
to
On Sat, 11 Oct 2008 18:27:44 +0200, "Petr Vileta \(fidokomik\)"
<sto...@practisoft.cz> wrote:

>I have placed few Labels into main window but need to change -wraplength option
>for all Labels every time the mainwindow is resized. The program bellow do what
>I need but have anybody a better idea to avoid too many binds?
>
>#!/usr/bin/perl
>use strict;
>use Tk;
>use Tk::Label;
>
>our $mw = Tk::MainWindow->new();
>my $lab1=$mw->Label(-text => 'abc abc abc abc abc abc abc abc abc abc abc abc',
> -wraplength => 20)->pack;
>$lab1->bind('<Configure>',\&rewrap);
>
>my $lab2=$mw->Label(-text => 'abc abc abc abc abc abc abc abc abc abc abc abc',
> -wraplength => 20)->pack;
>$lab2->bind('<Configure>',\&rewrap);
>
>MainLoop;
>
>sub rewrap
>{
>$mw->Widget($_[0]->{_TkValue_})->configure(-wraplength => ($mw->width > 20 ?
>$mw->width : 21) - 20);
>}

I don't really know what you are trying to do, but you may be getting
confused because the -wraplength option seems to be in pixels, where
-width is in chars.

See if this gives you ideas:


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

our $mw = Tk::MainWindow->new();

my $lab1=$mw->Label(-text => 'abc abc abc abc abc abc abc abc abc abc
abc abc',

-bg=>'white',
-width => 40,
-wraplength => 200,
)->pack(-expand=> 0, -fill=> 'x');

my $lab2=$mw->Label(-text => 'abc abc abc abc abc abc abc abc abc abc
abc abc',

-bg=>'lightblue',
-width => 40,
-wraplength => 200,
)->pack(-expand=> 0, -fill=> 'x');

MainLoop;

__END__


zentara

--
I'm not really a human, but I play one on earth.
http://zentara.net/Remember_How_Lucky_You_Are.html

Petr Vileta (fidokomik)

unread,
Oct 12, 2008, 8:12:42 PM10/12/08
to
zentara wrote:
> On Sat, 11 Oct 2008 18:27:44 +0200, "Petr Vileta \(fidokomik\)"
> <sto...@practisoft.cz> wrote:
>
>> #!/usr/bin/perl
>> use strict;
>> use Tk;
>> use Tk::Label;
>>
>> our $mw = Tk::MainWindow->new();
>> my $lab1=$mw->Label(-text => 'abc abc abc abc abc abc abc abc abc
>> abc abc abc', -wraplength => 20)->pack;
>> $lab1->bind('<Configure>',\&rewrap);
>>
>> my $lab2=$mw->Label(-text => 'abc abc abc abc abc abc abc abc abc
>> abc abc abc', -wraplength => 20)->pack;
>> $lab2->bind('<Configure>',\&rewrap);
>>
>> MainLoop;
>>
>> sub rewrap
>> {
>> $mw->Widget($_[0]->{_TkValue_})->configure(-wraplength =>
>> ($mw->width > 20 ? $mw->width : 21) - 20);
>> }
>
> I don't really know what you are trying to do, but you may be getting
> confused because the -wraplength option seems to be in pixels, where
> -width is in chars.
>
Hi zentara,
I not see any idea except -expand and -fill parameters in pack() :-) But this
not help me. I want to do some other.
When I not set -width for Label then width of Label is set to needed length to
view text but max to screen width (if mainwindow width is not set in other way).
So label's text is displayed in single row. When I shring mainwindow width I
want to set label -wraplength to mainwindow width - few pixels (border + small
x-spaces). Please try again my previous example and try to shring mainwindow
width ;-) You will se what I want to do. But, maybe, it can be done better.
In other words: I need the same effect as html code bellow when browser width is
resized to say 50 pixels :-)

<table width="100%">
<tr><td>bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla</td></tr>
<tr><td>bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla</td></tr>
<tr><td>bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla</td></tr>
<tr><td>bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla</td></tr>
</table>

zentara

unread,
Oct 13, 2008, 7:53:37 AM10/13/08
to
On Mon, 13 Oct 2008 02:12:42 +0200, "Petr Vileta \(fidokomik\)"
<sto...@practisoft.cz> wrote:


>Hi zentara,
>I not see any idea except -expand and -fill parameters in pack() :-) But this
>not help me. I want to do some other.
>When I not set -width for Label then width of Label is set to needed length to
>view text but max to screen width (if mainwindow width is not set in other way).
>So label's text is displayed in single row. When I shring mainwindow width I
>want to set label -wraplength to mainwindow width - few pixels (border + small
>x-spaces). Please try again my previous example and try to shring mainwindow
>width ;-) You will se what I want to do. But, maybe, it can be done better.
>In other words: I need the same effect as html code bellow when browser width is
>resized to say 50 pixels :-)
>
><table width="100%">
><tr><td>bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla</td></tr>
><tr><td>bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla</td></tr>
><tr><td>bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla</td></tr>
><tr><td>bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla</td></tr>
></table>

I think you will need to go to a Tk::TableMatrix to get table resizing.
Here is something to look at, it's not perfect, but may show you a way.
#!/usr/bin/perl
use warnings;
use strict;
use Tk;
use Tk::TableMatrix;

my $mw = MainWindow->new;

my $arrayVar = {};

my ($rows,$cols) = (2, 2);

foreach my $row (0..($rows-1)){
foreach my $col (0..($cols-1)){
$arrayVar->{"$row,$col"} = 'abcdefghijklmnopqrstuvwxyz';
}
}

my $t = $mw->TableMatrix(
-rows => 2,
-cols => 2,
-width => 10,
-height => 10,
-titlerows => 0,
-titlecols => 0,
-variable => $arrayVar,
#-coltagcommand => \&colSub,
#-browsecommand => \&brscmd,
-colstretchmode => 'all',
-rowstretchmode => 'all',
-drawmode => 'slow',
-wrap => 10,
)->pack(-expand=>1,-fill=>'both');

Tk::MainLoop;

Petr Vileta (fidokomik)

unread,
Oct 13, 2008, 7:41:26 PM10/13/08
to
zentara wrote:
> On Mon, 13 Oct 2008 02:12:42 +0200, "Petr Vileta \(fidokomik\)"
> <sto...@practisoft.cz> wrote:
>
>
>> In other words: I need the same effect as html code bellow when
>> browser width is resized to say 50 pixels :-)
>>
>> <table width="100%">
>> <tr><td>bla bla bla bla bla bla bla bla bla bla bla bla bla bla
>> bla</td></tr> <tr><td>bla bla bla bla bla bla bla bla bla bla bla
>> bla bla bla bla</td></tr> <tr><td>bla bla bla bla bla bla bla bla
>> bla bla bla bla bla bla bla</td></tr> <tr><td>bla bla bla bla bla
>> bla bla bla bla bla bla bla bla bla bla</td></tr> </table>
>
> I think you will need to go to a Tk::TableMatrix to get table
> resizing. Here is something to look at, it's not perfect, but may
> show you a way.
>
TableMatrix is "too big gun" for my purpose. I thought about HList or ROtext but
these modules are needlessly robust too. I'm in progress on some solution called
"package DynLabel". When I done it I will publish it here.
0 new messages