For Programmers: Free Programming Magazines  


Home > Archive > PerlTk > January 2005 > Text Widget - extra leading space on all lines after first line.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Text Widget - extra leading space on all lines after first line.
bayxarea-usenet@yahoo.com

2005-01-24, 4:00 pm

I am inserting an array of text into a text widget and I keep noticing
that on all lines after the first line keep gettig a leading space. Is
this working as designed? Is there some sort of indentation? My array
has newline characters at then end - but I can't seem to figure this
out. I have even tried printing each
->$line<-
to see if there is a space in there anywhere - and there is not.
Any clues?

Thanks,

John

bayxarea-usenet@yahoo.com

2005-01-24, 4:00 pm

Ok - I just found that if I loop through each element in the array and
insert it one at a time then I don't get the leading space.
The docs only indicate a lmargin2 option that seems similar to this but
this option is not set on my widget and when I try to set it - it
complains that this is not a valid option. Go figure?

Can someone explain this behaviour and if its found that you should not
attempt to insert an entire array of text - instead just one line at a
time?

John

Marc Dashevsky

2005-01-24, 4:00 pm

In article <1106581742.419371.241560@f14g2000cwb.googlegroups.com>, bayxarea-
usenet@yahoo.com says...
> I am inserting an array of text into a text widget and I keep noticing
> that on all lines after the first line keep gettig a leading space. Is
> this working as designed? Is there some sort of indentation? My array
> has newline characters at then end - but I can't seem to figure this
> out. I have even tried printing each
> ->$line<-
> to see if there is a space in there anywhere - and there is not.
> Any clues?


Well, I couldn't replicate it, but that's not surprising since there
are any number of ways one can imagine to test this. Why don't *you*
post a *small*, *working* program that demonstrates the problem.
Then we may be able to help better, and you may even figure out the
solution while preparing the example.

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

2005-01-25, 3:59 pm

bayxarea-usenet@yahoo.com wrote:
> I am inserting an array of text into a text widget and I keep noticing
> that on all lines after the first line keep gettig a leading space. Is
> this working as designed? Is there some sort of indentation? My array
> has newline characters at then end - but I can't seem to figure this
> out. I have even tried printing each
> ->$line<-
> to see if there is a space in there anywhere - and there is not.
> Any clues?
>
> Thanks,
>
> John
>


I have run into this before and suspect that it is a artifact of passing
a double quoted array. You get similar behaviour when printing a double
quoted array. Unfortunately, you can't pass a unquoted array to the
insert method because it will assume every other element is a tag name,
so only half of the items will show up.

Probably your best bet is to just iterate over the array, inserting
each element in turn.

Here's a short example script demonstrating what is going on.


use strict;
use warnings;
use Tk;
my @array;

push @array, "$_\n" for (1..6);

my $top = MainWindow->new;
my $text = $top->Text->pack;

# Set up some bogus tags for example purposes
$text->tagConfigure('2', -background => 'green');
$text->tagConfigure('4', -background => 'blue');
$text->tagConfigure('6', -background => 'red');

$text->insert('end',"@array");
# Bad, you get an extra space before each line after
# the first for, I suspect, the same reason you do
# when you print a quoted array.

$text->insert('end',"\n\n");

$text->insert('end',@array);
# Bad, @array is treated as a text=>tag list
# so you only get every other line from the array.

$text->insert('end',"\n\n");

$text->insert('end',$_) for @array;
# This is probably what you want.

MainLoop;
plugh

2005-01-25, 3:59 pm

On Mon, 24 Jan 2005 08:06:23 -0800, bayxarea-usenet wrote:

> Ok - I just found that if I loop through each element in the array and
> insert it one at a time then I don't get the leading space.


Seems like you'd want to:

$text->insert(join('', @array));

No?

Regards,

plugh
Joseph Baldassini

2005-01-25, 3:59 pm

bayxarea-usenet@yahoo.com wrote:
> I am inserting an array of text into a text widget and I keep noticing
> that on all lines after the first line keep gettig a leading space. Is
> this working as designed? Is there some sort of indentation? My array
> has newline characters at then end - but I can't seem to figure this
> out. I have even tried printing each
> ->$line<-
> to see if there is a space in there anywhere - and there is not.
> Any clues?
>
> Thanks,
>
> John
>


If you're doing something like this:

$widget->insert('end',"@array");

Then do this first:

$" = '';

That should eliminate those leading spaces.


John W. Krahn

2005-01-25, 8:58 pm

bayxarea-usenet@yahoo.com wrote:
> I am inserting an array of text into a text widget and I keep noticing
> that on all lines after the first line keep gettig a leading space. Is
> this working as designed? Is there some sort of indentation? My array
> has newline characters at then end - but I can't seem to figure this
> out. I have even tried printing each
> ->$line<-
> to see if there is a space in there anywhere - and there is not.
> Any clues?


perldoc -q "Why do I get weird spaces when I print an array of lines"



John
--
use Perl;
program
fulfillment
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com