SpinCtrl, focus, select all

I have a wxSpinCtrl with some default value set. I want to have that value
selected whenever ctrl gains focus, either by using TAB or by clicking with
the mouse. How can I connect EVT_SET_FOCUS(func) with my control?

I'm used to EVT_SOME_EVENT(Id, func) to connect the specific event to the
control with id Id, but I don't know how to use, say, EVT_SET_FOCUS in
MyFrame.__init__() where I have all of my event handlers.

Looking at this, I'm probably doing something wrong:

I have

class ExtractFrame(wxFrame):
    def __init__(self, parent, id, title,
        pos = wxPyDefaultPosition, size = wxPyDefaultSize,
        style = wxMINIMIZE_BOX|wxSYSTEM_MENU|wxCAPTION ):
        wxFrame.__init__(self, parent, id, title, pos, size, style)
...
        self.panel = wxPanel(self, -1, pos, size, style)
        self.mainDialog = MainDialog(self.panel)
...
        # WDR: handler declarations for ExtractFrame
        EVT_MENU(self, ID_ABOUT, self.OnAbout)
...
        EVT_BUTTON(self, ID_INPUT_BROWSE_BUTTON, self.ShowInputDirDialog)
...
        EVT_CHECKBOX(self, ID_SAME_OUTPUT_CHECKBOX, self.SameOutputCheckBox)

I can easily fetch my spin control, but where I should EVT_SET_FOCUS? Is
that the right event handler?

Thanks!

···

--
davor.cengija@mail.inet.hr

Davor Cengija wrote:

I have a wxSpinCtrl with some default value set. I want to have that value
selected whenever ctrl gains focus, either by using TAB or by clicking
with the mouse. How can I connect EVT_SET_FOCUS(func) with my control?

Ok, I found the solution for this problem: need to send an object's
reference, along with the function. The documentation is a little bit
confusing here, but demo examples helped.

However, ctrl.SetSelection problem seems to be more serious. Is that
function implemented in wxPython? I'm using 2.4.0.6 and wxWindows
documentation says:

wxSpinCtrl::SetSelection(long, long)

but when running my program:

EVT_SET_FOCUS(self.GetUpperLimitSpinctrl(), self.SpinFocus)
...
    def SpinFocus(self, event):
        ctrl = event.GetEventObject()
        ctrl.SetSelection(-1, -1)
...

Traceback (most recent call last):
  File "/home/davor/python/extract/extractUI.py", line 123, in SpinFocus
    ctrl.SetSelection(-1, -1)
AttributeError: wxSpinCtrl instance has no attribute 'SetSelection'

Tried on both windows and linux, similar error messages, wxPython 2.4.0.6.
Maybe that method is not implemented in wxPython?

···

--
davor.cengija@mail.inet.hr

Davor Cengija wrote:

Davor Cengija wrote:

I have a wxSpinCtrl with some default value set. I want to have that value
selected whenever ctrl gains focus, either by using TAB or by clicking
with the mouse. How can I connect EVT_SET_FOCUS(func) with my control?

Ok, I found the solution for this problem: need to send an object's reference, along with the function. The documentation is a little bit confusing here, but demo examples helped.

Events derived from wxCommand event will travel up from child to parent looking for a handler. All other event types go only to the window they are generated from. So to get focus events you need to bind the handler to the window you are tracking the focus for.

However, ctrl.SetSelection problem seems to be more serious. Is that function implemented in wxPython?

No, it's another case of a missing method. I can add it but it is not implemented on wxGTK for some reason. If that is a problem, or you can't wait until the next release then I suggest you build your own spin control by hooking a wxTextCtrl and a wxSpinButton together.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Robin Dunn wrote:

Davor Cengija wrote:

Davor Cengija wrote:

I have a wxSpinCtrl with some default value set. I want to have that
value selected whenever ctrl gains focus, either by using TAB or by
clicking with the mouse. How can I connect EVT_SET_FOCUS(func) with my
control?

Ok, I found the solution for this problem: need to send an object's
reference, along with the function. The documentation is a little bit
confusing here, but demo examples helped.

Events derived from wxCommand event will travel up from child to parent
looking for a handler. All other event types go only to the window they
  are generated from. So to get focus events you need to bind the
handler to the window you are tracking the focus for.

I see. This works:

EVT_SET_FOCUS(self.GetUpperLimitSpinctrl(), self.SpinFocus)

(in wxFrame.__init__)

Thanks!

However, ctrl.SetSelection problem seems to be more serious. Is that
function implemented in wxPython?

No, it's another case of a missing method. I can add it but it is not
implemented on wxGTK for some reason. If that is a problem, or you
can't wait until the next release then I suggest you build your own spin
control by hooking a wxTextCtrl and a wxSpinButton together.

No, it's not so critical feature, so I'll wait (but as you say, it probably
won't work on Linux due to wxGTK's problem). However, it's good to know
that it could be done by hooking wxTextCtrl and wxSpinButton.

···

--
davor.cengija@mail.inet.hr