wxValidator with wxSpinCtrl?

Is there a way to use a validator with the spin control? I want to keep
non-digits from being entered. Thanks.

Hi all :slight_smile:

I made a dialog window, derived from wxDialog of course, and in it put four radio buttons.

Everything works fine (I can set, get thier values and all that stuff), untill I try to register the radio button events.

I tried messing around with it, but the error message might as well be
in chinease, I have no idea what’s wrong, no clue as to where to look.

If you comment out the EVT lines, the program works fine. The purpose behind
registering the radio button events is to disable the ‘Link To:’ TextCtrl, which is called ‘link’ in the code.

Here’s the error i get:

Traceback (most recent call last):

File "C:\Documents and Settings\Amos\My Documents\Python\AI\Behav Maker.py", line 159, in OnAddWordNode

  win = NodeDialog(self, -1, "New Node", wxSize(350, 200), wxCAPTION, 'W')

File "C:\Documents and Settings\Amos\My Documents\Python\AI\Behav Maker.py", line 71, in __init__

  EVT_RADIOBUTTON(self, self.RB1, self.OnRB)

File "C:\PYTHON22\Lib\site-packages\wxPython\wx.py", line 1341, in EVT_RADIOBUTTON

  win.Connect(id, -1, wxEVT_COMMAND_RADIOBUTTON_SELECTED, func)

File "C:\PYTHON22\Lib\site-packages\wxPython\windows.py", line 54, in Connect

  val = apply(windowsc.wxEvtHandler_Connect,(self,) + _args, _kwargs)

AttributeError: wxRadioButton instance has no attribute ‘int

And heres the code, I bolded the place where I register the event:

···

from wxPython.wx import *

def parse(Text):
for i in string.punctuation:
if Text.count(i) > 0:
Text = Text.replace(i, " " + i + " ")
Text = Text.split()
return Text

class NodeDialog(wxDialog):
def init(self, parent, id, title, size, style, default):
wxDialog.init(self, parent, id, title, size=size, style=style)
sizer = wxBoxSizer(wxVERTICAL)

      box = wxBoxSizer(wxHORIZONTAL)

      label = wxStaticText(self, -1, "Word:")
      box.Add(label, 0, wxALIGN_CENTRE|wxALL, 5)

      text = wxTextCtrl(self, -1, "", size=(80,-1))
      box.Add(text, 1, wxALIGN_CENTRE|wxALL, 5)

      sizer.AddSizer(box, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5)

      box = wxBoxSizer(wxHORIZONTAL)

      label = wxStaticText(self, -1, "Link To:")
      box.Add(label, 0, wxALIGN_CENTRE|wxALL, 5)

      link = wxTextCtrl(self, -1, "", size=(80,-1))
      box.Add(link, 1, wxALIGN_CENTRE|wxALL, 5)

      sizer.AddSizer(box, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 5)

      self.RB = wxRadioButton(self, -51, "Word-Node")
      sizer.Add(self.RB, 0, wxLEFT|wxRIGHT, 20)
      if default == 'W':
          self.RB.SetValue(1)
     
      self.RB1 = wxRadioButton(self, -52, "Arg-Node")
      sizer.Add(self.RB1, 0, wxLEFT|wxRIGHT, 20)
      if default == 'A':
          self.RB1.SetValue(1)          
      
      self.RB2 = wxRadioButton(self, -53, "Function-Node")
      sizer.Add(self.RB2, 0, wxLEFT|wxRIGHT, 20)
      if default == 'F':
          self.RB2.SetValue(1)
     
      self.RB3 = wxRadioButton(self, -54, "Link-Node")
      sizer.Add(self.RB3, 0, wxLEFT|wxRIGHT, 20)
      if default == 'L':
          self.RB3.SetValue(1)

      box = wxBoxSizer(wxHORIZONTAL)

      btn = wxButton(self, wxID_OK, " OK ")
      btn.SetDefault()
      box.Add(btn, 0, wxALIGN_CENTRE|wxALL, 5)

      btn = wxButton(self, wxID_CANCEL, " Cancel ")
      box.Add(btn, 0, wxALIGN_CENTRE|wxALL, 5)

      sizer.AddSizer(box, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5)

      self.SetSizer(sizer)
      self.SetAutoLayout(true)
      sizer.Fit(self)

**EVT_RADIOBUTTON(self, self.RB, self.OnRB)**
**EVT_RADIOBUTTON(self, self.RB1, self.OnRB)
      EVT_RADIOBUTTON(self, self.RB2, self.OnRB)
      EVT_RADIOBUTTON(self, self.RB3, self.OnRB3)**



  def OnRB(self, event):

      self.link.SetEditable(0)



  def OnRB3(self, event):

      self.link.SetEditable(1)

class MyTreeCtrl(wxTreeCtrl):

  def __init__(self, parent, id, pos, size, style, log):

      wxTreeCtrl.__init__(self, parent, id, pos, size, style)

      self.log = log



  def OnCompareItems(self, item1, item2):

      t1 = self.GetItemText(item1)

      t2 = self.GetItemText(item2)

      self.log.WriteText('compare: ' + t1 + ' <> ' + t2 + '\n')

      if t1 < t2: return -1

      if t1 == t2: return 0

      return 1

class MyFrame(wxFrame):

  def __init__(self, parent, id, title):

      wxFrame.__init__(self, parent, id, title,

                       wxPoint(100, 100), wxSize(500, 500))



      EVT_CLOSE(self, self.OnCloseWindow)



      self.log = ""



      self.tree = MyTreeCtrl(self, -1, wxDefaultPosition, wxDefaultSize,

                             wxTR_HAS_BUTTONS

                             , self.log)

      self.root = self.tree.AddRoot("Words")

      self.tree.SetPyData(self.root, None)

     



      menuBar = wxMenuBar()



      menu1 = wxMenu()

      menu1.Append(101, "New\tCtrl+N", "")

      menu1.Append(102, "Open\tCtrl+O", "")

      menu1.Append(103, "Save\tCtrl+T", "")

      menu1.Append(104, "Save As", "")

      menu1.AppendSeparator()

      menu1.Append(105, "Make Script\tCtrl+M", "")

      menu1.Append(106, "Generate BHV file\tCtrl+G", "")

      menu1.AppendSeparator()

      menu1.Append(107, "Close", "Close this frame")

     

      menuBar.Append(menu1, "&File")



      menu2 = wxMenu()

      menu2.Append(201, "Word-Node\tCtrl+W")

      menu2.Append(202, "Arg-Node\tCtrl+A")

      menu2.Append(203, "Function-Node\tCtrl+F")

      menu2.Append(204, "Link-Node\tCtrl+A")

      menu2.Append(205, "Continue sentence\tCtrl+T")

      menu2.Append(206, "New sentence\tCtrl+E")



      menuBar.Append(menu2, "&Add")



      menu3 = wxMenu()

      menu3.Append(301, "Item\tCtrl+R")

      menu3.Append(302, "All Items")

      menu3.Append(303, "Sentence")

      menu3.Append(304, "All Children")



      menuBar.Append(menu3, "&Remove")



      self.SetMenuBar(menuBar)



      EVT_MENU(self, 107, self.OnCloseWindow)

      EVT_MENU(self, 201, self.OnAddWordNode)

      EVT_MENU(self, 202, self.OnAddArgNode)

      EVT_MENU(self, 203, self.OnAddFunctionNode)

      EVT_MENU(self, 204, self.OnAddLinkNode)

      EVT_MENU(self, 301, self.OnRemoveItem)

EVT_MENU(self, 302, self.OnRemoveAll)

  def OnCloseWindow(self, event):

      self.Destroy()



  def OnAddWordNode(self, event):

      while 1:

          win = NodeDialog(self, -1, "New Node", wxSize(350, 200), wxCAPTION, 'W')

          ok = true

          if win.ShowModal() == wxID_OK:

              text = win.text.GetValue()

              if win.RB3.GetValue():

                  text += 'L'                                       


              if text == '' and not win.RB3.GetValue():

                  ok = false

                  dlg = wxMessageDialog(self, 'You must enter a word.','Error', wxOK)

                  dlg.ShowModal()

                  dlg.Destroy()

              elif len(parse(text)) > 1:

                  ok = false

                  dlg = wxMessageDialog(self, 'Too many words.','Error', wxOK)

                  dlg.ShowModal()

                  dlg.Destroy()

              else:

                  if win.RB.GetValue():

                      text = 'R:' + text

                  if win.RB1.GetValue():

                      text = 'A:' + text

                  if win.RB2.GetValue():

                      text = 'F:' + text

                  if win.RB3.GetValue():

                      text = 'L:' + text

                  item = self.tree.GetSelection()

                  child = self.tree.AppendItem(item, text)

                  self.tree.SetPyData(child, None)

                  self.tree.Expand(item)

          win.Destroy()

          if ok:

              break

 

  def OnAddArgNode(self, event):

      while 1:

          win = NodeDialog(self, -1, "New Node", wxSize(350, 200), wxCAPTION, 'A')

          ok = true

          if win.ShowModal() == wxID_OK:

              text = win.text.GetValue()

              if win.RB3.GetValue():

                  text += 'L'                                       


              if text == '' and not win.RB3.GetValue():

                  ok = false

                  dlg = wxMessageDialog(self, 'You must enter a word.','Error', wxOK)

                  dlg.ShowModal()

                  dlg.Destroy()

              elif len(parse(text)) > 1:

                  ok = false

                  dlg = wxMessageDialog(self, 'Too many words.','Error', wxOK)

                  dlg.ShowModal()

                  dlg.Destroy()

              else:

                  if win.RB.GetValue():

                      text = 'R:' + text

                  if win.RB1.GetValue():

                      text = 'A:' + text

                  if win.RB2.GetValue():

                      text = 'F:' + text

                  if win.RB3.GetValue():

                      text = 'L:' + text

                  item = self.tree.GetSelection()

                  child = self.tree.AppendItem(item, text)

                  self.tree.SetPyData(child, None)

                  self.tree.Expand(item)

          win.Destroy()

          if ok:

              break



  def OnAddFunctionNode(self, event):

      while 1:

          win = NodeDialog(self, -1, "New Node", wxSize(350, 200), wxCAPTION, 'F')

          ok = true

          if win.ShowModal() == wxID_OK:

              text = win.text.GetValue()

              if win.RB3.GetValue():

                  text += 'L'                                       


              if text == '' and not win.RB3.GetValue():

                  ok = false

                  dlg = wxMessageDialog(self, 'You must enter a word.','Error', wxOK)

                  dlg.ShowModal()

                  dlg.Destroy()

              elif len(parse(text)) > 1:

                  ok = false

                  dlg = wxMessageDialog(self, 'Too many words.','Error', wxOK)

                  dlg.ShowModal()

                  dlg.Destroy()

              else:

                  if win.RB.GetValue():

                      text = 'R:' + text

                  if win.RB1.GetValue():

                      text = 'A:' + text

                  if win.RB2.GetValue():

                      text = 'F:' + text

                  if win.RB3.GetValue():

                      text = 'L:' + text

                  item = self.tree.GetSelection()

                  child = self.tree.AppendItem(item, text)

                  self.tree.SetPyData(child, None)

                  self.tree.Expand(item)

          win.Destroy()

          if ok:

              break

         

  def OnAddLinkNode(self, event):

      while 1:

          win = NodeDialog(self, -1, "New Node", wxSize(350, 200), wxCAPTION, 'L')

          ok = true

          if win.ShowModal() == wxID_OK:

              text = win.text.GetValue()

              if win.RB3.GetValue():

                  text += 'L'                                       


              if text == '' and not win.RB3.GetValue():

                  ok = false

                  dlg = wxMessageDialog(self, 'You must enter a word.','Error', wxOK)

                  dlg.ShowModal()

                  dlg.Destroy()

              elif len(parse(text)) > 1:

                  ok = false

                  dlg = wxMessageDialog(self, 'Too many words.','Error', wxOK)

                  dlg.ShowModal()

                  dlg.Destroy()

              else:

                  if win.RB.GetValue():

                      text = 'R:' + text

                  if win.RB1.GetValue():

                      text = 'A:' + text

                  if win.RB2.GetValue():

                      text = 'F:' + text

                  if win.RB3.GetValue():

                      text = 'L:' + text

                  item = self.tree.GetSelection()

                  child = self.tree.AppendItem(item, text)

                  self.tree.SetPyData(child, None)

                  self.tree.Expand(item)

          win.Destroy()

          if ok:

              break



  def OnRemoveItem(self, event):

      item = self.tree.GetSelection()

      if not self.tree.GetItemText(item) == 'Words':

          self.tree.Delete(item)



  def OnRemoveAll(self, event):

      self.tree.DeleteAllItems()

      self.root = self.tree.AddRoot("Words")

class MyApp(wxApp):

  def OnInit(self):



      frame = MyFrame(None, -1, "Behav Maker")

      frame.Show(true)



      self.SetTopWindow(frame)



      return true

app = MyApp(0)

app.MainLoop()

Try EVT_RADIOBUTTON(self, self.RB.GetId(), self.OnRB)

The 'has no attribute __int__' message usually means it is looking for an integer ID, not the object itself.

Mark.

Amos Joshua wrote:

···

Hi all :slight_smile:

I made a dialog window, derived from wxDialog of course, and in it put four radio buttons.
Everything works fine (I can set, get thier values and all that stuff), untill I try to register the radio button events.
I tried messing around with it, but the error message might as well be in chinease, I have no idea what's wrong, no clue as to where to look.
If you comment out the EVT lines, the program works fine. The purpose behind registering the radio button events is to disable the 'Link To:' TextCtrl, which is called 'link' in the code.
Here's the error i get:

    Traceback (most recent call last):
      File "C:\Documents and Settings\Amos\My
    Documents\Python\AI\Behav Maker.py", line 159, in OnAddWordNode
        win = NodeDialog(self, -1, "New Node", wxSize(350, 200),
    wxCAPTION, 'W')
      File "C:\Documents and Settings\Amos\My
    Documents\Python\AI\Behav Maker.py", line 71, in __init__
        EVT_RADIOBUTTON(self, self.RB1, self.OnRB)
      File "C:\PYTHON22\Lib\site-packages\wxPython\wx.py", line 1341,
    in EVT_RADIOBUTTON
        win.Connect(id, -1, wxEVT_COMMAND_RADIOBUTTON_SELECTED, func)
      File "C:\PYTHON22\Lib\site-packages\wxPython\windows.py", line
    54, in Connect
        val = apply(windowsc.wxEvtHandler_Connect,(self,) + _args,
    _kwargs)
    AttributeError: wxRadioButton instance has no attribute '__int__'

I made a dialog window, derived from wxDialog of course, and in
it put four radio buttons. Everything works fine (I can set, get
thier values and all that stuff), untill I try to register the
radio button events. I tried messing around with it, but the
error message might as well be in chinease, I have no idea
what's wrong, no clue as to where to look. If you comment out
the EVT lines, the program works fine.

<snip>

        EVT_RADIOBUTTON(self, self.RB, self.OnRB)
        EVT_RADIOBUTTON(self, self.RB1, self.OnRB)
        EVT_RADIOBUTTON(self, self.RB2, self.OnRB)
        EVT_RADIOBUTTON(self, self.RB3, self.OnRB3)

Try this instead:

        EVT_RADIOBUTTON(self, self.RB.GetId(), self.OnRB)
        EVT_RADIOBUTTON(self, self.RB1.GetId(), self.OnRB)
        EVT_RADIOBUTTON(self, self.RB2.GetId(), self.OnRB)
        EVT_RADIOBUTTON(self, self.RB3.GetId(), self.OnRB3)

Then you will need to change:

        link = wxTextCtrl(self, -1, "", size=(80,-1))
        box.Add(link, 1, wxALIGN_CENTRE|wxALL, 5)

to:
        self.link = wxTextCtrl(self, -1, "", size=(80,-1))
        box.Add(self.link, 1, wxALIGN_CENTRE|wxALL, 5)

Regards,

···

--- Amos Joshua <andrewthegreen@netscape.net> wrote:

=====
Donnal Walter
Arkansas Children's Hospital

__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus � Powerful. Affordable. Sign up now.

Mark Melvin wrote:

> Try EVT_RADIOBUTTON(self, self.RB.GetId(), self.OnRB)
>
> The 'has no attribute __int__' message usually means it is looking for
> an integer ID, not the object itself.
>
> Mark.

Ahh, that works fine. Thanks so much!