How to get the details of an event in wxPython

非 Y 不嫁゛ 提交于 2019-12-25 01:21:34

问题


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.


回答1:


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.




回答2:


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()


来源:https://stackoverflow.com/questions/6528441/how-to-get-the-details-of-an-event-in-wxpython

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!