Re: Problem with setting the cursor in a text widget



mike.peattie@xxxxxxxxx wrote:
I have a text widget that I read a file's contents into, and I want to
set the input cursor to always start at the beginning of line 2 (2.0)
and not the default (1.0). The problem is, when I change the file I
have open and set the cursor again, an extra line is inserted at
position 2.0. Here's the code:


The default binding for Control-o is to add a blank line. When you bind to control-o, your binding fires and then the class binding (which implements the default behavior) fires.

You might notice that if you comment out the "mark set insert 2.0", the extra line gets added near the end. You just aren't seeing it, but it's there.

There are a couple solutions. The best is to perhaps simply add "break" to your control-o binding to prevent the default binding from taking effect.

Here are some other hints to improve your code:

You don't need all those updates. Unless you know why I said that you definitely shouldn't be using update. If you feel you must update something, use "update idletasks". In your specific case they are harmless, but if you get in the habit of using them, sooner or later they will bite you. It's best not to get in the habit of using update.

(a classic essay on why update is dangerous is at http://wiki.tcl.tk/1255)

Second: you can use [.f.t index insert] to get the current cursor location; this is a much cleaner solution than to iterate over the results of the dump command.

Third: I highly recommend never putting a bunch of code in a binding. Stick to this rule of thumb: never more than one command, unless the second command is "break". It just makes long term maintenance of your software easier.

Do it like this:

bind <Control-o> "doOpen; break"
proc doOpen {} {<all the code from your binding here>}

For more on putting bindings in procedures, see:

http://www.tclscripting.com/articles/apr06/article3.html

Fourth: get in the habit of placing expressions in curly braces. Do this:

set topLoc [expr {(double($cursorYLoc) - 9)/double($endLoc)}]

instead of this:

set topLoc [expr (double($cursorYLoc) - 9)/double($endLoc)]

It's a long story why that is better; just trust us that it is or google around to learn why.

Finally, you might want to read up on named fonts. They are quite easy to use and give you a lot of flexibility later on. Try this:

font create regularFont -family arial \
-size 24 -slant roman
font create highlightFont -family arial \
-size 24 -slant roman -weight bold

...
text .f.t -font regularFont

The advantage is that you can then add code to change the font at runtime and all the widgets that use that font will magically reconfigure themselves. For example, you can create a "smallFonts" command that does "font configure regularFont -size 12" and "font configure highlightFont -size 12" and everything that uses that font will switch to the new size.

You can read more about named fonts here:

http://www.tclscripting.com/articles/jun06/article1.html

(I know this sounds like a shameful plug for my web site, but I wrote these articles specifically targeted toward someone like you who isn't familiar with some of the neat features of Tk)


--
Bryan Oakley
www.tclscripting.com
.



Relevant Pages

  • Re: Problem with setting the cursor in a text widget
    ... The default binding for Control-o is to add a blank line. ... It's best not to get in the habit of using update. ... Stick to this rule of thumb: never more than one command, ... font create highlightFont -family arial \ ...
    (comp.lang.tcl)
  • Re: Style bug for Tables?
    ... "overlay" the paragraph styles, but don't take predence. ... > style present at the cursor where I insert the table. ... If heading 5 style is present at the ... Word just ignores the font, font size, ...
    (microsoft.public.word.docmanagement)
  • Re: SVGATextMode on 2.6.11
    ... Maybe another control sequence could make the current cursor ... I found the cause of this new behavior: font loading. ... loading, and if you want to change the font, do it before you run SVGATextMode. ... >to reproduce. ...
    (Linux-Kernel)
  • Re: Can I change the default font for a comment box in excel globally
    ... Create a comment in Excel, when it is on screen, move the cursor over the ... edge of the comment where the hatching is and when the cursor changes to a ... On the Appearance tab, click the Advanced Button. ... Change the font or colour as required. ...
    (microsoft.public.excel.misc)
  • Re: How to get cursor to jump to first line when at bottom of subject browser?
    ... John E. Davis had the audacity to say that: ... Try this in your .slrnrc file: ... jump *automatically* to the top of the list when the cursor gets at ... "His one secret thought, became like a chain, binding down his spirit, and, ...
    (news.software.readers)