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

Frank Millman frank at chagford.com
Fri Jan 20 00:41:06 EST 2006


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




More information about the Python-list mailing list