Highlighting text on richtextctrl?

In my app , I have a problem in “Hover” event. It suppose to work as following:
if the checkbox1 is checked then do:

if the user move the mouse on any word in list1 which is appeared on richtextbox1 do:

highlight the corresponding word on richtextbox2

and same thing for checkbox2 and 3.

Any Idea to get the highlighting works.

Thanks in advance.

sample1.py (18.1 KB)

Hi, again. Thanks for the sample. I started looking at it. The first
problem is in line 321:

    for word in list1:

Here list1 as well as list2 and list3 are assigned in two different places
in your code. The first is at the top of the script:

    list1=
    list2=
    list3=

The second is in the __init__ method of your frame (this is the problem):

    list1=['ridiculous','convinced']
    list2=['primary','secondary']
    list3=['run','walks']

The problem is that in Python, namespaces mean that when you refer to
list1, list2, and list3 in your self.OnHover() method, it is using the ones
at the top of the top of the script, which are empty, and so the for loop
line in 321 just does nothing. To fix it, you have to assign the lists such
that they are common the whole class, like this:

    self.list1=['ridiculous','convinced']
    self.list2=['primary','secondary']
    self.list3=['run','walks']

and then you'd write:

    for word in self.list1:

and you should, of course, just get rid of the ones at the top of the
script; they do nothing at all. Or, alternately, keep them, but put the
words in them and get rid of the ones in the __init__.

If you make that change, the script works *a little* but still is not fully
working. However, to chase down exactly why requires more debugging--there
was an index error on one of the dicts, at least--and I haven't found it
don't want to try more because the way you have written it seems far more
difficult to understand than is even close to necessary. E.g., this
structure:

    start=dic1[dic2[word][2]][0]

And index of a list on a dict value using an index of a list on a dict
value (or something like that)...which, above it in the code, is itself
inside a conditional "if" block...which is inside a loop...which is inside
another if block...which is all in an event handler called OnHover...that
kind of hurts my head. Maybe that's how you have to do it, but I have a
feeling there is a much simpler way.

But that's a Python issue, mostly nothing to do with wxPython. Though one
wxPython hint: you can get an event handler's event object, like a
checkbox, with event.GetEventObject(). For example:

the_checkbox_checked = event.GetEventObject()

So I'd suggest you try massively refactoring your code instead of copy and
pasting everything for each checkbox, **simplify**, and make short methods
with sensible names that do things, like UnderlineWord(self, word,
target_text), and if you need help, bring it to a Python tutor list.

I'm not pushing you off, but it seems that if you keep trying to move
forward with really overly complex code, everything beyond what you're
trying to do will be just miserable (I've had those moments!).

Hope this helps.

···

On Fri, Jan 1, 2016 at 5:02 PM, <a.m.n.alsubhi@gmail.com> wrote:

In my app , I have a problem in "Hover" event. It suppose to work as
following:
if the checkbox1 is checked then do:
            if the user move the mouse on any word in list1 which is
appeared on richtextbox1 do:
                             highlight the corresponding word on
richtextbox2

and same thing for checkbox2 and 3.

Any Idea to get the highlighting works.

Thanks in advance.

Hi Che,
Thanks a lot for helping.
I found the index error and I fixed it. Also, I mad the changes that you suggested but still the highlighting just work for the check-box that is checked last. For example, If I check ckeckbox2 then ckeckbox1, the highlighter only work for words that bound with ckeckbox1.

···

On Sunday, January 3, 2016 at 12:11:43 AM UTC-5, Che M wrote:

On Fri, Jan 1, 2016 at 5:02 PM, a.m.n....@gmail.com wrote:

In my app , I have a problem in “Hover” event. It suppose to work as following:
if the checkbox1 is checked then do:

if the user move the mouse on any word in list1 which is appeared on richtextbox1 do:

highlight the corresponding word on richtextbox2

and same thing for checkbox2 and 3.

Any Idea to get the highlighting works.

Thanks in advance.

Hi, again. Thanks for the sample. I started looking at it. The first problem is in line 321:

for word in list1:

Here list1 as well as list2 and list3 are assigned in two different places in your code. The first is at the top of the script:

list1=[]
list2=[]
list3=[]

The second is in the init method of your frame (this is the problem):

list1=['ridiculous','convinced']
list2=['primary','secondary']
list3=['run','walks']

The problem is that in Python, namespaces mean that when you refer to list1, list2, and list3 in your self.OnHover() method, it is using the ones at the top of the top of the script, which are empty, and so the for loop line in 321 just does nothing. To fix it, you have to assign the lists such that they are common the whole class, like this:

self.list1=['ridiculous','convinced']
self.list2=['primary','secondary']
self.list3=['run','walks']

and then you’d write:

for word in self.list1:

and you should, of course, just get rid of the ones at the top of the script; they do nothing at all. Or, alternately, keep them, but put the words in them and get rid of the ones in the init.

If you make that change, the script works a little but still is not fully working. However, to chase down exactly why requires more debugging–there was an index error on one of the dicts, at least–and I haven’t found it don’t want to try more because the way you have written it seems far more difficult to understand than is even close to necessary. E.g., this structure:

start=dic1[dic2[word][2]][0]

And index of a list on a dict value using an index of a list on a dict value (or something like that)…which, above it in the code, is itself inside a conditional “if” block…which is inside a loop…which is inside another if block…which is all in an event handler called OnHover…that kind of hurts my head. Maybe that’s how you have to do it, but I have a feeling there is a much simpler way.

But that’s a Python issue, mostly nothing to do with wxPython. Though one wxPython hint: you can get an event handler’s event object, like a checkbox, with event.GetEventObject(). For example:

the_checkbox_checked = event.GetEventObject()

So I’d suggest you try massively refactoring your code instead of copy and pasting everything for each checkbox, simplify, and make short methods with sensible names that do things, like UnderlineWord(self, word, target_text), and if you need help, bring it to a Python tutor list.

I’m not pushing you off, but it seems that if you keep trying to move forward with really overly complex code, everything beyond what you’re trying to do will be just miserable (I’ve had those moments!).

Hope this helps.

Hi Che,
Thanks a lot for helping.
I found the index error and I fixed it. Also, I mad the changes that you
suggested

but still the highlighting just work for the check-box that is checked

last. For example, If I check ckeckbox2 then ckeckbox1, the highlighter
only work for words that bound with ckeckbox1.

Without seeing the sample of your newest code, I don't know. If you can
post a really refactored and simplified version of your code, I can take a
look.

···

On Sun, Jan 3, 2016 at 12:57 PM, <a.m.n.alsubhi@gmail.com> wrote: