Search code examples
pythoneventswxpythonwxwidgets

wxPython caret move event


What event is called when the caret inside a TextCtrl / Styled TextCtrl has its position changed? I need to bind the event to show in the status bar, the current position of the caret.


Solution

  • Try binding the wx.EVT_KEY_UP event with the wx.TextCtrl object like this:

    import wx
    
    class MyForm(wx.Frame):
    
        def __init__(self):
            wx.Frame.__init__(self, None, wx.ID_ANY, "Show Caret Position", size=(400, 140))
            panel = wx.Panel(self, wx.ID_ANY)
            sizer = wx.BoxSizer(wx.VERTICAL)
            text = wx.StaticText(panel, -1, "Text:", (10, 22))
            self.textCtrl = wx.TextCtrl(
                    panel,
                    -1, "",
                    (50,5),
                    size=(250, 50),
                    style=wx.TE_MULTILINE
                )
            self.textCtrl.SetInsertionPoint(0)
            self.textCtrl.Bind(wx.EVT_KEY_UP,self.onTextKeyEvent)
            self.textCtrl.Bind(wx.EVT_LEFT_UP,self.onTextKeyEvent)
            self.statusbar = self.CreateStatusBar(1)
            panel.SetSizerAndFit(sizer, wx.VERTICAL)
    
        def onTextKeyEvent(self, event):
            statusText = "Caret Position: "+str(self.textCtrl.GetInsertionPoint())
            self.SetStatusText(statusText,0)
            event.Skip()
    
    
    #Run application
    if __name__ == "__main__":
        app = wx.PySimpleApp()
        frame = MyForm()
        frame.Show()
        app.MainLoop()
    

    I've tested on Windows 7 environment with Python 2.7 + wxPython 2.8.

    Here is how it should look like