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

H

Hako

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")
 
F

Frank Millman

Hako said:
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 - (e-mail address removed)
 
T

Tim Chase

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,

(e-mail address removed)
 
F

Frank Millman

Tim said:
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
 
T

Tim Chase

Additionally, you should be able to copy text from a
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.

(e-mail address removed)
 
F

Frank Millman

Tim said:
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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top