import sys import wx class MyForm(wx.Frame): def __init__(self, parent, style=wx.DEFAULT_FRAME_STYLE): wx.Frame.__init__(self, parent, title="Redirect", style=style) # Add a panel so it looks the correct on all platforms panel = wx.Panel(self, wx.ID_ANY) style = wx.TE_MULTILINE|wx.TE_READONLY|wx.HSCROLL log = wx.TextCtrl(panel, wx.ID_ANY, size=(300,100), style=style) btnPush = wx.Button(panel, wx.ID_ANY, 'Push me!') self.Bind(wx.EVT_BUTTON, self.OnButton, btnPush) btnClose = wx.Button(panel, wx.ID_ANY, 'Close') self.Bind(wx.EVT_BUTTON, self.OnButton, btnClose) self.Bind(wx.EVT_CLOSE, self.OnClose) #### Soluce here self.Bind(wx.EVT_BUTTON, self.OnClose, btnClose) # Add widgets to a sizer sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(log, 1, wx.ALL|wx.EXPAND, 5) sizer.Add(btnPush, 0, wx.ALL|wx.CENTER, 5) sizer.Add(btnClose, 0, wx.ALL|wx.CENTER, 5) panel.SetSizer(sizer) # Redirect text here sys.stdout = log def OnButton(self, event): print("You pressed the button!") def OnClose(self, event): #### Soluce here # self.Close() self.Hide() class MyMainFrame(wx.Frame): def __init__(self, style=wx.DEFAULT_FRAME_STYLE): super(MyMainFrame, self).__init__(None, -1, title="", style=style) self.SetProperties() self.CreateCtrls() self.BindEvents() self.DoLayout() self.CenterOnScreen() def SetProperties(self): self.SetTitle("Main frame") def CreateCtrls(self): self.frm = MyForm(self) #### Soluce here self.panel = wx.Panel(self, -1) self.btnDlg = wx.Button(self.panel, -1, "&Show logging frame") self.btnClose = wx.Button(self.panel, wx.ID_CLOSE, "&Close") def BindEvents(self): self.Bind(wx.EVT_BUTTON, self.OnLoggingFrm, self.btnDlg) self.Bind(wx.EVT_BUTTON, self.OnCloseMe, id=wx.ID_CLOSE) self.Bind(wx.EVT_CLOSE, self.OnCloseWindow) def DoLayout(self): mainSizer = wx.BoxSizer(wx.VERTICAL) mainSizer.Add(self.btnDlg, 1, wx.EXPAND | wx.ALL, 10) mainSizer.Add(self.btnClose, 1, wx.EXPAND | wx.ALL, 10) self.panel.SetAutoLayout(True) self.panel.SetSizer(mainSizer) mainSizer.Fit(self.panel) # def OnLoggingFrm(self, event): # try : # self.frm.Show(True) # except (AttributeError) as err: # self.frm = MyForm(self) # self.frm.Show(True) def OnLoggingFrm(self, event): #### Soluce here try : self.frm.Show(True) except (AttributeError) as err: print("error") def OnCloseMe(self, event): self.Close() def OnCloseWindow(self, event): self.Destroy() class MyApp(wx.App): def OnInit(self): frame = MyMainFrame() self.SetTopWindow(frame) frame.Show(True) return True def main(): app = MyApp(False) app.MainLoop() if __name__ == "__main__": main()