What's the correct way to cleanly terminate a Phoenix app?

61 views
Skip to first unread message

Mark Summerfield

unread,
Mar 20, 2017, 11:41:55 AM3/20/17
to wxPython-users
I'm just starting out learning Phoenix.

At the moment I'm using this model:

#!/usr/bin/env python3
import sys
import wx

class MainWindow(wx.Frame):

    def __init__(self, parent, title):
        super().__init__(parent, title=title, size=(600, 400))
        filemenu= wx.Menu()
        menuQuit = filemenu.Append(wx.ID_EXIT,"&Quit", "Quit")
        menuBar = wx.MenuBar()
        menuBar.Append(filemenu,"&File")
        self.SetMenuBar(menuBar)
        self.Bind(wx.EVT_MENU, self.OnClose, menuQuit)
        self.Bind(wx.EVT_CLOSE, self.OnClose)
        self.Show(True)

    def OnClose(self, event):
        print("OnClose") # Save config etc. here
        self.Destroy()

app = wx.App()
frame = MainWindow(None, "Test")
app.MainLoop()


This means that no matter if the user clicks File->Quit, or presses Ctrl+Q (which wx seems to give me for free), or clicks the window's X close button, the OnClose method is called. So the code works. My question is, is this the correct way to do it?

Thanks.

Tim Roberts

unread,
Mar 20, 2017, 2:37:28 PM3/20/17
to wxpytho...@googlegroups.com
'Mark Summerfield' via wxPython-users wrote:
> I'm just starting out learning Phoenix.
>
> At the moment I'm using this model:
> ...
> This means that no matter if the user clicks File->Quit, or presses
> Ctrl+Q (which wx seems to give me for free), or clicks the window's X
> close button, the OnClose method is called. So the code works. My
> question is, is this the correct way to do it?

Yes, that's all pretty normal. You want your close actions to all
result in an EVT_CLOSE event, which should route to a single function
where you do your clean up and call Destroy.

The only minor picky quibble I might make is that the menuQuit handler
should probably call self.Close, which would then trigger the onClose
handler, but that's an extremely minor religious point that would not
make a difference except in extreme circumstances.

--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

Adrien Tétar

unread,
Apr 7, 2017, 5:04:14 PM4/7/17
to wxPython-users
To completely quit the app, you can use wx.Exit().
Reply all
Reply to author
Forward
0 new messages