example of a Ctrl-F find dialog in a wxTextCtrl

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

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).

···

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-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

Tried that now. But:

AttributeError: ‘FindReplaceDialog’ object has no attribute ‘GetText’

···

On Mon, Aug 6, 2012 at 5:16 PM, Gadget/Steve GadgetSteve@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
  1. I can open a FindDialog
  1. 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-users+unsubscribe@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

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().

···

On Mon, Aug 6, 2012 at 3:53 PM, C M cmpython@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’

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

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

···

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

On Mon, Aug 6, 2012 at 3:53 PM, C M cmpython@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,

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

···

On 8/6/12 7:58 PM, C M wrote:

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

    On Mon, Aug 6, 2012 at 3:53 PM, C M <cmpython@gmail.com > <mailto:cmpython@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...?

--
Robin Dunn
Software Craftsman

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 (2.12 KB)

···

On Mon, Aug 6, 2012 at 11:06 PM, Robin Dunn robin@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.english@gmail.com mailto:joshua.r.english@gmail.com> wrote:

On Mon, Aug 6, 2012 at 3:53 PM, C M <cmpython@gmail.com > > <mailto:cmpython@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()

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.

···

On 8/6/12 10:11 PM, C M wrote:

On Mon, Aug 6, 2012 at 11:06 PM, Robin Dunn <robin@alldunn.com > <mailto:robin@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.english@gmail.com <mailto:joshua.r.english@gmail.com> > <mailto:joshua.r.english@__gmail.com > <mailto:joshua.r.english@gmail.com>>> wrote:

             On Mon, Aug 6, 2012 at 3:53 PM, C M <cmpython@gmail.com > <mailto:cmpython@gmail.com> > <mailto:cmpython@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.

--
Robin Dunn
Software Craftsman

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.

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.

···

On 8/6/12 11:17 PM, C M wrote:

    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
Software Craftsman

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