TextCtrl, getting current line, is that possible ?

328 views
Skip to first unread message

Stef Mientki

unread,
Jul 26, 2010, 6:12:24 AM7/26/10
to wxPython-users
hello,

When I click on a line in a textctrl,
I want retrieve the information on that line (contains linenr and search results),
is that possible.

I tried GetPosition, GetInsertionPoint, LastPosition but they don't give the desired result.

thanks,
Stef Mientki

Raoul Palma

unread,
Jul 26, 2010, 1:09:31 PM7/26/10
to wxpytho...@googlegroups.com
See the example in the attached file. The panel needs to be put in a wx.Window or one of its subclasses like a wx.Frame.

You also need to put some line items in the wx.TextCtrl, too, but that's easy.

Raoul


-------------------------------------------------------------------------------------------------------------------
Sponsored links: Rock Hard Erections. All New Formula Attacks the Root. Fast. www.capitolbird.org/pharma.html

TEXTCTRL_SELECTED_VALUES.PY

Vlastimil Brom

unread,
Jul 26, 2010, 4:34:25 PM7/26/10
to wxpytho...@googlegroups.com
2010/7/26 Stef Mientki <stef.m...@gmail.com>:
Hi,
I am not sure, this is exactly what you need, or whether it can be
done in a more straightforward way, but for getting the line info for
the cursor position in a TextCtrl I use something like:

col, ln = my_TextCtrl.PositionToXY(my_TextCtrl.GetInsertionPoint()) #
zero-based indices

If line-wrapping is active, this returns the currently visible columns
and lines, regardless of the physical newlines in the text.
The search results must probably be handled with specific code; I
guess TextCtrl doesn't have a search capability (unlike e.g. stc).

hth,
vbr

Stef Mientki

unread,
Jul 26, 2010, 5:41:05 PM7/26/10
to wxpytho...@googlegroups.com
thanks Vlastimil,
but at my place GetInsertionPoint always returns 0 ???

cheers,
Stef

Stef Mientki

unread,
Jul 26, 2010, 5:42:28 PM7/26/10
to wxpytho...@googlegroups.com
On 26-07-2010 19:09, Raoul Palma wrote:
See the example in the attached file. The panel needs to be put in a wx.Window or one of its subclasses like a wx.Frame.

You also need to put some line items in the wx.TextCtrl, too, but that's easy.

Raoul

thanks Raoul,

but I don't see any TextCtrl in your example code ;-)
On the other hand, you brought me to the idea of using a list, which might be a better idea.

cheers,
Stef

Stef Mientki

unread,
Jul 26, 2010, 6:23:51 PM7/26/10
to wxpytho...@googlegroups.com

> thanks Vlastimil,
> but at my place GetInsertionPoint always returns 0 ???
>
>
Sorry, I was doing something very stupid ( referencing the wrong textctrl) :-(

thanks again,
Stef

Vlastimil Brom

unread,
Jul 26, 2010, 6:36:49 PM7/26/10
to wxpytho...@googlegroups.com
2010/7/27 Stef Mientki <stef.m...@gmail.com>:

Ok, I'm glad, the solution was found :-)
I also experienced some surprises with handling text in this widget,
but these were of another art; mainly differences in indexing and
handling surrogate pairs depending on the TE_RICH... flags (GetString
and GetRange functions), or the potential speed problems of the
richtext variants, but this seems to be OS issue.

Regards,
vbr

Raoul

unread,
Jul 26, 2010, 7:17:53 PM7/26/10
to wxPython-users
Yup, you're right. You can only retrieve a cursor coordinate from a
TextCtrl. (Why would anyone want to do that ?! ) I am guessing that
you want to retrieve the actual text from a list item and its position
in the list. To do this requires a wx.ListBox which is far more
flexible. A TextCtrl is just for display purposes and is not meant to
be user-interactive.

Cody Precord

unread,
Jul 26, 2010, 7:36:07 PM7/26/10
to wxpytho...@googlegroups.com
Hi,

On Mon, Jul 26, 2010 at 6:17 PM, Raoul <raoul...@yahoo.com> wrote:
> Yup, you're right.  You can only retrieve a cursor coordinate from a
> TextCtrl. (Why would anyone want to do that ?! ) I am guessing that
> you want to retrieve the actual text from a list item and its position
> in the list. To do this requires a wx.ListBox which is far more
> flexible. A TextCtrl is just for display purposes and is not meant to
> be user-interactive.
>

What?!?!

A TextCtrl is quite possible the most common and widely used user
interactive control in all applications ever made (or at least a very
close second behind a Button). Sorry but saying it is not meant to be
user interactive is simply false.


Cody

WinCrazy

unread,
Jul 26, 2010, 8:09:15 PM7/26/10
to wxPython-users
It probably is more popular (i.e., used more), but it just doesn't
have the methods to get the text values and the item indices. Its
general purpose is to allow the user to enter and modify lines of text
in a list. There doesn't seem to be a method to retrieve the index of
a text item within the list, yet many of its methods are dependent on
knowing that item's index.

A ListBox, although its items are immutable, is another form of a text
list in which any random combination of lines can be selected and
their indices and values be easily be retrieved. This seems to be what
is wanted.



On Jul 26, 7:36 pm, Cody Precord <codyprec...@gmail.com> wrote:
> Hi,
>

Stef Mientki

unread,
Jul 27, 2010, 4:02:06 AM7/27/10
to wxpytho...@googlegroups.com
So many good people, so many good opions ;-)

The reason I used a TextCtrl is exactly the reason Cody mentioned: KISS
And Kiss in more than 1 way:
- keep it simple for the programmer (not to learn difficult objects like a listctrl)
- keep it simple for the user, in having just a few widgets he can learn in a short time
To keep it even more simple (= all my programs will have an identical look and feel and even more
important: identical behavior,
and I've to rememember just a small subset of the overwhelming wxPython methods ),
therefor I wrap wxPython components almost always in "My_Own_Class"

cheers,
Stef

Reply all
Reply to author
Forward
0 new messages