473,465 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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.G etBackgroundColour())

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

Jan 18 '06 #1
6 10568

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 - wx************@lists.wxwidgets.org

Jan 18 '06 #2

Frank Millman wrote:

def onGotFocus(self,evt):
if readonly:
self.Navigate()


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

Frank

Jan 18 '06 #3
> 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,

-t**@youcanneverhavetoomanypeoplenamedtim.com

Jan 18 '06 #4

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

Jan 19 '06 #5
>> 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.

-t**@yetanothertim.com

Jan 19 '06 #6

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

Jan 20 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Logan | last post by:
I asked a similar question already in the wxPython mailing list, but did not get an answer up to now: 1.) When using a TextCtrl with styles (color etc.) in wxPython, is it then possible to get a...
0
by: DaveR | last post by:
I have a webform with a two-column datagrid based on an Arraylist. The Arraylist draws the data for the two columns from two different tables in an SQL database. The data is displayed in datagrid...
3
by: Angela Chen | last post by:
Hi, I have a readOnly textbox. I want to be able to double click it to make it editable? How to implement it?\ Thanks a lot
8
by: pmud | last post by:
Hi, I have 2 questions: 1. I have an editable data grid with 21 columns. I need to edit only 2 cloumns in this data grid. But when the grid is displayed in Edit mode, all the columns show long...
7
by: Benton | last post by:
Hi there, I have a text box which will receive its value from a pop-up date picker. The user should not be able to edit this field with the keyboard or mouse. I am using ASP.NET. If I set the...
2
by: Frank | last post by:
Hello, I am developing with VS.Net 2003 on the asp 1.1 platform. I am a few days new to using a datagrid. I found a nice tutorial and had no problems using an editable datagrid with textboxes...
11
true911m
by: true911m | last post by:
I can't seem to find the "ReadOnly" attribute for a wx.TextCtrl in Boa. It's usually just a checkbox/boolean in VS; I think wxGlade used a wx value like TE_READONLY or something similar. It's...
3
by: bcwhite | last post by:
I'm running Python2.5 with wxPython v2.8.3.0 under WinXP and I cannot get the SetDefaultStyle method to work. I'm trying: self.output.SetDefaultStyle(wx.TextAttr(wx.RED))...
1
by: aeroumr | last post by:
In the following code, I have created a panel with a button and a textctrl object on it. I have also created a menubar that will create a new text file (i.e. textctrl object). My problem is that...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.