wxPython Event handling problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dynamo
    New Member
    • Apr 2007
    • 51

    #1

    wxPython Event handling problem

    Hi everyone.I'm new to wxpython.And i have a little problem.I tried to run the following wxpython app code in IDLE for python25:
    Code:
    import wx
    import os
    import re
    def z(a):
        for i in range(1,13):
            return i*a
    class Mine(wx.Frame):
        def __init__(self,parent,id,title,pos,size,):
            wx.Frame.__init__(self,parent,id,title,pos,size)
            self.inp=wx.TextCtrl(self,1,'type here')
            self.buton=wx.Button(self,2,'answer',pos=(50,50))
            self.b=wx.Button(self,3,'exit',pos=(120,50))
            wx.EVT_BUTTON(self,2,self.disp)
            wx.EVT_BUTTON(self,3,self.end)
            self.Show(True)
        def disp(self,eve):
            x=self.inp.GetValue()
            d=z(x)
            f=wx.MessageDialog(self,d,wx.OK)
            f.ShowModal()
            f.Destroy()
        def end(self):
            raise SystemExit()
    app=wx.PySimpleApp()
    frame=Mine(None,wx.ID_ANY,'filefinder',pos=wx.DefaultPosition,size=wx.DefaultSize)
    app.MainLoop()
    
    And i got the following error:
      >>> 
    Traceback (most recent call last):
      File "C:\Python25\mine.py", line 19, in disp
        f=wx.MessageDialog(self,d,wx.OK)
      File "C:\Python25\Lib\site-packages\wx-2.8-msw-ansi\wx\_windows.py", line 2898, in __init__
        _windows_.MessageDialog_swiginit(self,_windows_.new_MessageDialog(*args, **kwargs))
    TypeError: String or Unicode type required
    TypeError: end() takes exactly 1 argument (2 given)
    >>>
    Can someone please show me where i have gone wrong?Thanks
    Last edited by bartonc; Apr 13 '07, 06:56 PM. Reason: added [code][/code] tags
  • Arnold Schuur
    New Member
    • Apr 2007
    • 36

    #2
    You pass the wrong number of arguments to the MessageDialog function

    Try this:
    Code:
    f = wx.MessageDialog(self, "Message","Title",wx.OK)

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Code:
      import wx
      import os
      import re
      
      # Don't hard code ID nubbers! Use NewID()!
      [wxID_FRAME1, wxID_BUTTON1, wxID_BUTTON2,
      ] = [wx.NewId() for _init_ctrls in range(3)]
      
      
      class Mine(wx.Frame):
          def __init__(self, parent, title, pos, size,):
              wx.Frame.__init__(self, parent, wxID_FRAME1, title, pos, size)
              self.InputTextCtrl = wx.TextCtrl(self,1,'type here')
              self.Button1 = wx.Button(self, wxID_BUTTON1, 'answer', pos=(50,50))
              # Need to Bind() to the event
              self.Button1.Bind(wx.EVT_BUTTON, self.OnButton1, id=wxID_BUTTON1)
      
              self.Button2 = wx.Button(self, wxID_BUTTON2, 'exit', pos=(120,50))
              # Need to Bind() to the event
              self.Button2.Bind(wx.EVT_BUTTON, self.OnButton2, id=wxID_BUTTON2)
      
      ##        wx.EVT_BUTTON(self,2,self.disp)
      ##        wx.EVT_BUTTON(self,3,self.end)
      
              self.Show(True)
      
          # Use better names for objects and handlers (conventionally start with "On").
      
          def OnButton1(self, event):
              x = self.InputTextCtrl.GetValue()
      ##        d=z(x)
              f = wx.MessageDialog(self, "message: x = %s" %x, "title", wx.OK)
              answer = f.ShowModal()
              if answer == wx.ID_OK:
                  print "OK"
              f.Destroy()
      
          def OnButton2(self, event):  # event was left out
              # let wx cleanup! #
              self.Destroy()
      ##        raise SystemExit()
      
      
      
      app=wx.PySimpleApp()
      frame=Mine(None, 'filefinder', pos=wx.DefaultPosition, size=wx.DefaultSize)
      app.MainLoop()

      Comment

      • dynamo
        New Member
        • Apr 2007
        • 51

        #4
        Originally posted by Arnold Schuur
        You pass the wrong number of arguments to the MessageDialog function

        Try this:
        Code:
        f = wx.MessageDialog(self, "Message","Title",wx.OK)
        thank you.

        Comment

        • dynamo
          New Member
          • Apr 2007
          • 51

          #5
          Originally posted by bartonc
          Code:
          import wx
          import os
          import re
          
          # Don't hard code ID nubbers! Use NewID()!
          [wxID_FRAME1, wxID_BUTTON1, wxID_BUTTON2,
          ] = [wx.NewId() for _init_ctrls in range(3)]
          
          
          class Mine(wx.Frame):
              def __init__(self, parent, title, pos, size,):
                  wx.Frame.__init__(self, parent, wxID_FRAME1, title, pos, size)
                  self.InputTextCtrl = wx.TextCtrl(self,1,'type here')
                  self.Button1 = wx.Button(self, wxID_BUTTON1, 'answer', pos=(50,50))
                  # Need to Bind() to the event
                  self.Button1.Bind(wx.EVT_BUTTON, self.OnButton1, id=wxID_BUTTON1)
          
                  self.Button2 = wx.Button(self, wxID_BUTTON2, 'exit', pos=(120,50))
                  # Need to Bind() to the event
                  self.Button2.Bind(wx.EVT_BUTTON, self.OnButton2, id=wxID_BUTTON2)
          
          ##        wx.EVT_BUTTON(self,2,self.disp)
          ##        wx.EVT_BUTTON(self,3,self.end)
          
                  self.Show(True)
          
              # Use better names for objects and handlers (conventionally start with "On").
          
              def OnButton1(self, event):
                  x = self.InputTextCtrl.GetValue()
          ##        d=z(x)
                  f = wx.MessageDialog(self, "message: x = %s" %x, "title", wx.OK)
                  answer = f.ShowModal()
                  if answer == wx.ID_OK:
                      print "OK"
                  f.Destroy()
          
              def OnButton2(self, event):  # event was left out
                  # let wx cleanup! #
                  self.Destroy()
          ##        raise SystemExit()
          
          
          
          app=wx.PySimpleApp()
          frame=Mine(None, 'filefinder', pos=wx.DefaultPosition, size=wx.DefaultSize)
          app.MainLoop()
          thanks.I actually learned one or two things about wxpython from your message.Thanks again

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            Originally posted by dynamo
            thanks.I actually learned one or two things about wxpython from your message.Thanks again
            You are welcome. That's what TheScripts is all about.

            Keep posting,
            Barton

            Comment

            Working...