TextCtrl Focus?

Hi all,

As part of a larger project, I have a simple-minded text editor consisting of a wx.Frame with a menu bar and a single wx.TextCtrl.
(The frame actually contains a Sizer with one slot).

This mostly works fine. However, when the Frame first opens, the
TextCtrl does not have the focus. Clicking on the edit control
gives it the focus and it stays there from then on, being restored correctly even if I switch to another window and back.

Is this a bug?

Is there a way I can make the TextCtrl take the focus as soon as the window opens?

I'm using wxWindows-2.4.2 with GTK-2.6.10 and wxPython 2.4.2 (if I'm reading the Fedora version numbers correctly) under Fedora Core 4.

Thanks in advance for any help.

                              --Chris

Chris Reuter wrote:

Is there a way I can make the TextCtrl take the focus as soon as the window opens?

There are two parts to the recipe:

1) Telling the textbox to take the focus. This can be accomplished easily:

textbox.SetFocus()

2) Calling the SetFocus() at the correct time (after it has been created and the form shown). This will depend on when/where you set up the textbox. Perhaps the easiest way is to do it in EVT_ACTIVATE of the form, but this will get called unnecessarily every time it activates. If you wanted, you could add a flag to determine if the focus still needs to happen or not:

#(form's __init__):
  self.Bind(wx.EVT_ACTIVATE, self.onActivate)

def onActivate(self, evt):
  self.textbox.SetFocus()

···

--
Paul

Try the following after you have created the text control:
    textctrl.SetFocus()

If that doesn't work, the following should...
    wx.CallAfter(textctrl.SetFocus)

- Josiah

···

Chris Reuter <cgreuter@archelon.com> wrote:

Hi all,

As part of a larger project, I have a simple-minded text editor
consisting of a wx.Frame with a menu bar and a single wx.TextCtrl.
(The frame actually contains a Sizer with one slot).

This mostly works fine. However, when the Frame first opens, the
TextCtrl does not have the focus. Clicking on the edit control
gives it the focus and it stays there from then on, being restored
correctly even if I switch to another window and back.

Is this a bug?

Is there a way I can make the TextCtrl take the focus as soon as the
window opens?

I'm using wxWindows-2.4.2 with GTK-2.6.10 and wxPython 2.4.2 (if I'm
reading the Fedora version numbers correctly) under Fedora Core 4.

Thanks in advance for any help.