[wxPython] Capturing text from wxTextCtrl

I'd like to get text input from wxTextCtrl and assign it to a variable
(make a pointer, I'd guess) for use later in the program...
...
Traceback (most recent call last):
File "Address2.py", line 27, in OnFindButton1
   win = self.FindWindowById(self.editname)
File "I:\Python21\wxPython\windows.py", line 170, in FindWindowById
   val = apply(windowsc.wxWindow_FindWindowById,(self,) + _args,
_kwargs)
AttributeError: wxTextCtrl instance has no attribute '__int__'

I'm just extracting the important parts of the code.

  #Logging for niceness................
  self.logger = wxTextCtrl(self,5, "",wxPoint(300,20),
wxSize(200,300),wxTE_MULTILINE | wxTE_READONLY)

  self.lblname = wxStaticText(self, -1, "Name :",wxPoint(20,y))

  #I want to capture text in this window.................
  self.editname = wxTextCtrl(self, 20, "", wxPoint(100, y),
wxSize(200,-1))

Now, you have a window with an id of "20". The window is wrapped by a
wxTextCtrl object that you have stored in self.editname.

  #Capture text when button is pushed..................
  self.btn1 = wxButton(self,BTN1,"Push",wxPoint(210,y+20))

  EVT_TEXT(self, 20, self.EvtText)
  EVT_BUTTON(self,BTN1,self.OnFindButton1)

def EvtText(self, event):
  self.logger.AppendText('EvtText: %s\n' % event.GetString())

def OnFindButton1(self,event):
  win = self.FindWindowById(self.editname)
  if win is None:
    self.logger.write("*******Name not found")
    return
  if win is self.editname:
    #Dump captured text into logger.........
    self.logger.AppendText('Found %s\n' % event.GetString())
  else:
    self.logger.AppendText('Not the same')

OnFindButton1 will get called when your "self.btn1" is triggered. You don't
need to "look up" the window object for self.editname in order to fetch its
contents, because you already HAVE the window object. Further,
"event.GetString()" contains a string that relates to this specific event.
In this functions, we are handling the pushing of btn1, not the changing of
the text box. GetString() won't help you. Instead, just fetch the value of
the text control, which you can do because you saved the wrapper object in
self.editname.

You should be able to replace that entire procedure with:

    def OnFindButton1(self,event):
        self.logger.AppendText('Found %s\n' % self.editname.GetValue())

···

On Thu, 02 May 2002 18:10:08 -0400, Charles Bowman <bowman@acsu.buffalo.edu> wrote:

--
- Tim Roberts, timr@probo.com
  Providenza & Boekelheide, Inc.

Tim Roberts wrote:
[snip....]

···

You should be able to replace that entire procedure with:

    def OnFindButton1(self,event):
        self.logger.AppendText('Found %s\n' % self.editname.GetValue())

#===========================================================================
Your code worked OK, but I am still puzzled by what is returned when the
button is pushed.

I modified the above code to:
#=================================================
  def OnFindButton1(self,event):
    self.logger.AppendText('Found %s\n' %self.editname.GetValue())
    a = self.editname.GetValue()
    print 'This is the value of a: ',a
    print 'a is of type ',type(a)
    return self.editname.GetValue()

  stuff = OnFindButton1
  print 'Returned stuff from OnFindButton1 ',stuff
  print 'stuff is of type ',type(stuff)
  print ''

#=================================================
#..............which returns the following information:

pythonw -u Address2.py
Returned stuff from OnFindButton1 <function OnFindButton1 at 01545D0C>
stuff is of type <type 'function'>

This is the value of a: asdfasdfasdf #......this is what I typed in the
window........
a is of type <type 'string'>
Exit code: 0
#======================================================

Although "self.editname.GetValue()" returns a sting inside the method, I
get a function returned
(instead of the string) when I ask the OnFindButton1 to "return
self.editname.GetValue()".

How can I get the string returned?

Thanks again for your help...

Regards,

Charles Bowman

Although "self.editname.GetValue()" returns a sting inside the method, I
get a function returned
(instead of the string) when I ask the OnFindButton1 to "return
self.editname.GetValue()".

But "stuff = OnFindButton1" does not call the function, it just assigns
stuff to be another reference to the function object.

···

--
Robin Dunn
Software Craftsman
robin@AllDunn.com Java give you jitters?
http://wxPython.org Relax with wxPython!