how to get event id's in wxPython

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

    how to get event id's in wxPython

    I am currently trying to write a program where all events will be handled by one function.To do this appropriately i believe i would need to use 'if' statements to create an output depending on the widget pressed.So i tried to use the following:
    Code:
    id=event.GetId()
    However when i try to run the program
    i get the following error:
    Code:
    Traceback (most recent call last):
                    File "C:\Python25\CalculatorBlueprint.py",line 64,in handler
                       id=event.GetId()
    NameError:global name 'event' is not defined
    Can anyone show me where i have gone wrong?By the way i use python25 with IDLE and the wx module.
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by dynamo
    I am currently trying to write a program where all events will be handled by one function.To do this appropriately i believe i would need to use 'if' statements to create an output depending on the widget pressed
    First of all, DON'T DO IT. It may seem like a fun project, but events/event handlers are designed to make programming easy and programs (sort of) easy to debug. Sometimes events share certain functions. Those are found through a process called "refactorin g" whereby you go through and eliminate duplication AFTER your program works.

    Secondly, event handlers take two arguments: self and event.
    You can call your own class methods from within the handler and pass the event to it if it needs event info:
    Code:
    def OnThisEvent(self, event):
        self.OnThatEvent(event)
    Let's go back to the basic example, shall we:
    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

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by bartonc
      Originally posted by dynamo
      I am currently trying to write a program where all events will be handled by one function.To do this appropriately i believe i would need to use 'if' statements to create an output depending on the widget pressed
      First of all, DON'T DO IT. It may seem like a fun project, but events/event handlers are designed to make programming easy and programs (sort of) easy to debug. Sometimes events share certain functions. Those are found through a process called "refactorin g" whereby you go through and eliminate duplication AFTER your program works.

      Secondly, event handlers take two arguments: self and event.
      You can call your own class methods from within the handler and pass the event to it if it needs event info:
      Code:
      def OnThisEvent(self, event):
          self.OnThatEvent(event)
      Let's go back to the basic example, shall we:
      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()
      wazzup
      Something is wrong - perhaps a site bug - will investigate

      Comment

      • dynamo
        New Member
        • Apr 2007
        • 51

        #4
        Originally posted by bartonc
        First of all, DON'T DO IT. It may seem like a fun project, but events/event handlers are designed to make programming easy and programs (sort of) easy to debug. Sometimes events share certain functions. Those are found through a process called "refactorin g" whereby you go through and eliminate duplication AFTER your program works.

        Secondly, event handlers take two arguments: self and event.
        You can call your own class methods from within the handler and pass the event to it if it needs event info:
        Code:
        def OnThisEvent(self, event):
            self.OnThatEvent(event)
        Let's go back to the basic example, shall we:
        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 for replying.I understand the point you are making but my problem is i am trying to write a calculator program and if i define a function for each event,i will have to define so many functions.What really disturbs me is that i have seen 'event.GetId()' used in other programs but it doesn't seem to work in mine.

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by dynamo
          Thanks for replying.I understand the point you are making but my problem is i am trying to write a calculator program and if i define a function for each event,i will have to define so many functions.What really disturbs me is that i have seen 'event.GetId()' used in other programs but it doesn't seem to work in mine.
          Ok. That sounds smart. Here's what you're looking for:
          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, '1', pos=(50,50))
                  # Need to Bind() to the event
                  self.Button1.Bind(wx.EVT_BUTTON, self.OnAnyButton, id=wxID_BUTTON1)
          
                  self.Button2 = wx.Button(self, wxID_BUTTON2, '2', pos=(120,50))
                  # Need to Bind() to the event
                  self.Button2.Bind(wx.EVT_BUTTON, self.OnAnyButton, id=wxID_BUTTON2)
          
          
                  # just one way to give a button a certain value
                  self.buttonDict = {self.Button1:1, self.Button2:2}
          
              def OnAnyButton(self, event):
                  thisButton = event.GetEventObject()
                  print self.buttonDict[thisButton]
          
          
          
          if __name__ == "__main__":
              app = wx.PySimpleApp()
              frame = Mine(None, 'filefinder', pos=wx.DefaultPosition, size=wx.DefaultSize)
              frame.Show(True)
              app.MainLoop()

          Comment

          • dynamo
            New Member
            • Apr 2007
            • 51

            #6
            Originally posted by bartonc
            Ok. That sounds smart. Here's what you're looking for:
            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, '1', pos=(50,50))
                    # Need to Bind() to the event
                    self.Button1.Bind(wx.EVT_BUTTON, self.OnAnyButton, id=wxID_BUTTON1)
            
                    self.Button2 = wx.Button(self, wxID_BUTTON2, '2', pos=(120,50))
                    # Need to Bind() to the event
                    self.Button2.Bind(wx.EVT_BUTTON, self.OnAnyButton, id=wxID_BUTTON2)
            
            
                    # just one way to give a button a certain value
                    self.buttonDict = {self.Button1:1, self.Button2:2}
            
                def OnAnyButton(self, event):
                    thisButton = event.GetEventObject()
                    print self.buttonDict[thisButton]
            
            
            
            if __name__ == "__main__":
                app = wx.PySimpleApp()
                frame = Mine(None, 'filefinder', pos=wx.DefaultPosition, size=wx.DefaultSize)
                frame.Show(True)
                app.MainLoop()
            So for 'event' to be recognized you have to bind the object to the appropriate event.Thanks for saving me again

            Comment

            • bartonc
              Recognized Expert Expert
              • Sep 2006
              • 6478

              #7
              Originally posted by dynamo
              So for 'event' to be recognized you have to bind the object to the appropriate event.Thanks for saving me again
              You are quite welcome.

              Keep posting,
              Barton

              Comment

              Working...