python - wxPython childframes interaction -
i've created simple program consists 1 mainframe(frame) , 2 childframes (childframe1 , childframe2). mainframe has 2 buttons 1 check if childframe1 created , create same if not , other check if childframe2 created , create same if not. tricky part (at least tricky me), childframe1 has button needs check if childframe2 created mainframe , if not create it. in code button creates childframe2 alongside created childframe2 mainframe. how can make work? have 2 buttons on 2 frames 1 event.
code
import wx class frame(wx.frame): def __init__(self): wx.frame.__init__(self, none, wx.id_any,'parent') panel = wx.panel(self, -1) sizer = wx.boxsizer(wx.vertical) button = wx.button(panel, -1, 'open child1') button2 = wx.button(panel, -1, 'open child2') sizer.add(button, 0, wx.center|wx.all, 5) sizer.add(button2, 0, wx.center|wx.all, 5) panel.setsizer(sizer) self.bind(wx.evt_button, self.onbutton, button) self.bind(wx.evt_button, self.onbutton2, button2) def onbutton(self, e): try: self.childf.show() except: self.childf = childframe1() self.childf.show() self.childf.setfocus() def onbutton2(self, e): try: self.childf2.show() except: self.childf2 = childframe2() self.childf2.show() self.childf2.setfocus() class childframe1(wx.frame): def __init__(self): wx.frame.__init__(self, wx.getapp().topwindow, wx.id_any, 'child1') panel = wx.panel(self, -1) sizer = wx.boxsizer(wx.vertical) button = wx.button(panel, -1, 'open child2') sizer.add(button, 0, wx.center|wx.all, 5) panel.setsizer(sizer) self.bind(wx.evt_button, self.onbutton, button) def onbutton(self,e): try: self.childf.show() except: self.childf = childframe2() self.childf.show() self.childf.setfocus() class childframe2(wx.frame): def __init__(self): wx.frame.__init__(self, wx.getapp().topwindow, wx.id_any, 'child2') if __name__ == '__main__': app = wx.app() frame = frame().show() app.mainloop()
you bind childframe1's button handler parent frames method opening/showing childframe2.
import wx class frame(wx.frame): def __init__(self): wx.frame.__init__(self, none, wx.id_any, 'parent') panel = wx.panel(self, -1) sizer = wx.boxsizer(wx.vertical) button = wx.button(panel, -1, 'open child1') button2 = wx.button(panel, -1, 'open child2') sizer.add(button, 0, wx.center | wx.all, 5) sizer.add(button2, 0, wx.center | wx.all, 5) panel.setsizer(sizer) self.bind(wx.evt_button, self.onbutton, button) self.bind(wx.evt_button, self.onbutton2, button2) def onbutton(self, e): try: self.childf.show() except: self.childf = childframe1() self.childf.show() self.childf.setfocus() def onbutton2(self, e): try: self.childf2.show() except: self.childf2 = childframe2() self.childf2.show() self.childf2.setfocus() class childframe1(wx.frame): def __init__(self): wx.frame.__init__(self, wx.getapp().topwindow, wx.id_any, 'child1') panel = wx.panel(self, -1) sizer = wx.boxsizer(wx.vertical) button = wx.button(panel, -1, 'open child2') sizer.add(button, 0, wx.center | wx.all, 5) panel.setsizer(sizer) self.bind(wx.evt_button, self.getparent().onbutton2, button) class childframe2(wx.frame): def __init__(self): wx.frame.__init__(self, wx.getapp().topwindow, wx.id_any, 'child2') if __name__ == '__main__': app = wx.app() frame = frame().show() app.mainloop()
Comments
Post a Comment