Re: Problem with setting the cursor in a text widget
- From: Bryan Oakley <oakley@xxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 31 Jan 2007 23:03:31 GMT
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
.
- Follow-Ups:
- Prev by Date: Re: TCLDOM question - tagnames?
- Next by Date: Re: TWAPI and NET SESSION
- Previous by thread: Problem with setting the cursor in a text widget
- Next by thread: Re: Problem with setting the cursor in a text widget
- Index(es):
Relevant Pages
|