[wxPython] wxTextCtrl

Further to my earlier post, a script to illustrate the problem
is given below. This returns the error message:

TRAP FRAME

Traceback (most recent call last):
  File "G:\My Files\Python\Testing\TextGrief.py", line 82, in OnInit
    runTest(self, frame, self.log)
  File "G:\My Files\Python\Testing\TextGrief.py", line 24, in runTest
    f= Field(frame, -1, 'UGH', size= (60, 22))
  File "G:\My Files\Python\Testing\TextGrief.py", line 8, in __init__
    wxTextCtrl(self, parent, Id= Id, pos= pos, value= value, size=size)
  File "g:\python20\wxPython\controls.py", line 561, in __init__
    self.this = apply(controlsc.new_wxTextCtrl,_args,_kwargs)
AttributeError: 'MainFrame' instance has no attribute '__int__'

Colin W.

''' textGrief.py To subclass a TextCtrl '''
from wxPython.wx import *
class Field(wxTextCtrl):
  def __init__(self, parent, Id= -1, value= ' ',
               pos= (-1, -1),
               size= wxDefaultSize):
    wxTextCtrl(self, parent, Id= Id, pos= pos, value= value, size=size)
## EVT_KILL_FOCUS(self, self.OnKillFocus)
## EVT_SET_FOCUS(self, self.OnFocus)
    EVT_TEXT_ENTER(self.GetId(), func)

  def OnCommand(self, event):
    self.log.WriteText("TextPanel OnCommand: " + `self.wList` + "\n")
  def OnEnter(self, event):
    self.log.WriteText("TextPanel OnEnter: " + `self.wList` + "\n")
  def OnFocus(self, event):
    self.log.WriteText("TextPanel OnFocus: " + `self.wList` + "\n")
  def OnKillFocus(self, event):
    self.log.WriteText("TextPanel KillFocus: " + `self.wList` + "\n")

def runTest(app, frame, log):
## b = wxBoxSizer(wxHORIZONTAL)
  f= Field(frame, -1, 'UGH', size= (60, 22))
  return

ID_ABOUT = 101
ID_EXIT = 102
class MainFrame(wxFrame):
  def __init__(self):
    try:
      wxFrame.__init__(self, NULL, -1, pos= (20, 20), size= (200, 200),
        title= "Testing...", name= 'Main Frame')
      self.SetBackgroundColour(wxWHITE)
      self.CreateStatusBar()
      mainmenu = wxMenuBar()
      menu = wxMenu()
      menu.Append(ID_ABOUT, "&About",
                         "TestCtrl problem")
      menu.Append(ID_EXIT, 'E&xit', 'We are finished!')
      mainmenu.Append(menu, "&File")
      self.SetMenuBar(mainmenu)
      EVT_MENU(self, 200, self.OnExit)
      EVT_CLOSE(self, self.OnCloseWindow)
      rect= self.GetSize()
      rect.x+=1
      self.SetSize(rect)
      print 'Main: End of __init__'

    except:
      import traceback
      print 'TRAP FRAME'
      traceback.print_exc() # and you still get your trace
      self.Destroy() # causes main loop to exit
      return 0

  def OnSetFocus(self):
    print 'OnFocus:'
  def OnSize(self):
    print 'MainFrame: OnSize'
  def OnCloseWindow(self, event):
    print 'MainFrame: OnClose Veto=', event.GetVeto(), event.CanVeto()
    self.Destroy()

  def OnExit(self, event):
    print 'Main: OnExit'
    self.Close()

class Logger:
  def __init__(self):
    z= 1 # Do nothing
  def WriteText(self, txt= 'anything'):
    print txt

class TestApp(wxApp):
  def OnInit(self):
    try:

      frame = MainFrame()
      self.log = Logger()
      self.log.WriteText('TestApp')
      runTest(self, frame, self.log)

      frame.Show(true)
      self.SetTopWindow(frame)
      print 'End of TestApp.__init__'
      return true
    except:
      import traceback
      print 'TRAP FRAME'
      traceback.print_exc() # and you still get your trace
      self.ExitMainLoop() # causes main loop to exit
      return false

if __name__ == '__main__':
  app = TestApp(0)
  app.MainLoop()

···

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users

Several suggestions...

from wxPython.wx import *
class Field(wxTextCtrl):
  def __init__(self, parent, Id= -1, value= ' ',

id,not Id. All occurrences of this keyword argument appear to be in lower
case except wxGenButton, which is ID (Robin: I guess fixing this would break
too much existing code? How about including another, lower-case keyword
argument 'id' and raising some exception when both are present? Presumably
that MIGHT break some existing code too, but not so much...)

               pos= (-1, -1),
               size= wxDefaultSize):
    wxTextCtrl(self, parent, Id= Id, pos= pos, value= value, size=size)
## EVT_KILL_FOCUS(self, self.OnKillFocus)
## EVT_SET_FOCUS(self, self.OnFocus)
    EVT_TEXT_ENTER(self.GetId(), func)

  def OnCommand(self, event):
    self.log.WriteText("TextPanel OnCommand: " + `self.wList` + "\n")

Where is the wList attribute set? Do you perhaps want a sell.wList = value
in the __init__ above?

  def OnEnter(self, event):
    self.log.WriteText("TextPanel OnEnter: " + `self.wList` + "\n")
  def OnFocus(self, event):
    self.log.WriteText("TextPanel OnFocus: " + `self.wList` + "\n")
  def OnKillFocus(self, event):
    self.log.WriteText("TextPanel KillFocus: " + `self.wList` + "\n")

def runTest(app, frame, log):
## b = wxBoxSizer(wxHORIZONTAL)
  f= Field(frame, -1, 'UGH', size= (60, 22))
  return

ID_ABOUT = 101
ID_EXIT = 102
class MainFrame(wxFrame):
  def __init__(self):
    try:
      wxFrame.__init__(self, NULL, -1, pos= (20, 20), size= (200, 200),
        title= "Testing...", name= 'Main Frame')
      self.SetBackgroundColour(wxWHITE)
      self.CreateStatusBar()
      mainmenu = wxMenuBar()
      menu = wxMenu()
      menu.Append(ID_ABOUT, "&About",
                         "TestCtrl problem")
      menu.Append(ID_EXIT, 'E&xit', 'We are finished!')
      mainmenu.Append(menu, "&File")
      self.SetMenuBar(mainmenu)
      EVT_MENU(self, 200, self.OnExit)
      EVT_CLOSE(self, self.OnCloseWindow)
      rect= self.GetSize()
      rect.x+=1m
      self.SetSize(rect)
      print 'Main: End of __init__'

    except:
      import traceback
      print 'TRAP FRAME'
      traceback.print_exc() # and you still get your trace
      self.Destroy() # causes main loop to exit
      return 0

  def OnSetFocus(self):
    print 'OnFocus:'
  def OnSize(self):
    print 'MainFrame: OnSize'
  def OnCloseWindow(self, event):
    print 'MainFrame: OnClose Veto=', event.GetVeto(), event.CanVeto()
    self.Destroy()

  def OnExit(self, event):
    print 'Main: OnExit'
    self.Close()

class Logger:
  def __init__(self):
    z= 1 # Do nothing

Not really a problem... have yet come across the "pass" statement? Just in
the interest of clarity...! ::sunglasses:

  def WriteText(self, txt= 'anything'):
    print txt

...

Have a good one! (Whoops, I shouldn't use colloquialisms on an
international mailing list!... Have a good day! ::sunglasses:

Chris

···

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users

>>> TRAP FRAME
Traceback (most recent call last):
  File "G:\My Files\Python\Testing\TextGrief.py", line 82, in OnInit
    runTest(self, frame, self.log)
  File "G:\My Files\Python\Testing\TextGrief.py", line 24, in runTest
    f= Field(frame, -1, 'UGH', size= (60, 22))
  File "G:\My Files\Python\Testing\TextGrief.py", line 8, in __init__
    wxTextCtrl(self, parent, Id= Id, pos= pos, value= value, size=size)
  File "g:\python20\wxPython\controls.py", line 561, in __init__
    self.this = apply(controlsc.new_wxTextCtrl,_args,_kwargs)
AttributeError: 'MainFrame' instance has no attribute '__int__'
>>>

Colin W.

''' textGrief.py To subclass a TextCtrl '''
from wxPython.wx import *
class Field(wxTextCtrl):
  def __init__(self, parent, Id= -1, value= ' ',
               pos= (-1, -1),
               size= wxDefaultSize):
    wxTextCtrl(self, parent, Id= Id, pos= pos, value= value, size=size)

Okay, look at this line and look at the traceback. It says MainFrame (I
assume this is 'parent') has no __int__, so it is trying to convert parent
to an integer.

Look at the line again... When wxTextCtrl is used this way it is creating a
*NEW* wxTextCtrl, so it expects (parentwindow, int_id, pos, ...) but you are
giving it (text_window, frame_window, int, ...) See the problem?

What you probably wanted is a call to the unbound method __init__

    wxTextCtrl.__init__(self, parent, Id= Id, pos= pos, value= value,
size=size)

···

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

_______________________________________________
wxPython-users mailing list
wxPython-users@lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/wxpython-users