[wxPython-users] No Highlight of text selection
Morton Publications
morton1611 at preservedwords.com
Tue Jan 2 09:19:37 PST 2007
Robin,
Attached is a working example of my popup app using Python 2.4 and WxPython =
2.8.
Notice how the text highlight goes over every word with the cursor (as it =
should with the mouse motion event), but I need this highlight to be hidden =
or transparent.
Second, put the cursor on a reference in the bottom line and the popup will =
display and stay up, even if the cursor is moved down in blank space. I =
would like the popup to leave when the cursor leaves the boundary of the =
reference.
Is there anyway to capture an event when the cursor changes from a "text" =
cursor to the default "arrow" cursor when it leaves text?
Thanks for your consideration,
Tim
----- Original Message ----- =
From: "Robin Dunn" <robin at alldunn.com>
To: <wxPython-users at lists.wxwidgets.org>
Sent: Monday, January 01, 2007 3:24 PM
Subject: Re: [wxPython-users] No Highlight of text selection
> Morton Publications wrote:
>> Using "tipWindow," "fancyStaticText," and Robin's helpful "Wordwrap" =
>> module, I have made a tooltip like popup that can display html formatted =
>> text (bold, italic, color, etc). I use it to show Bible verses in the =
>> popup with a "motion" mouse event when the mouse is over a Bible =
>> reference in an htmlWindow. It works well except for two problems,
>> 1. The highlightimg from the selected word from Html "SelectWord" =
>> follows the mouse all over the window. This is annoying. I need to hide =
>> this highlighting. I have tried various Freeze and Thaw combinations to =
>> no avail. What is the solution?
>
> Without a working example it's very hard to even guess about this. What =
> kind of event is OnReadPopup bound to?
>
>
>> 2. When the cursor moves to empty space from a word, the word remains =
>> highlighted and the popup (if a reference) remains displayed. Even if th=
e =
>> cursor is on the other side of the window (as long as it doesn't cross =
>> over another word, then that word is highlighted). I've tried to find a =
>> way to make a wx.Rect from the highlight to set bounds, but havent =
>> succeeded. Any suggestions?
>
> I think I would just assume that if the cursor has moved more than a =
> certain amount from the position where it was when you showed the popup, =
> then assume that the word has changed and react accordingly.
>
>
> -- =
> Robin Dunn
> Software Craftsman
> http://wxPython.org Java give you jitters? Relax with wxPython!
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: wxPython-users-unsubscribe at lists.wxwidgets.org
> For additional commands, e-mail: wxPython-users-help at lists.wxwidgets.org
> =
-------------- next part --------------
import wx, wx.html, re
import wx.lib.fancytext as fancytext
from wx.lib.wordwrap import wordwrap
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
kwds["style"] =3D wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.panel_1 =3D wx.Panel(self, -1)
self.display =3D wx.html.HtmlWindow(self.panel_1, -1)
self.__set_properties()
self.__do_layout()
def __set_properties(self):
self.SetTitle("Popup Demo")
self.SetSize((400, 300))
def __do_layout(self):
sizer_1 =3D wx.BoxSizer(wx.HORIZONTAL)
sizer_2 =3D wx.BoxSizer(wx.HORIZONTAL)
sizer_2.Add(self.display, 1, wx.EXPAND, 0)
self.panel_1.SetAutoLayout(True)
self.panel_1.SetSizer(sizer_2)
sizer_2.Fit(self.panel_1)
sizer_2.SetSizeHints(self.panel_1)
sizer_1.Add(self.panel_1, 1, wx.EXPAND, 0)
self.SetAutoLayout(True)
self.SetSizer(sizer_1)
self.Layout()
=
self.display.SetPage(text)
self.popRef =3D ''
self.display.Bind(wx.EVT_MOTION, self.OnReadPopup)
=
def OnReadPopup(self, event):
pos =3D event.GetPosition()
focus =3D self.FindFocus()
npos =3D self.display.CalcUnscrolledPosition(pos)
spos =3D focus.GetScreenPosition()
self.display.SelectWord(npos)
ref =3D self.display.SelectionToText()
=
#Keeps popup stationary over reference
if ref =3D=3D self.popRef:
pass
else:
self.popRef =3D ref
try: self.toolwin.Destroy()
except: pass
if '_' in ref:
nRef, verse =3D self.popText(ref) =
verse =3D '^%s}, $Authorized Version, 1769 Edition}\n\n%s'=
% (nRef, verse) =
self.toolwin =3D Popup(self, verse)
self.toolwin.Position(spos, (pos[0]-30, pos[1]+25))
self.toolwin.Show()
=
#wx.TipWindow(self.leftHtml, verse, 300) =
=
event.Skip()
self.display.Refresh() =
=
=
def popText(self, ref):
ref =3D re.sub(r'_', ' ', ref)
for line in verses:
if line.startswith(ref):
line =3D re.search(r'(.*:\d+)\t(.*)', line)
nRef =3D line.group(1) =
verse =3D line.group(2)
return nRef, verse
=
=
text =3D '''<br><br>The following verses attest to the nature and extent of=
God's
preservation of His word: <br><br>
<u>Psa_119:155</u>, <u>Psa_119:149</u>,
<u>Psa_119:140</u>, <u>Psa_119:153</u>,
<u>Psa_119:160</u>; <u>Psa_119:155</u>,
<u>Psa_119:158</u>, <u>Psa_119:156</u>;'''
=
verses =3D [ 'Psa 119:140\t Thy word <i>is</i> very pure: therefore thy se=
rvant loveth it.', =
'Psa 119:149\t Hear my voice according unto thy lovingkindness:=
O LORD, quicken me according to thy judgment. ',
'Psa 119:150\t They draw nigh that follow after mischief: they =
are far from thy law. ',
'Psa 119:153\t RESH. Consider mine affliction, and deliver me: =
for I do not forget thy law. ',
'Psa 119:154\t Plead my cause, and deliver me: quicken me accor=
ding to thy word. ',
'Psa 119:155\t Salvation <i>is</i> far from the wicked: for the=
y seek not thy statutes. ',
'Psa 119:156\t Great <i>are</i> thy tender mercies, O LORD: qui=
cken me according to thy judgments. ',
'Psa 119:157\t Many <i>are</i> my persecutors and mine enemies;=
<i>yet</i> do I not decline from thy testimonies. ',
'Psa 119:158\t I beheld the transgressors, and was grieved; bec=
ause they kept not thy word. ',
'Psa 119:159\t Consider how I love thy precepts: quicken me, O =
LORD, according to thy lovingkindness. ',
'Psa 119:160\t Thy word <i>is</i> true <i>from</i> the beginnin=
g: and every one of thy righteous judgments <i>endureth</i> for ever.',] =
=
class Popup(wx.PopupWindow):
def __init__(self, parent, verse):
wx.PopupWindow.__init__(self, parent)
self.SetBackgroundColour("#CCFFFF") #FFFFCC =3D light yellow, FFCCC=
C =3D light red, CCFFFF =3D light blue
=
#Necessary so wordwrap won't consider the longer tags when wrapping
verse =3D re.sub(r'<i>', r'{', verse[:-1])
verse =3D re.sub(r'</i>', r'}', verse)
=
verse =3D wordwrap(verse, 400, wx.ClientDC(self))
=
#Put the correct tags in.
verse =3D re.sub(r'{', r'<font style=3D"italic">', verse)
verse =3D re.sub(r'}', r'</font>', verse)
verse =3D re.sub(r'\^', r'<font weight=3D"bold">', verse)
verse =3D re.sub(r'\$', r'<font color=3D"navy">', verse)
st =3D fancytext.StaticFancyText(self, -1, verse, pos=3D(10,5))
sz =3D st.GetBestSize()
self.SetSize( (sz.width+15, sz.height+15) )
class MyApp(wx.App):
def OnInit(self):
wx.InitAllImageHandlers()
frame_1 =3D MyFrame(None, -1, "")
self.SetTopWindow(frame_1)
frame_1.Show()
return 1
# end of class MyApp
if __name__ =3D=3D "__main__":
app =3D MyApp(0)
app.MainLoop()
More information about the wxpython-users
mailing list