I am new to wxPython and trying out some examples. I am using wxFormBuilder to create a simple GUI with many wxTextCtrl. I want to determine the current location of cursor, which is in one of those wxTextCtrl and do some operation. How do I do this? Please help!
Asked
Active
Viewed 409 times
1 Answers
1
Try using the wx.Frame's FindFocus()
method. That should return the widget that has focus.
See also:

Community
- 1
- 1

Mike Driscoll
- 32,629
- 8
- 45
- 88
-
I tried that yesterday. Here is what I am doing: _if self.MainWindow.FindFocus() == self.Var1: self.Var1.SetFocus()_ where, _MainWindow_ is the main GUI window and _Var1_ is one of the wxTextCtrl. But this doesn't work. Suggestions? – John Smith Oct 07 '14 at 15:11
-
Var1 has the focus and if it has it you set the focus? What does not work? Maybe create a small sample application we can run to better help. – Werner Oct 07 '14 at 15:52
-
Yes I know in normal circumstances if _Var1_ already has the focus, there is no need to check if it has the focus. But I am using a lot of _*.show()_ and _*.hide()_ functions to hide and show some options because of which _Var1_ is loosing focus. So I need to find if _Var1_ had focus previously and return the focus back to it. I will try to provide an example but in the meantime any other suggestions? – John Smith Oct 07 '14 at 15:58
-
You could use a variable like `self.last_focus` and use that to keep track of which widget last had focus. That would make it a lot easier to reset focus back after a Show/Hide – Mike Driscoll Oct 07 '14 at 16:00
-
@MikeDriscoll, To set the _self.last_focus_, don't I need to know on which widget the control was previously? We are back to my original question? How do I know where the control came back from? – John Smith Oct 07 '14 at 16:09
-
1Bind the appropriate widgets to EVT_SET_FOCUS and update the variable whenever that event is called. Then you can use that information along with whether or not the widget is visible to help you determine which widget to set focus on. – Mike Driscoll Oct 07 '14 at 16:10