Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Making an entry highlighted (activated) when a window appear? 2

Status
Not open for further replies.

erixire

Technical User
Jun 4, 2002
72
CA
Hi,

I'm making a child window appear with a default value in the entry. I would like to know if it's possible that the entry (the default value) is highligted when the window appear so that the user doesn't have to click inside the window to change the default entry?

Thanks
 
If
Code:
.entry[/entry] is the path of the entry widget you only need to add:
[code]focus .entry
.entry select range 0 end

Good luck!

ulis
 
I tried it, but it didn't work. Even the focus doesn't work. I use Tcl/tk under Microsoft Windows. I tried to focus (and also try to grab), but the focus stay to the icon that I double-clic to execute my program. Does somebody had the same problem?
 
There are actually two concepts of "focus." In a multi-application environment, only one application has focus at a time (when you type characters, those characters are sent to only one of the applications currently running). And in a given application, only one widget has focus at a time (when you type characters, they appear in only one widget in the currently active application).

By default, the force command assigns widget focus within an application. The application that has application focus is usually determined by the window manager.

What you'll need to do in this case is to use the focus -force option, for example:

Code:
focus -force .e

The -force option not only gives focus to the widget you specify, but also gives focus to the application. Use this option sparingly, though. It's usually considered "rude" for an application to go around grabbing focus for itself; most of the time you're expected to let the window manager play "traffic cop" in deciding which applications gets focus when.

By the way, if you'd also like for all of the characters in the entry to be selected (for example, so that the user could just start typing to overwrite the old value with a new one) use the selection range operation on your entry widget. You might also want to position the insertion cursor at the end of the text; use the icursor operation to position the insertion cursor to a given character index. So, you might want to combine these operations as follows:

Code:
.e selection range 0 end
.e icursor end
- Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
If your problem is to assign focus to your main Tk window under Windows, you need to use the tricky:
Code:
wm deiconify .
and then
Code:
focus .entry
.entry select range 0 end
(this should work with all Windows and I tried it under NT 4.0 & 2k)

ulis
 
Thanks a lot ulis and AviaTraining! I tried both trick and it works! The "focus -force .e" or the "wm deiconify ."!
 
Avia,

In comp.lang.tcl I found a thread about this subject:
----
In article <3D13424A.10905@web.de>, Martin Bachem <m.bachem@web.de> wrote:
>Hi there,
>I have a probelm with TCL applications and MS-Windows. When I double
>Click the Aicon in the Windows-Explorer, wish is loadad, and the main
>Window named . appers, but it does not have the focus.
>I tried the following, is this the proper way ?
>--
>#!/bin/sh ># exec wish &quot;$0&quot; &quot;$@&quot;
>wm withdraw .
>wm deiconify .
----
And Cameron Laird replied:
----
Short answer:
focus -force .

There are consequences to this I don't now have time to explain.
----

And effectively, 'wm deiconify' without a following 'focus' to a widget doesn't work.

Do you know about that?

ulis
 
Well, I'm not sure what other &quot;consequences&quot; Cameron had in mind with focus -force beyond what I already mentioned.

As for the wm deiconify trick, I'll admit that I wasn't aware that it would work to give the window focus. (See, even I learn things on this board! :) ) But I can see why doing wm deiconify without a subsequent focus doesn't work. You've given the application focus, but haven't specified a specific widget within that window to have focus. In a situation like this, the toplevel window itself (for example, &quot;.&quot;) has focus. But unless you have bindings on the toplevel window, you won't notice it. - Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
True! Now you said that, I see that binding is the key.

Thanks, Avia
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top