Search code examples
wxpython

How can I have access on the MainFrame in wxPython after opening another frame?


What I am trying to do actually is I want to have access on all the frames I have created.Here I have created 3 frames - "MainFrame","FirstFrame","SecondFrame".So,I want to open,close or minimize any of the frames whenever I want.Is it possible to close the "MainFrame" without closing other frames??I have made a program.But here I can not have access on the MainFrame after opening other frames without closing them.

here is my code -

import wx

class MainFrame(wx.Frame):
    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,"Main WIndow",size=(600,400))
        panel=wx.Panel(self)

        self.button1=wx.Button(panel,label='First Window',pos=(80,30),size=(130,50))
        self.button2=wx.Button(panel,label='Second Window',pos=(80,100),size=(130,50))
        self.Bind(wx.EVT_BUTTON,self.evt1,self.button1)
        self.Bind(wx.EVT_BUTTON,self.evt2,self.button2)


    def evt1(self,parent):
        frame=FirstFrame(self,-1)
        frame.Show(True)
        frame.MakeModal(True)

    def evt2(self,parent):
        frame=SecondFrame(self,-1)
        frame.Show(True)
        frame.MakeModal(True)

class FirstFrame(wx.Frame):
    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,"First Window",size=(600,400))
        self.Bind(wx.EVT_CLOSE, self.on_close)
    def on_close(self, evt):
        self.MakeModal(False)
        evt.Skip()


class SecondFrame(wx.Frame):
    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,"Second Window",size=(600,400))
        self.Bind(wx.EVT_CLOSE, self.on_close1)
    def on_close1(self, evt):
        self.MakeModal(False)
        evt.Skip()    

if __name__=='__main__':
    app=wx.PySimpleApp()
    frame=MainFrame(None,-1)
    frame.Show()
    app.MainLoop()

Solution

  • You cant access the mainwindow because you have made the other windows modal, thats the whole point of modal, to stop everything else and wait for the modal frame to close before returning to the previous frame.

    To stop the child frames closing when the parent frame closes, you need to make them independant frames. To do this instead of pasing self as the parent, pass None as the parent.