I am creating an application that will have several windows. However, most of the windows should be created only one time. If window B already exists then instead of creating a new window B, the existing window B should be shown. It may be that window B was created but then destroyed and then, of course, I have to create window B again.
The problem is that I cannot find a way to know if windows B exists.
In the example below from the "Initial Window" you can create as many secondary windows as you want. I need a way to make sure that only one secondary window exists at a given time.
I am using Phython3.7 and wxPython 4.0.3
import wx
class FrameA(wx.Frame):
def __init__(self, parent, title):
super().__init__(parent, title=title)
self.i = 1
self.panel = wx.Panel(self)
self.sizer = wx.GridBagSizer(1, 1)
self.buttonUtil = wx.Button(self.panel, label='New Window')
self.sizer.Add(self.buttonUtil, pos=(0, 0))
self.panel.SetSizer(self.sizer)
self.buttonUtil.Bind(wx.EVT_BUTTON, self.Util)
self.Centre()
def Util(self, event):
frame = FrameB(None, title=str(self.i))
frame.Show()
self.i += 1
class FrameB(wx.Frame):
def __init__(self, parent, title):
super().__init__(parent, title=title)
app = wx.App()
frameA = FrameA(None, title='Initial Window')
frameA.Show()
app.SetTopWindow(frameA)
app.MainLoop()
Store created FrameB
in an object variable in FrameA
, e.g. as self.frame
. In FrameA
constructor initialize this variable to None
.
In constructor of FrameB
add parameter to store the creating FrameA
(if multiple can exist). Then bind to close event to set frame
of creating FrameA
to None
if B is closed.
In Util()
check first if frame
is None
. If so, create new FrameB
and assign it to self.frame
. Otherwise focus existing B.