【问题标题】:Getting the event name rather than integer ID in wxPython在 wxPython 中获取事件名称而不是整数 ID
【发布时间】:2011-07-01 21:53:39
【问题描述】:

我有以下代码:

self.sliderR.Bind(wx.EVT_SCROLL,self.OnSlide)

在函数OnSlide我已经插入代码pdb.set_trace()来帮助我调试。

如果我在 pdb 提示符中键入 event.GetEventType(),它会返回一个数字 (10136),但我不知道对应的是哪个事件。

10136 是指wx.EVT_SCROLL 还是其他也触发wx.EVT_SCROLL 事件的事件?如果后者为真,如何找到具体事件?

谢谢。

【问题讨论】:

    标签: python event-handling wxpython


    【解决方案1】:

    没有内置方法。您将需要构建一个事件字典。 Robin Dunn 有一些代码可以帮助您:http://osdir.com/ml/wxpython-users/2009-11/msg00138.html

    或者您可以查看我的简单示例:

    import wx
    
    class MyForm(wx.Frame):
    
        def __init__(self):
            wx.Frame.__init__(self, None, title="Tutorial")
    
            self.eventDict = {}
            for name in dir(wx):
                if name.startswith('EVT_'):
                    evt = getattr(wx, name)
                    if isinstance(evt, wx.PyEventBinder):
                        self.eventDict[evt.typeId] = name
    
            # Add a panel so it looks the correct on all platforms
            panel = wx.Panel(self, wx.ID_ANY)
            btn = wx.Button(panel, wx.ID_ANY, "Get POS")
    
            btn.Bind(wx.EVT_BUTTON, self.onEvent)
            panel.Bind(wx.EVT_LEFT_DCLICK, self.onEvent)
            panel.Bind(wx.EVT_RIGHT_DOWN, self.onEvent)
    
    
        def onEvent(self, event):
            """
            Print out what event was fired
            """
            evt_id = event.GetEventType()
            print self.eventDict[evt_id]
    
    
    # Run the program
    if __name__ == "__main__":
        app = wx.App(False)
        frame = MyForm().Show()
        app.MainLoop()
    

    【讨论】:

    • 这很有用!我通常手工制作一本字典,但显然不能很好地扩展。我想我会看看是否可以合并您的示例:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-10
    • 2020-11-12
    • 2018-01-16
    • 2011-10-28
    • 2015-06-12
    • 1970-01-01
    • 2021-01-12
    相关资源
    最近更新 更多