如何在主框架中捕获子帧关闭事件?

时间:2015-05-11 09:55:03

标签: python wxpython wxwidgets

在WxPython中,我在菜单上打开一个新框架(ChildFrame),单击父框架(ParentFrame)。子框架用于将数据添加到配置文件。添加数据后(即在关闭或在ChildFrame中单击按钮时),我需要让父框架知道并刷新。如何在wxPython中做到这一点?

class ParentFrame(wx.Frame):
    def __init__(self,parent,id):
        ...
        self.Bind(wx.EVT_MENU, self.ShowChildFrame, None, ID_MENU_ITEM)


    def ShowChildFrame(self, event):
        self.child_frame= ChildFrame(parent=None, id=-1, title='New Frame Title')
        self.Bind(self.child_frame.EVT_CLOSE, self.OnChildFrameClose) #this does not work since no EVT_CLOSE is available
        self.child_frame.Show()

    def OnChildFrameClose(self, event):
        #do something

 class ChildFrame(wx.Frame):
    ...

1 个答案:

答案 0 :(得分:0)

您必须绑定到子框架而不是父框架,并使用wx.EVT_CLOSE事件。

import wx


class ParentFrame(wx.Frame):

    def __init__(self, *args, **kwargs):
        super(ParentFrame, self).__init__(*args, **kwargs)

    def ShowChildFrame(self):
        self.child_frame = wx.Frame(self, title='New Frame Title')
        self.child_frame.Bind(wx.EVT_CLOSE, self.OnChildFrameClose)
        self.child_frame.Show()

    def OnChildFrameClose(self, event):
        print 'do something'
        event.Skip()


app = wx.App()
frame = ParentFrame(None, title='Parent Frame')
frame.Show()
frame.ShowChildFrame()
app.MainLoop()