Control of text caret (cursor) with wx.TextCtrl

Here is some code that I have been working with for using TextCtrl with radio buttons, which was adapted from the book "wxPython in Action".

import wx
'''
  Adapted fromListing 7.11 p. 213 of "wxPython in Action"
  # Mods by V. P. Stokes
'''

class RadioButtonFrame(wx.Frame):
     def __init__(self):
         wx.Frame.__init__(self, None, -1, 'Radio Example',
                           size=(190, 200))
         panel = wx.Panel(self, -1)
         radio1 = wx.RadioButton(panel, -1, "Manual", pos=(20, 50), style=wx.RB_GROUP)
         radio2 = wx.RadioButton(panel, -1, "Random", pos=(20, 80))
         radio3 = wx.RadioButton(panel, -1, "Puzzle number:", pos=(20, 110))
         self.textField = wx.TextCtrl(panel, -1, "1", pos=(115, 110),size=(40,18),
                                      style=wx.TE_PROCESS_ENTER)
         self.textField.Enable(False)

         self.Bind(wx.EVT_RADIOBUTTON,self.onManual,radio1)
         self.Bind(wx.EVT_RADIOBUTTON,self.onRandom,radio2)
         self.Bind(wx.EVT_RADIOBUTTON,self.onPuzzleNumber,radio3)
         self.Bind(wx.EVT_TEXT_ENTER,self.onTextInput,self.textField)

         # Fire (trigger) event to select Random button
         self.onRandom(wx.EVT_RADIOBUTTON)

     def onRandom(self,event):
         self.textField.Enable(False)
         pass
         return

     def onManual(self,event):
         self.textField.Enable(False)
         pass
         return

     def onPuzzleNumber(self,event):
         self.textField.Enable(True)
         pass
         return

     def onTextInput(self,event):
         xx = self.textField.GetValue()
         print xx
         pass
         return

if __name__ == '__main__':
     app = wx.PySimpleApp()
     RadioButtonFrame().Show()
     app.MainLoop()

I would like to know how I can accomplish the following:

  When the radio button labeled Puzzle number: is selected, the following should occur:

   1. The text blinking caret (cursor) should appear at the end of the text (in this case after the "1").
   2. Hitting of the Enter key should then capture the "1".

Note in this case, the mouse pointer need not be positioned and clicked inside the text field to capture the "1".

Of course, if one wishes to change the puzzle number (e.g. to "10"), then clicking the mouse in the text field is required.

Ok,
I found something that solves this problem --- sorry to waste list space-time! I only needed to modify the onPuzzleNumber method, as follows:

def onPuzzleNumber(self,event):
         self.textField.Enable(True)
         self.textField.SetFocus()
         self.textField.SetInsertionPointEnd()
         pass
         return

···

On 2012-03-17 19:55, Virgil Stokes wrote:

Here is some code that I have been working with for using TextCtrl with radio buttons, which was adapted from the book "wxPython in Action".

import wx
'''
Adapted fromListing 7.11 p. 213 of "wxPython in Action"
# Mods by V. P. Stokes
'''

class RadioButtonFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Radio Example',
                          size=(190, 200))
        panel = wx.Panel(self, -1)
        radio1 = wx.RadioButton(panel, -1, "Manual", pos=(20, 50), style=wx.RB_GROUP)
        radio2 = wx.RadioButton(panel, -1, "Random", pos=(20, 80))
        radio3 = wx.RadioButton(panel, -1, "Puzzle number:", pos=(20, 110))
        self.textField = wx.TextCtrl(panel, -1, "1", pos=(115, 110),size=(40,18),
                                     style=wx.TE_PROCESS_ENTER)
        self.textField.Enable(False)

        self.Bind(wx.EVT_RADIOBUTTON,self.onManual,radio1)
        self.Bind(wx.EVT_RADIOBUTTON,self.onRandom,radio2)
        self.Bind(wx.EVT_RADIOBUTTON,self.onPuzzleNumber,radio3)
        self.Bind(wx.EVT_TEXT_ENTER,self.onTextInput,self.textField)

        # Fire (trigger) event to select Random button
        self.onRandom(wx.EVT_RADIOBUTTON)

    def onRandom(self,event):
        self.textField.Enable(False)
        pass
        return

    def onManual(self,event):
        self.textField.Enable(False)
        pass
        return

    def onPuzzleNumber(self,event):
        self.textField.Enable(True)
        pass
        return

    def onTextInput(self,event):
        xx = self.textField.GetValue()
        print xx
        pass
        return

if __name__ == '__main__':
    app = wx.PySimpleApp()
    RadioButtonFrame().Show()
    app.MainLoop()

I would like to know how I can accomplish the following:

When the radio button labeled Puzzle number: is selected, the following should occur:

  1. The text blinking caret (cursor) should appear at the end of the text (in this case after the "1").
  2. Hitting of the Enter key should then capture the "1".

Note in this case, the mouse pointer need not be positioned and clicked inside the text field to capture the "1".

Of course, if one wishes to change the puzzle number (e.g. to "10"), then clicking the mouse in the text field is required.

Hi Virgil,

...

I found something that solves this problem --- sorry to waste list space-time! I only needed to modify the onPuzzleNumber method, as follows:

def onPuzzleNumber(self,event):
        self.textField.Enable(True)
        self.textField.SetFocus()
        self.textField.SetInsertionPointEnd()
        pass
        return

BTW, the lines "pass" and "return" are not needed. "pass" is often inserted by code generators to be able to generate a "place holder" event handler but one normally removes it when one codes the actual handler.

Werner

···

On 17/03/2012 20:35, Virgil Stokes wrote: