Getting Pixel Position in a wx.richtext.RichTextCtrl

Hi all,

I’m wondering if there’s a way to get the pixel coordinates of a particular point from this widget (that is, wx.richtext.RichTextCtrl)?

I’ve only been able to find the position of any particular point with either the text position (i.e. an integer representing where a particular character is in the control) or an XY point where X is a character index for any particular line and Y is the line number.

Does this sound familiar to anybody. Apologies in advance if I’m missing something obvious.

Thanks!

Try this:

    r = wx.Rect()
    stc.GetCaretPOsitionForIndex(pos)
    print(r)

The rectangle will be filled with the coordinates and size that would be used if the caret was shown at the given position. You can probably then extrapolate from that rectangle the pixel point that you are looking for.

Ahhh thank you so much. I had spent so long trying to figure this out!

Here is a working example for anyone else who may have had this problem.

import wx.richtext as wxr

char_pos = 10  # a random number for e.g. purposes - imagine that we've retrieved this character position using some other method.
rtc = wxr.RichTextCtrl(parent)
r = wx.Rect()
rtc.GetCaretPositionForIndex(char_pos, r)
pixel_coords = (r[0], r[1])

Thanks again :slight_smile:

1 Like