Django
Django

Reputation: 415

How to notify the application if a window closes

I have a wxpython application. From this application you can launch a window (frame) that exists independently, so outside of the frame of the main application. The main application is aware of this window, the window itself is not (yet) aware of its parent.

If one closes the window, the main application should know this because it needs to stop sending certain updates. In principle easy to do, I just pass the parent to the window, when it closes I catch an event and let the parent know the window has been shut just before the window closes.

Is there another way to detect from the main application that this window has been shut? I am operating on the (dubious?) assumption that I should try to make the window unaware of the parent, but again, this maybe nonsense.

So what is the best way to let the parent know that this window has been shut?

Upvotes: 1

Views: 576

Answers (1)

Mike Driscoll
Mike Driscoll

Reputation: 33071

I would recommend using pubsub. It can send a message out that you have subscribed to in your main window (or other windows too) and act accordingly. I wrote a tutorial on the subject a while back that you might find helpful:

Here's some example code from that tutorial that does almost exactly what you're talking about

import wx
from wx.lib.pubsub import pub 

########################################################################
class OtherFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, wx.ID_ANY, "Secondary Frame")
        panel = wx.Panel(self)

        msg = "Enter a Message to send to the main frame"
        instructions = wx.StaticText(panel, label=msg)
        self.msgTxt = wx.TextCtrl(panel, value="")
        closeBtn = wx.Button(panel, label="Send and Close")
        closeBtn.Bind(wx.EVT_BUTTON, self.onSendAndClose)

        sizer = wx.BoxSizer(wx.VERTICAL)
        flags = wx.ALL|wx.CENTER
        sizer.Add(instructions, 0, flags, 5)
        sizer.Add(self.msgTxt, 0, flags, 5)
        sizer.Add(closeBtn, 0, flags, 5)
        panel.SetSizer(sizer)

    #----------------------------------------------------------------------
    def onSendAndClose(self, event):
        """
        Send a message and close frame
        """
        msg = self.msgTxt.GetValue()
        pub.sendMessage("panelListener", message=msg)
        pub.sendMessage("panelListener", message="test2", arg2="2nd argument!")
        self.Close()

########################################################################
class MyPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)
        pub.subscribe(self.myListener, "panelListener")

        btn = wx.Button(self, label="Open Frame")
        btn.Bind(wx.EVT_BUTTON, self.onOpenFrame)

    #----------------------------------------------------------------------
    def myListener(self, message, arg2=None):
        """
        Listener function
        """
        print "Received the following message: " + message
        if arg2:
            print "Received another arguments: " + str(arg2)

    #----------------------------------------------------------------------
    def onOpenFrame(self, event):
        """
        Opens secondary frame
        """
        frame = OtherFrame()
        frame.Show()

########################################################################
class MyFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="New PubSub API Tutorial")
        panel = MyPanel(self)
        self.Show()

#----------------------------------------------------------------------
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyFrame()
    app.MainLoop()

You might also want to take a look at the documentation:

You can use Pubsub outside of wxPython too if you download the separate library. the official website has some good examples that you can use in your wxPython application with a little adaptation:

Upvotes: 1

Related Questions