1

my application is made of one MDIParentFrame which tries to open two MDIChildFrame. I say "try" because each time I open the first MDIChildFrame, my MDIParentFrame shows in the menu bar that menu exported by the MDIChildFrame, and never recovers its own menu.

I do not know how to deactivate my MDIChildFrame so that MDIPArentFrame shows again its own menu in order to load the second MDIChildFrame through its menu.

Here is my code:

import wx

class MDIFrame(wx.MDIParentFrame):

    def __init__(self):
        wx.MDIParentFrame.__init__(self, None, -1, "MDI Parent",size=(600,400))
        menu = wx.Menu()
        menu.Append(5000, "&New Window 1")
        menu.Append(5002, "&New Window 2")
        menu.Append(5001, "E&xit")
        menubar = wx.MenuBar()
        menubar.Append(menu, "&File")
        self.SetMenuBar(menubar)
        self.Bind(wx.EVT_MENU, self.OnNewWindow1, id=5000)
        self.Bind(wx.EVT_MENU, self.OnExit, id=5001)
        self.Bind(wx.EVT_MENU, self.OnNewWindow2, id=5002)

    def OnExit(self, evt):
        self.Close(True)

    def OnNewWindow1(self, evt):
        #win = wx.MDIChildFrame(self, -1, "Child Window")
        win = MDIHijo1(self)
        win.Show(True)

    def OnNewWindow2(self, evt):
        #win = wx.MDIChildFrame(self, -1, "Child Window")
        win = MDIHijo2(self)
        win.Show(True)


class MDIHijo1(wx.MDIChildFrame):

    def __init__(self,parent):
        wx.MDIChildFrame.__init__(self,parent,title='Ventana Hijo 1')
        menu = wx.Menu()
        menu.Append(5500, "&Son 1")
        menu.Append(5501, "&Son 1")
        menubar = wx.MenuBar()
        menubar.Append(menu, "&File")
        self.SetMenuBar(menubar)


class MDIHijo2(wx.MDIChildFrame):

    def __init__(self,parent):
        wx.MDIChildFrame.__init__(self,parent,title='Ventana Hijo 2')
        menu = wx.Menu()
        menu.Append(5500, "&Son 2")
        menu.Append(5501, "&Son 2")
        menubar = wx.MenuBar()
        menubar.Append(menu, "&File")
        self.SetMenuBar(menubar)         








app = wx.PySimpleApp()
frame = MDIFrame()
frame.Show()
app.MainLoop()

Thanks a lot for your help, best regards, Javier.

futpilari
  • 11
  • 2
  • Thanks for posting a clear question, with working code - nice job. I don't know whether it's related - maybe not - but it seems dodgy to be assigning hardcoded nimbers wxIDs. You should be getting these from wx.NewId(). – GreenAsJade Feb 19 '14 at 11:28

1 Answers1

0

Are you want to appen new menu item "Son 1"/"Son 2" to menu "Window"?

You can use menu = parent.GetWindowMenu() and append the menu item to it.

import wx

class MDIFrame(wx.MDIParentFrame):

    def __init__(self):
        wx.MDIParentFrame.__init__(self, None, -1, "MDI Parent",size=(600,400))
        menu = wx.Menu()
        menu.Append(5000, "&New Window 1")
        menu.Append(5002, "&New Window 2")
        menu.Append(5001, "E&xit")
        menubar = wx.MenuBar()
        menubar.Append(menu, "&File")
        self.SetMenuBar(menubar)
        self.Bind(wx.EVT_MENU, self.OnNewWindow1, id=5000)
        self.Bind(wx.EVT_MENU, self.OnExit, id=5001)
        self.Bind(wx.EVT_MENU, self.OnNewWindow2, id=5002)

    def OnExit(self, evt):
        self.Close(True)

    def OnNewWindow1(self, evt):
        #win = wx.MDIChildFrame(self, -1, "Child Window")
        win = MDIHijo1(self)
        win.Show(True)

    def OnNewWindow2(self, evt):
        #win = wx.MDIChildFrame(self, -1, "Child Window")
        win = MDIHijo2(self)
        win.Show(True)


class MDIHijo1(wx.MDIChildFrame):

    def __init__(self,parent):
        wx.MDIChildFrame.__init__(self,parent,title='Ventana Hijo 1')
        menu = parent.GetWindowMenu()
        menu.Append(5500, "&Son 1")
        menu.Append(5501, "&Son 1")
        # menubar = wx.MenuBar()
        # menubar.Append(menu, "&File")
        # self.SetMenuBar(menubar)


class MDIHijo2(wx.MDIChildFrame):

    def __init__(self,parent):
        wx.MDIChildFrame.__init__(self,parent,title='Ventana Hijo 2')
        menu = parent.GetWindowMenu()
        menu.Append(5500, "&Son 2")
        menu.Append(5501, "&Son 2")
        # menubar = wx.MenuBar()
        # menubar.Append(menu, "&File")
        # self.SetMenuBar(menubar)


app = wx.PySimpleApp()
frame = MDIFrame()
frame.Show()
app.MainLoop()
Jerry_Y
  • 1,724
  • 12
  • 9
  • I don't think he wants to append child menu items to the parent menu. He wants a way to make the parent menu come back when the child is deselected. However, I think this "isn't going to work". This is because inside the MDIParent, one of the children is always selected. There is not a case (or a way) to have no children selected. – GreenAsJade Feb 19 '14 at 11:55
  • @GreenAsJade original menu disappeared becasue it is replaced by new created menu bar with self.SetMenuBar(menubar) when child frame init.You can have a try with my code. – Jerry_Y Feb 19 '14 at 12:04
  • 1
    Thank you, both of you are right somehow. What I really need is what GreenAsJade says in the last comment, but as Jerry_Y says, the menubar is changed since I use "self.SetMenuBar(menubar)". I will try to manage some events in order to control those menubars. – futpilari Feb 19 '14 at 13:46
  • I think the fundamental difficulty is that there does not appear to be a "no child selected" state for MDI Parent. – GreenAsJade Feb 19 '14 at 21:57