How to set a wx.textctrl can editable or readonly?

Discussion in 'Python' started by Hako, Jan 18, 2006.

  1. Hako

    Hako Guest

    Hello All,

    I have a function to set readonly or editable of a textctrl. I'd like
    to make the textctrl initial set readonly and use other event funciton
    to set editable of the textctrl but it always can editable. How to set
    a textctrl can editable or readonly?

    Any Ideas? (see short snippet below)
    self.tcFirstName = wx.TextCtrl(id=wxID_FRAME1TCFIRSTNAME,
    name=u'tcFirstName', parent=self.panel2, pos=wx.Point(16,
    size=wx.Size(168, 24), style=0, value=u'')

    def setForm(self):

    def Readonly(self, flag):
    if flag=="ro":


    # Other event function set textctrl can editable
    def OnBAddButton(self, event):
    Hako, Jan 18, 2006
    1. Advertisements

  2. Here is one way.

    Create a subclass of wx.TextCtrl, with an attribute called 'readonly'.
    Set the attribute to True or False to represent the desired state.

    In the subclass, create a wx.EVT_SET_FOCUS handler to be called
    whenever the text control receives focus. Then do something like this -

    def onGotFocus(self,evt):
    if readonly:

    This causes the control to react as if the user press 'tab'. By default
    it always tabs forwards, but it takes an optional 'IsForward' argument
    - set it to False to tab backwards.


    Frank Millman

    ps You will get a better response to questions like this if you post
    them to the wxPython mailing list -
    Frank Millman, Jan 18, 2006
    1. Advertisements

  3. Oops - I meant 'if self.readonly' ...

    Frank Millman, Jan 18, 2006
  4. Hako

    Tim Chase Guest

    def onGotFocus(self,evt):
    Just a pedantic query, not having wx under my fingertips at the
    moment...what happens if you three controls, A (r/w), B
    (read-only), and C (r/w) in that focus order...if use shift+tab
    in control C, does it properly go back to A, or does it move you
    forward again to control C.

    Additionally, you should be able to copy text from a read-only
    control, so ousting the focus may not be quite the right thing to do.

    Just a few random thoughts,

    Tim Chase, Jan 18, 2006
  5. You have to control it yourself, but wx gives you the tools to do so.
    You can register an event handler at panel level called
    wx.EVT_NAVIGATION_KEY It is triggered each time tab or shift-tab is
    pressed, and has a method called GetDirection(), which returns True for
    forwards (i.e. tab) and False for backwards (i.e. shift-tab). You can
    use this to maintain a 'direction' attribute, which you can pass as an
    argument to self.Navigate(). It will then always navigate in the
    correct direction.
    Good point. Alternative approaches would be to trap EVT_KEY_DOWN or
    EVT_TEXT to detect and block attempts to modify the contents of the

    Frank Millman, Jan 19, 2006
  6. Hako

    Tim Chase Guest

    Additionally, you should be able to copy text from a
    Other complications come if your controls try to second-guess you
    when it comes to copy&paste. With the keyboard, you want to be
    able to copy, but not paste. Likewise, in Win32, textboxes and
    comboboxes (not combo-list boxes) allow you to right click on
    them to get a context menu, an option of which is "paste", which
    can put all sorts of bogus data in the field if not intercepted
    properly. Within X environments, middle-click-to-paste may also
    be a problem. I don't know if either case applies to wx widgets,
    but it's something I've banged my head against in other languages
    on Win32 (VB, in particular) when trying to make a filtered
    (though not R/O) control.

    Tim Chase, Jan 19, 2006
  7. You have forced me to do my homework properly :)

    I have done a bit more digging, and have found that wx has a simple
    solution that seems to answer all your concerns, and is also the
    correct answer to the OP's question.

    A wx.TextCtrl has a 'readonly' property. You can set this when creating
    the control, by using the style wx.TE_READONLY. You can toggle it on
    and off after creation by using self.SetEditable(True/False). When set
    to False, it behaves according to your requirements. It can receive
    focus, you can copy its contents, but any attempt to alter its
    contents, but any means, is ignored.

    I have learned something new - thanks.

    Frank Millman, Jan 20, 2006
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.