简体   繁体   中英

How to get the details of an event in wxPython

I have a section of code which returns events generated by a slider.

I bind the event with self.Bind(wx.EVT_SCROLL,self.OnSlide) .

The code which handles the event reads something like:

def OnSlide(self,event):
    widget = event.GetEventObject()

This is great but an error gets thrown every time the code is executed. It reads:

AttributeError: 'PyEventBinder' object has no attribute 'GetEventObject'

I want to be able to see which of the sliders generated the event but the error appears every time I attempt to find out.

How can I get the code to execute correctly?

Many thanks in advance.

To debug something like this, put the following as the first statement in your event handler:

import pdb; pdb.set_trace()

This will stop the execution of the program at this point and give you an interactive prompt. You can then issue the following command to find out what methods are available:

print dir(event)

When I was first learning wxPython I found this technique invaluable.

The following works for me on Windows 7, wxPython 2.8.10.1, Python 2.5

import wx

class MyForm(wx.Frame):

    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None, title="Tutorial")

        # Add a panel so it looks the correct on all platforms
        panel = wx.Panel(self, wx.ID_ANY)

        slider = wx.Slider(panel, size=wx.DefaultSize)
        slider.Bind(wx.EVT_SLIDER, self.onSlide)

    #----------------------------------------------------------------------
    def onSlide(self, event):
        """"""
        obj = event.GetEventObject()
        print obj

#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm().Show()
    app.MainLoop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM