For Programmers: Free Programming Magazines  


Home > Archive > Tcl > July 2007 > text - going to specific line numbers.









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 - going to specific line numbers.
Dave (from the UK)

2007-07-26, 8:09 am

Hi,

I have a text box, 7 lines long:

set statsresults [text $w.statsresults -height 7]

and want to start writing half-way down the box (say line 4). but are
having difficulty is knowing exact what the line numbers refer to.


I've tried this:

$statsresults insert 4.0 "Text to insert on line 4"

but it normally sticks it on the first line, NOT the fourth as I want.

I think the issue is that once the box is created, there is nothing on
lines 1, 2 or 3, so line 4 does not exist, so nothing can be written to
line 4.

If I want to write at at specific line like this, is it necessary I
first put a newline on all lines above it i.e.



# Is this the best way to write to line 4?
for {set linenumber 0} {$linenumber<9} {incr linenumber} {
$statsresults insert $linenumber.0 "\n"
}
statsresults insert 4.0 "Text to insert on line 4"


Likewise, if I have something on all 7 lines, and I want to leave them
all intact except the 4th, how should I do that? If I delete the fourth
line with something like:

$statsresults delete 4.0 4.end

will that achieve it, or do all the other line numbers change, the fact
one has been deleted?

Perhaps instead I should write just a newline on the line I want to clear:

$statsresults insert 4.0 "\n"


--
Dave (from the UK)

Please note my email address changes periodically to avoid spam.
It is always of the form: month-year@althorne.org
Hitting reply will work for a few months only - later set it manually.

http://chessdb.sourceforge.net/ - a Free open-source Chess Database
Ron Fox

2007-07-26, 8:09 am

You can't insert text 'past end'. Where end is the
last line that has been inserted in the widget so you're
pretty well right, however, you can simplify your
scheme of extending the set of line sin the widget;

set nextBlankLine [$statsresults index end]

Will return a line.0 where line is the first 'unused'
line in the box... so you know how many lines are already
in the widget.

If you have an application that just needs a fixed number
of randomly accessed strings you may be better off with a
grid of label widgets.
RF

Dave (from the UK) wrote:
> Hi,
>
> I have a text box, 7 lines long:
>
> set statsresults [text $w.statsresults -height 7]
>
> and want to start writing half-way down the box (say line 4). but are
> having difficulty is knowing exact what the line numbers refer to.
>
>
> I've tried this:
>
> $statsresults insert 4.0 "Text to insert on line 4"
>
> but it normally sticks it on the first line, NOT the fourth as I want.
>
> I think the issue is that once the box is created, there is nothing on
> lines 1, 2 or 3, so line 4 does not exist, so nothing can be written to
> line 4.
>
> If I want to write at at specific line like this, is it necessary I
> first put a newline on all lines above it i.e.
>
>
>
> # Is this the best way to write to line 4?
> for {set linenumber 0} {$linenumber<9} {incr linenumber} {
> $statsresults insert $linenumber.0 "\n"
> }
> statsresults insert 4.0 "Text to insert on line 4"
>
>
> Likewise, if I have something on all 7 lines, and I want to leave them
> all intact except the 4th, how should I do that? If I delete the fourth
> line with something like:
>
> $statsresults delete 4.0 4.end
>
> will that achieve it, or do all the other line numbers change, the fact
> one has been deleted?
>
> Perhaps instead I should write just a newline on the line I want to clear:
>
> $statsresults insert 4.0 "\n"
>
>

tclcoder@nurdglaw.demon.co.uk

2007-07-26, 8:09 am

On 26 Jul, 10:24, "Dave (from the UK)" <see-my-signat...@see-
below.com> wrote:
> Hi,
>
> I have a text box, 7 lines long:
>
> set statsresults [text $w.statsresults -height 7]
>
> and want to start writing half-way down the box (say line 4). but are
> having difficulty is knowing exact what the line numbers refer to.
>
> I've tried this:
>
> $statsresults insert 4.0 "Text to insert on line 4"
>
> but it normally sticks it on the first line, NOT the fourth as I want.
>
> I think the issue is that once the box is created, there is nothing on
> lines 1, 2 or 3, so line 4 does not exist, so nothing can be written to
> line 4.


Quite right. After creation, you have a text box that, when managed
will occupy an area that is 7 lines long.

> If I want to write at at specific line like this, is it necessary I
> first put a newline on all lines above it i.e.
>
> # Is this the best way to write to line 4?
> for {set linenumber 0} {$linenumber<9} {incr linenumber} {
> $statsresults insert $linenumber.0 "\n"}
>
> statsresults insert 4.0 "Text to insert on line 4"


Yes and no. You'll have to create each line, but you only need do it
once. So on creation you want something like

set statusresults [text $w.statusresults -height 7]
pack $statusresults; # You need something like this or you
won't
# see the text widget
for {set i 1} {$i <= 7} {incr i} {
$statusresults insert end \n
}

And then to output something to line 4

$statusresults insert 4.0 "Some text on line 4"

> Likewise, if I have something on all 7 lines, and I want to leave them
> all intact except the 4th, how should I do that? If I delete the fourth
> line with something like:
>
> $statsresults delete 4.0 4.end
>
> will that achieve it, or do all the other line numbers change, the fact
> one has been deleted?


That will work. 4.end is just before the newline, so nothing else
changes. Continuing my example above

$statusresults delete 4.0 4.end
$statusresults insert "Some new text for line 4"

Should do the trick.

> Perhaps instead I should write just a newline on the line I want to clear:
>
> $statsresults insert 4.0 "\n"
>


If you do this, you will change the existing lines 4-7 to lines 5-8
which is almost certainly not what you want.

Note also that text widget line numbers start from one rather than
zero. Writing to line zero appears not to be an error, it just gets
silently converted to line one.

Finally, it looks as though you will be using your seven lines to
format things in a fixed way. Have you considered a set of 7 label
widgets, rather than a single 7-line text widget? It might be less
confusing to code.

Hope this helps,
Alan

Dave (from the UK)

2007-07-26, 8:09 am

tclcoder@nurdglaw.demon.co.uk wrote:

> Quite right. After creation, you have a text box that, when managed
> will occupy an area that is 7 lines long.


Thanks for clarifying this - and to Ron too, who also made some helpful
suggestions.


>
>
> That will work. 4.end is just before the newline, so nothing else
> changes. Continuing my example above
>
> $statusresults delete 4.0 4.end
> $statusresults insert "Some new text for line 4"
>
> Should do the trick.


Cheers

>
>
>
> If you do this, you will change the existing lines 4-7 to lines 5-8
> which is almost certainly not what you want.


You are right - it was not what I wanted!


> Note also that text widget line numbers start from one rather than
> zero. Writing to line zero appears not to be an error, it just gets
> silently converted to line one.


I was aware of that, although I know the code snippit i posted refereed
to line 0.

> Finally, it looks as though you will be using your seven lines to
> format things in a fixed way. Have you considered a set of 7 label
> widgets, rather than a single 7-line text widget? It might be less
> confusing to code.


Its possible I might want to do a bit of formatting (underlining a word,
putting a few characters in a proportional font etc), which I think
would be easier in a text box.

> Hope this helps,


Yes it did Alan, thank you.


--
Dave (from the UK)

Please note my email address changes periodically to avoid spam.
It is always of the form: month-year@althorne.org
Hitting reply will work for a few months only - later set it manually.

http://chessdb.sourceforge.net/ - a Free open-source Chess Database
Schelte Bron

2007-07-26, 10:08 pm

Dave (from the UK) wrote:
> for {set linenumber 0} {$linenumber<9} {incr linenumber} {
> $statsresults insert $linenumber.0 "\n"
> }


Easier is:
$statsresults insert 1.0 [string repeat \n 9]


Schelte
--
set Reply-To [string map {nospam schelte} $header(From)]
Sponsored Links







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

Copyright 2008 codecomments.com