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)
    Thanks.
    -----------------------------------
    self.tcFirstName = wx.TextCtrl(id=wxID_FRAME1TCFIRSTNAME,
    name=u'tcFirstName', parent=self.panel2, pos=wx.Point(16,
    32),
    size=wx.Size(168, 24), style=0, value=u'')
    self.setForm()

    def setForm(self):
    self.Readonly("ro")

    def Readonly(self, flag):
    if flag=="ro":
    self.tcFirstName.style=wx.TE_READONLY
    self.tcFirstName.Refresh()
    self.tcFirstName.SetBackgroundColour(self.panel1.GetBackgroundColour())

    else:
    self.tcFirstName.style=0
    self.tcFirstName.Refresh()
    self.tcFirstName.SetBackgroundColour((255,255,255))

    # Other event function set textctrl can editable
    def OnBAddButton(self, event):
    self.Readonly("o")
    -------------------------------------------
    end
    Hako, Jan 18, 2006
    #1
    1. Advertising

  2. Hako wrote:
    > 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?
    >


    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:
    self.Navigate()

    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.

    HTH

    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
    #2
    1. Advertising

  3. Frank Millman wrote:
    >
    > def onGotFocus(self,evt):
    > if readonly:
    > self.Navigate()
    >


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

    Frank
    Frank Millman, Jan 18, 2006
    #3
  4. Hako

    Tim Chase Guest

    > def onGotFocus(self,evt):
    > if readonly:
    > self.Navigate()
    >
    > 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.


    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
    #4
  5. Tim Chase wrote:
    > > def onGotFocus(self,evt):
    > > if readonly:
    > > self.Navigate()
    > >
    > > 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.

    >
    > 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.
    >

    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.

    > 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.
    >

    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
    control.

    Frank
    Frank Millman, Jan 19, 2006
    #5
  6. Hako

    Tim Chase Guest

    >> 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.

    >
    > 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 control.


    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
    #6
  7. Tim Chase wrote:
    > >> 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.

    > >
    > > 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 control.

    >
    > 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.
    >


    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
    Frank Millman, Jan 20, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Angela Chen
    Replies:
    3
    Views:
    9,027
    parshuram
    Dec 6, 2004
  2. newgene
    Replies:
    0
    Views:
    829
    newgene
    Jul 28, 2003
  3. Logan
    Replies:
    3
    Views:
    441
    Edward K. Ream
    Dec 2, 2003
  4. Simon Hibbs

    TextCtrl focus events in wxWidgets

    Simon Hibbs, Jul 19, 2006, in forum: Python
    Replies:
    12
    Views:
    13,839
    Frank Millman
    Jul 19, 2006
  5. Jonathan Hyatt
    Replies:
    1
    Views:
    1,869
    Jonathan Hyatt
    Jun 8, 2004
Loading...

Share This Page