2

I'm trying to fire a line of code when the user clicks on a textCtrl. The end goal is to highlight the contents of the box when it is clicked on. I am aware that this is possible with wx.EVT_SET_FOCUS, but it's either buggy or I'm implementing it wrong. Here's my code:

self.m_textCtrl1 = wx.TextCtrl(self.m_panel2, wx.ID_ANY, wx.EmptyString, 
                               wx.DefaultPosition, wx.Size(100,-1), wx.TE_LEFT)
self.m_textCtrl1.SetMaxLength(8) 
self.m_textCtrl1.SetMinSize(wx.Size(100,-1))
self.m_textCtrl1.SetMaxSize(wx.Size(100,-1))
self.m_textCtrl1.Bind(wx.EVT_SET_FOCUS, self.highlightText, self.m_textCtrl1)

This code is able to successfully fire highlightText when I want it to, but for some reason the cursor is removed from the textCtrl, leaving the user unable to pick his spot, highlight, or backspace. Any suggestions would be appreciated. As a side note, is there a way to do this in wxFormBuilder? I built my application using it but was unable to add a focus event. It seems the only focus events it offers are for the entire window.

EDIT 9/19/14: Mike, here's my automatically generated wxFormBuilder code, in gui.py:

class OrderNumEntry ( wx.Frame ):
    def __init__( self, parent ):
        # there's a lot more stuff here, but it's irrelevant
        self.m_textCtrl1.Bind( wx.EVT_SET_FOCUS, self.highlightText )

    def __del__( self ):
        pass

    # Virtual event handlers, overide them in your derived class
    def highlightText( self, event ):
        event.Skip()

... and here's the event handler that I wrote

import wx, gui

class OrderFrame(gui.OrderNumEntry):
    def __init__(self, parent):
        gui.OrderNumEntry.__init__(self, parent)
        # again, a lot more irrelevant stuff here

    def highlightText(self, event):
        print 'test'

The event works fine (as in test is printed when I want it), but I'm not able to highlight text and I can't see my cursor.

TboneH
  • 51
  • 1
  • 5
  • Your last example doesn't have `event.Skip()` in the event handlers, so that's why you cannot highlight text, etc. – Mike Driscoll Sep 19 '14 at 18:06
  • You are right good sir. I assumed because it was in the virtual handler that was good enough, but now that I think about it that get's overridden anyways. Is this recommended for all event handlers? All my other events have worked fine without it. – TboneH Sep 19 '14 at 19:44
  • You only need event.Skip() if the event needs to be propagated up the event chain. In this case, you want the event to continue so you can use the widget normally. Most of the time you won't need to call Skip() because the event handler you defined does everything you want it to. – Mike Driscoll Sep 22 '14 at 13:19

1 Answers1

1

You don't show your event handler, but my guess would be that you need to call event.Skip() at the end of it. I want to also note that you binding the event incorrectly. It should be:

self.m_textCtrl1.Bind(wx.EVT_SET_FOCUS, self.highlightText)

or

self.Bind(wx.EVT_SET_FOCUS, self.highlightText, self.m_textCtrl1)

See the wxPython wiki for a complete explanation:

Mike Driscoll
  • 32,629
  • 8
  • 45
  • 88
  • I have event.Skip(), so that can't be the problem. The virtual event handlers are automatically generated by wxFormBuilder, so all I have to do is write a subroutine with the same name. However, I tried out both of your examples and I'm still having issues. Your first example does the same thing mine does. It functions properly yet I cannot highlight text and I see no cursor. Interestingly, it also runs the code on a window focus event. The second example doesn't seem to do anything at all. – TboneH Sep 18 '14 at 14:21
  • The first example works for me. When you click into it, the text is highlighted and then the cursor appears where you clicked, so it doesn't stay highlighted very long. This only happens if I have `event.Skip()` at the end. Otherwise it selects everything, but I can't edit the text. – Mike Driscoll Sep 18 '14 at 18:24