user2744506
user2744506

Reputation:

WxTimer doesn't let the wxFrame close

I am trying to debug this code, the Frame doesn't close on pressing the 'x' button, however when I comment out the wxTimer I am able to close it. Upon googling I found this - http://wiki.wxpython.org/Timer and I tried to bind an event to the top level window however the onClose function is never called on pressing the 'x' button.

Any suggestions?

class matplotsink(wx.Panel):

    def __init__(self, parent, title, queue):
        # wx.Frame.__init__(self, parent, -1, title)
        wx.Panel.__init__(self, parent, wx.SIMPLE_BORDER)

        #self.datagen = DataGen()
        #self.data = self.datagen.next()

        self.data = []

        self.parent = parent
        self.title = title
        self.queue = queue

        self.paused = False

        #self.create_menu()
        #self.create_status_bar()

        self.toplevelcontainer = wx.GetApp().GetTopWindow()
        self.toplevelcontainer.CreateStatusBar()


        print 'Hey'
        # menuBar = wx.MenuBar()
        # fileMenu = wx.Menu()
        # fileMenu.Append(wx.ID_NEW, '&New')
        # fileMenu.Append(wx.ID_OPEN, '&Open')
        # fileMenu.Append(wx.ID_SAVE, '&Save')
        # menuBar.Append(fileMenu, '&File')
        # self.toplevelcontainer.SetMenuBar(menuBar)  


        self.toplevelcontainer.Bind(wx.EVT_CLOSE, self.onCloseFrame)
        self.create_main_panel()

        self.redraw_timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.draw_callback, self.redraw_timer)

        self.redraw_timer.Start(100)

    def onCloseFrame(self,event):
        print 'Hey1'
        self.redraw_timer.Stop()
        self.toplevelcontainer.Destroy()
        self.toplevelcontainer.Close()

Upvotes: 2

Views: 403

Answers (1)

nepix32
nepix32

Reputation: 3177

I cannot see that onCLoseFrame() does not get called. Quite to the contrary, self.toplevelcontainer.Destroy() retriggers EVT_CLOSE ad infinitum until the maximum recursion depth is reached. This is the reason self.toplevelcontainer never gets closed.

Instead of trying to destroy the top level window yourself, let the event handler do its job by skipping after you are done with the cleanup:

    def onCloseFrame(self, event):
        # ...
        self.redraw_timer.Stop() #cleanup, important!
        event.Skip()

You can check this stackoverflow answer (the link in the answer) for an explanation. As I have seen, the particular wxPython-wiki entry for wx.Timer is not very useful.

Upvotes: 3

Related Questions