[d]wxPython的button点击响应事件为啥发生两次

[d]wxPython的button点击响应事件为什么发生两次
本帖最后由 fibbery 于 2012-08-25 21:39:26 编辑
使用的版本信息为:
python:2.5.1
wxPython:wxPython2.8-win32-unicode-2.8.7.1-py25
代码如下:

import wx

class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Hello My World")
b = wx.Button(self, -1, "Create and Show a Frame", (50,50))
self.Bind(wx.EVT_BUTTON, self.OnButton, b)
self.Bind(wx.EVT_CLOSE, self.OnClose)
self.Show()
def OnButton(self, evt):
dlg = wx.MessageDialog(self, 'Hello from Python and wxPython!',
'A Message Box',
wx.OK | wx.ICON_INFORMATION
#wx.YES_NO | wx.NO_DEFAULT | wx.CANCEL | wx.ICON_INFORMATION
)
dlg.ShowModal()
dlg.Destroy()
evt.Skip()
def OnClose(self, evt):
dlg = wx.MessageDialog(self, 'Are you sure you want to close My World?',
'Closing...', wx.YES_NO | wx.ICON_QUESTION)
ret = dlg.ShowModal()
dlg.Destroy()
if ret == wx.ID_YES:
evt.Skip()

app = wx.App(0)
MyFrame()
app.MainLoop()

现在的问题是:我点击button之后对话框会出现两次,搞了好久,
实在是搜索不到答案了,请各位大侠帮忙看下,thks!!!---------------------
Double行动:
原帖分数:40
帖子加分:40

------解决方案--------------------
代码看起来没问题,在我的机器上(linux+python2.7+wxpython2.8)运行也没问题。

对话框是两个一起出现?还是关上一个又出一个?

怀疑是鼠标灵敏度的问题。试试用键盘的回车键来打开/关上对话框。
------解决方案--------------------
引用:
关键的问题是我不明白为什么我在frame中捕捉到了事件,然后skip,让事件抛向上层继续处理
但是frame的parent为空,按理说应该不会出现第二个弹出窗口啊,这才是我不明白的地方


我还是觉得这个问题不是skip引起的。在下面的代码中,onButton会改变事件的id,如果是同一个事件引起onButton运行两次,则会打印出连续的id。用你的有问题的环境组合运行它,看错误发生时输出是什么?


import wx

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="Hello My World")
        #panel = wx.Panel(self, -1)
        b = wx.Button(self, -1, "Create and Show a Frame", (50,50))
        self.Bind(wx.EVT_BUTTON, self.OnButton, b)
        self.Bind(wx.EVT_CLOSE, self.OnClose)
        self.Show()
    def OnButton(self, evt):
        print "before change id:", evt.GetId()
        evt.SetId(evt.GetId()+1)
        print "after change id:", evt.GetId()
        dlg = wx.MessageDialog(self, 'Hello from Python and wxPython!',
                                'A Message Box',
                                wx.OK