example of a Ctrl-F find dialog in a wxTextCtrl

297 views
Skip to first unread message

C M

unread,
Aug 6, 2012, 4:10:47 PM8/6/12
to wxPytho...@googlegroups.com
This has to be such a common need--is there one out there I've missed?  If not, what's the basic logic of doing this?  So far:

1. I've got the key_char of Ctrl-F recognized
2. I can open a FindDialog
3. I can find and select text in the textCtrl if I have a word

I just am not sure how to go from 2 to 3 and so far I'm doing it wrong.  How do I pass the text the user wants to find to step 3? 

Thanks,
Che

Gadget/Steve

unread,
Aug 6, 2012, 5:16:40 PM8/6/12
to wxpytho...@googlegroups.com
> --
> To unsubscribe, send email to wxPython-user...@googlegroups.com
> or visit http://groups.google.com/group/wxPython-users?hl=en
You can either:
2a) get the text from the FindDialog when the user presses find, (or
ok), using yourDialog.GetText() or
OR:
Bind the FindDialog OnKey handler to when a new key has been pressed do
2a above, (or even to highlight all the matches so far).


C M

unread,
Aug 6, 2012, 6:53:33 PM8/6/12
to wxpytho...@googlegroups.com
On Mon, Aug 6, 2012 at 5:16 PM, Gadget/Steve <Gadge...@live.co.uk> wrote:
On 06/08/2012 9:10 PM, C M wrote:
> This has to be such a common need--is there one out there I've
> missed?  If not, what's the basic logic of doing this?  So far:
>
> 1. I've got the key_char of Ctrl-F recognized
> 2. I can open a FindDialog
> 3. I can find and select text in the textCtrl if I have a word
>
> I just am not sure how to go from 2 to 3 and so far I'm doing it
> wrong.  How do I pass the text the user wants to find to step 3?
>
> Thanks,
> Che
> --
> To unsubscribe, send email to wxPython-user...@googlegroups.com
> or visit http://groups.google.com/group/wxPython-users?hl=en
You can either:
2a) get the text from the FindDialog when the user presses find, (or
ok), using yourDialog.GetText() or

Tried that now.  But:

AttributeError: 'FindReplaceDialog' object has no attribute 'GetText'

Josh English

unread,
Aug 6, 2012, 10:38:49 PM8/6/12
to wxpytho...@googlegroups.com
On Mon, Aug 6, 2012 at 3:53 PM, C M <cmpy...@gmail.com> wrote:

2a) get the text from the FindDialog when the user presses find, (or
ok), using yourDialog.GetText() or

Tried that now.  But:

AttributeError: 'FindReplaceDialog' object has no attribute 'GetText'

You can easily write one, adding it to your dialog class, where it refers to the TextCtrl inside the dialog.

In stubcode:

class MyFindDialog(wx.Dialog):
    def __init__(self, *args, **kwargs):
        ...
        self.myText = wx.TextCtrl(self)
        ...

    def GetText(self):
        return self.myText.GetValue()

This is the safest way to do it, in my opinion. It's better than having your main class having to call dlg.myText.GetValue().




--
Josh English
Joshua.R...@gmail.com
http://www.joshuarenglish.com

C M

unread,
Aug 6, 2012, 10:58:07 PM8/6/12
to wxpytho...@googlegroups.com
On Mon, Aug 6, 2012 at 10:38 PM, Josh English <joshua.r...@gmail.com> wrote:
On Mon, Aug 6, 2012 at 3:53 PM, C M <cmpy...@gmail.com> wrote:

2a) get the text from the FindDialog when the user presses find, (or
ok), using yourDialog.GetText() or

Tried that now.  But:

AttributeError: 'FindReplaceDialog' object has no attribute 'GetText'

You can easily write one, adding it to your dialog class,

Thanks, but I don't have (nor want) a custom dialog class.  I'm using wx.FindReplaceDialog, which is already made for just this purpose, is native, etc.  There must be a dirt simple way to get the search text from this...?

Che

Robin Dunn

unread,
Aug 6, 2012, 11:06:57 PM8/6/12
to wxpytho...@googlegroups.com
Basically the opposite of how you put initial values into the dialog,
using a wx.FindReplaceData object:

findData = flg.GetData()
text = findData.GetFindString()

or using its properties:

text = dlg.Data.FindString


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

C M

unread,
Aug 7, 2012, 1:11:05 AM8/7/12
to wxpytho...@googlegroups.com
On Mon, Aug 6, 2012 at 11:06 PM, Robin Dunn <ro...@alldunn.com> wrote:
On 8/6/12 7:58 PM, C M wrote:


On Mon, Aug 6, 2012 at 10:38 PM, Josh English
<joshua.r...@gmail.com <mailto:joshua.r.english@gmail.com>> wrote:

    On Mon, Aug 6, 2012 at 3:53 PM, C M <cmpy...@gmail.com
    <mailto:cmpy...@gmail.com>> wrote:


            2a) get the text from the FindDialog when the user presses
            find, (or
            ok), using yourDialog.GetText() or


        Tried that now.  But:

        AttributeError: 'FindReplaceDialog' object has no attribute
        'GetText'


    You can easily write one, adding it to your dialog class,


Thanks, but I don't have (nor want) a custom dialog class.  I'm using
wx.FindReplaceDialog, which is already made for just this purpose, is
native, etc.  There must be a dirt simple way to get the search text
from this...?

Basically the opposite of how you put initial values into the dialog, using a wx.FindReplaceData object:

        findData = flg.GetData()
        text = findData.GetFindString()
 
I had tried that but the problem is I am getting a an empty string ('') as text because these lines are run immediately after creating the FindReplaceDialog so of course there is no find string yet put in by the user. 

I need to run this after the user presses find but I don't know where to put it.  I thought maybe I had to bind a Find event to the textCtrl?  But that isn't doing anything.  Binding it to the frame crashes it and to the panel does nothing.

Attached is my runnable sample...I just need it to pass the text-to-be-found to the OnFind() method. 

Thanks,
Che
finddialog.py

Robin Dunn

unread,
Aug 7, 2012, 1:38:23 AM8/7/12
to wxpytho...@googlegroups.com
On 8/6/12 10:11 PM, C M wrote:
>
>
> On Mon, Aug 6, 2012 at 11:06 PM, Robin Dunn <ro...@alldunn.com
> <mailto:ro...@alldunn.com>> wrote:
>
> On 8/6/12 7:58 PM, C M wrote:
>
>
>
> On Mon, Aug 6, 2012 at 10:38 PM, Josh English
> <joshua.r...@gmail.com <mailto:joshua.r...@gmail.com>
> <mailto:joshua.r.english@__gmail.com
> <mailto:joshua.r...@gmail.com>>> wrote:
>
> On Mon, Aug 6, 2012 at 3:53 PM, C M <cmpy...@gmail.com
> <mailto:cmpy...@gmail.com>
Have you looked at the sample in the demo yet? As it shows you can bind
the find events to the dialog. There is currently a bug on Windows (in
current 2.9 releases at least) where the find text sent with the event
is not being fetched properly, but you should be able to get it from the
dialog itself in the event handler.

C M

unread,
Aug 7, 2012, 2:17:58 AM8/7/12
to wxpytho...@googlegroups.com
Have you looked at the sample in the demo yet?  As it shows you can bind the find events to the dialog.  There is currently a bug on Windows (in current 2.9 releases at least) where the find text sent with the event is not being fetched properly, but you should be able to get it from the dialog itself in the event handler.

I have now looked at the Demo for both wx2.8.10 and 2.9.4.  In 2.8.10, it is the panel that is bound to the find event; in 2.9.4, it is, as you said, the find dlg. 

If I use 2.9.4 and bind the dlg, I get a crash.  (Contrary to the Demo)

If I use 2.8 and bind the panel, I get nothing.  If I bind the dlg, I get a crash.

Maybe the sample I attached previously gives a clue as to what I'm doing wrong?  It's probably something in plain sight I just don't see.



Robin Dunn

unread,
Aug 7, 2012, 2:40:38 AM8/7/12
to wxpytho...@googlegroups.com
Try holding a reference to the data object so it isn't garbage
collected. It looks like the find dialog is not taking ownership of it
and is not copying it, so you need to make sure that it sticks around at
least as long as the dialog does.

C M

unread,
Aug 7, 2012, 3:09:07 AM8/7/12
to wxpytho...@googlegroups.com
Try holding a reference to the data object so it isn't garbage collected.  It looks like the find dialog is not taking ownership of it and is not copying it, so you need to make sure that it sticks around at least as long as the dialog does.

That did it!  Wow that's the sort of thing that would have occurred to me...sometime in the next millennium or two.  Thanks very much.

Che
Reply all
Reply to author
Forward
0 new messages