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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Widget Focus

Status
Not open for further replies.

nhk100

Technical User
Jul 22, 2010
3
0
0
US
Hello,

I have 3 entry box displayed on the main form (among other widgets) and defined as:
entry .a.e -width 14 -relief sunken -textvariable xy0
entry .b.e -width 14 -relief sunken -textvariable xy1
entry .c.e -width 14 -relief sunken -textvariable xy2

In I function I have, I need to know if any of the 3 entry boxes has the input focus and which one.
I think that 'focus' returns the widget that has the input focus but not sure how to call it or read it.

Thanks for your help
 
The focus command is used to manage the Tk input focus. At any given time, one window on each display is designated as the focus window; any key press or key release events for the display are sent to that window. It is normally up to the window manager to redirect the focus among the top-level windows of a display. For example, some window managers automatically set the input focus to a top-level window whenever the mouse enters it; others redirect the input focus only when the user clicks on a window. Usually the window manager will set the focus only to top-level windows, leaving it up to the application to redirect the focus among the children of the top-level.

Tk remembers one focus window for each top-level (the most recent descendant of that top-level to receive the focus); when the window manager gives the focus to a top-level, Tk automatically redirects it to the remembered window. Within a top-level Tk uses an explicit focus model by default. Moving the mouse within a top-level does not normally change the focus; the focus changes only when a widget decides explicitly to claim the focus (e.g., because of a button click), or when the user types a key such as Tab that moves the focus.

The Tcl procedure tk_focusFollowsMouse may be invoked to create an implicit focus model: it reconfigures Tk so that the focus is set to a window whenever the mouse enters it. The Tcl procedures tk_focusNext and tk_focusPrev implement a focus order among the windows of a top-level; they are used in the default bindings for Tab and Shift-Tab, among other things.

The focus command can take any of the following forms:

focus
Returns the path name of the focus window on the display containing the application's main window, or an empty string if no window in this application has the focus on that display. Note: it is better to specify the display explicitly using -displayof (see below) so that the code will work in applications using multiple displays.
-----
focus -displayof window
Returns the name of the focus window on the display containing window. If the focus window for window's display isn't in this application, the return value is an empty string.
----------

_________________
Bob Rashkin
 
Thanks Bong
I'm new to tcl\tk. I read the documentation but the problem that I'm having is with the syntax.

Can you please provide a code example for 'focus' that returns where the cursor is or the name of the control that has the input focus.

Thank you

 
Code:
% pack [label .lbl1 -text "label 1"] -side top
() 2 % pack [label .lbl2 -text "label 2"] -side top
() 3 % pack [entry .ent1 -textvariable v1] -side left
() 4 % pack [entry .ent2 -textvariable v2] -side left
() 16 % pack [button .b1 -command test -text test] -side top
() 19 % proc test {} {set ::v1 "test text"; focus .ent1; set ::v2 [focus]}

Then push the button and see what happens.

_________________
Bob Rashkin
 
That's exactly what I needed.
Thanks Bong for your help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top