prevent the sub windows to open multiple times
Question: I am creating an application by using the language wxPython. I have a simple
problem in which I cant really find the solution in the internet.

I have a main user interface with a menubar which contain a menu called new
file. By clicking the new file, a new window will appear demanding the user to
fill up the necessary information.

The problem is that, by clicking multiple times the menu (new file), the
application opens multiple windows.

How can i prevent this?


Answer: The following code creates a new sub frame if one doesn't exists already. If
it does exist already, it uses the existing sub frame.

Note the code is tested with latest wxpython phoenix and classic.

    
    
    import wx
    from wx.lib import sized_controls
    
    
    class MultiMessageFrame(sized_controls.SizedFrame):
    
        def __init__(self, *args, **kwargs):
            super(MultiMessageFrame, self).__init__(*args, **kwargs)
            pane = self.GetContentsPane()
            text_ctrl = wx.TextCtrl(
                pane, style=wx.TE_READONLY | wx.TE_CENTRE | wx.TE_MULTILINE)
            text_ctrl.SetSizerProps(proportion=1, expand=True)
            text_ctrl.SetBackgroundColour('White')
            self.text_ctrl = text_ctrl
    
            pane_btns = sized_controls.SizedPanel(pane)
            pane_btns.SetSizerType('horizontal')
            pane_btns.SetSizerProps(align='center')
    
            button_ok = wx.Button(pane_btns, wx.ID_OK)
            button_ok.Bind(wx.EVT_BUTTON, self.on_button_ok)
    
        def append_msg(self, title_text, msg_text):
            self.SetTitle(title_text)
            self.text_ctrl.AppendText(msg_text)
    
        def on_button_ok(self, event):
            self.Close()
    
    
    class MainFrame(sized_controls.SizedFrame):
    
        def __init__(self, *args, **kwargs):
            super(MainFrame, self).__init__(*args, **kwargs)
            self.SetInitialSize((800, 600))
            self.CreateStatusBar()
            menubar = wx.MenuBar()
            self.SetMenuBar(menubar)
            menu_file = wx.Menu()
            menu_file.Append(
                wx.ID_NEW, 'Show msg', 'Add a new message to message frame')
            menubar.Append(menu_file, '&File')
            self.Bind(wx.EVT_MENU, self.on_new, id=wx.ID_NEW)
    
            self.count = 1
            self.multi_message_frame = None
    
        def on_new(self, event):
            title_text = 'MultiMessageFrame already exists'
    
            if not self.multi_message_frame:
                title_text = 'Newly created MultiMessageFrame'
                self.multi_message_frame = MultiMessageFrame(
                    self, style=wx.DEFAULT_FRAME_STYLE | wx.FRAME_FLOAT_ON_PARENT)
                self.multi_message_frame.Bind(
                    wx.EVT_CLOSE, self.on_multi_message_frame_close)
                self.multi_message_frame.Center()
                self.multi_message_frame.Show()
    
            self.multi_message_frame.append_msg(
                title_text, 'message no.{}\n'.format(self.count))
            self.count += 1
    
        def on_multi_message_frame_close(self, event):
            self.multi_message_frame = None
            event.Skip()
    
    
    if __name__ == '__main__':
        app = wx.App(False)
        main_frame = MainFrame(None)
        main_frame.Show()
        app.MainLoop()
    

