Possible to prevent focus on a wx.TextCtrl?

1,514 views
Skip to first unread message

wrybread

unread,
Mar 25, 2010, 8:14:45 AM3/25/10
to wxPython-users
I'm trying to use a wx.TextCtrl the way a wx.StaticText would normally
be used, meaning read only, and non selectable. In other words, if the
user clicks on the control, no cursor should appear.

Currently I'm using the wx.TE_READONLY style, which prevents the user
from selecting the text, but if they click on the text their cursor
appears inside the text.

Is it possible prevent this behavior?

Here's how I'm constructing it currently:

wx.TextCtrl(self, -1, "testing", size = (150, 17), style=wx.TE_RICH2
| wx.NO_BORDER | wx.TE_CENTRE | wx.TE_READONLY)

If you're using Windows, you can see the behavior by trying the
program I'm working on:

http://gizmoware.net/triggerfinger

Click on one of the captions under one of the buttons.

Mike Driscoll

unread,
Mar 25, 2010, 10:22:23 AM3/25/10
to wxPython-users

As I understand it, TE_READONLY just makes the text control read-only,
which means the user can still select the text so that they can copy
and paste it. They just can't edit the text in the control.

There are two ways to prevent the user from getting a cursor in there
(I think):

1) Bind the control to the EVT_SET_FOCUS and cancel it
2) Disable the widget (probably the best solution)

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

Robin Dunn

unread,
Mar 25, 2010, 5:46:15 PM3/25/10
to wxpytho...@googlegroups.com
On 3/25/10 7:22 AM, Mike Driscoll wrote:
> As I understand it, TE_READONLY just makes the text control read-only,
> which means the user can still select the text so that they can copy
> and paste it. They just can't edit the text in the control.
>
> There are two ways to prevent the user from getting a cursor in there
> (I think):
>
> 1) Bind the control to the EVT_SET_FOCUS and cancel it

Focus events are not veto-able, but what you can do is call Navigate so
the focus will be moved to the next widget in the tab-order, or
explicitly call SetFocus on some other widget if you want it to go to
some specific place.

> 2) Disable the widget (probably the best solution)

Yes, but that changes the appearance of the widget, which may not be
desired.

--
Robin Dunn
Software Craftsman
http://wxPython.org

Mike Driscoll

unread,
Mar 26, 2010, 9:33:05 AM3/26/10
to wxPython-users

On Mar 25, 4:46 pm, Robin Dunn <ro...@alldunn.com> wrote:
> On 3/25/10 7:22 AM, Mike Driscoll wrote:
>
> > As I understand it, TE_READONLY just makes the text control read-only,
> > which means the user can still select the text so that they can copy
> > and paste it. They just can't edit the text in the control.
>
> > There are two ways to prevent the user from getting a cursor in there
> > (I think):
>
> > 1) Bind the control to the EVT_SET_FOCUS and cancel it
>
> Focus events are not veto-able, but what you can do is call Navigate so
> the focus will be moved to the next widget in the tab-order, or
> explicitly call SetFocus on some other widget if you want it to go to
> some specific place.
>


I didn't know that...oops! I meant basically the same thing though
(i.e. SetFocus on a different widget)


> > 2) Disable the widget (probably the best solution)
>
> Yes, but that changes the appearance of the widget, which may not be
> desired.
>
> --
> Robin Dunn
> Software Craftsmanhttp://wxPython.org

Yeah, but I couldn't think of a different way other than Disabling the
widget or just using a StaticText.

Fabio Spadaro

unread,
Dec 22, 2010, 4:21:52 AM12/22/10
to wxpytho...@googlegroups.com
Hi all,

2010/3/26 Mike Driscoll <kyos...@gmail.com>

--
To unsubscribe, send email to wxPython-user...@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

To unsubscribe from this group, send email to wxpython-users+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.

I would also like to have textctrl without focus, and then I thought of using StaticText but I wish he had defined edges.
What to do?



--
Fabio Spadaro
www.fabiospadaro.com

Mike Driscoll

unread,
Dec 22, 2010, 10:46:33 AM12/22/10
to wxPython-users


On Dec 22, 3:21 am, Fabio Spadaro <fabiolinos...@gmail.com> wrote:
> Hi all,
>
> 2010/3/26 Mike Driscoll <kyoso...@gmail.com>
> I would also like to have textctrl without focus, and then I thought of
> using StaticText but I wish he had defined edges.
> What to do?
>
> --
> Fabio Spadaro


You can make the TextCtrl read-only...
Reply all
Reply to author
Forward
0 new messages