Re: app won't quit?

Chris Barker - NOAA Federal <[email protected]>
Newsgroups gmane.comp.python.apple
Message-ID <CALGmxELLB=ssVPKRanuojPuOdtqVOPzxgoCxep1S2FSoi7RH-w@mail.gmail.com>
On Tue, May 28, 2013 at 9:11 AM, Charles Hartman <[email protected]> wrote:
> Thanks, Chris.  My app has a wx.Frame (subclassed, of course).  It's there
> that I've tried Binding EVT_CLOSE, but a breakpoint in the method I find is
> never reached at all, including when I use menu or keyboard to Quit.  On way
> I've tried is this snippet I got from wxPyWiki.  (The line that purports to
> add an Exit item to the File menu does not in fact do that.  Mac still keeps
> Quit in the MyApp menu.)

right -- wx tries hard to make you app more Mac-like by moving menu
items around. If a menu item has ID ID_EXIT, it will get moved, maybe
also if it is called "exit" or "quit". But you want that, yes?

>         item = self.fileMenu.Append(-1,'E&xit','Terminate the program')
>         self.Bind(wx.EVT_MENU, self.OnClose, item)
>         if wx.Platform=="__WXMAC__":
>             wx.App.SetMacExitMenuItemId(item.GetId())
>
> This doesn't work either; OnClose() is never reached.

This is odd, but a few pointers. Try:

        item = self.fileMenu.Append(ex.ID_EXIT,'E&xit','Terminate the program')
        self.Bind(wx.EVT_MENU, self.OnClose, item)
## this shouldn't be needed if you use the ID above.
>         if wx.Platform=="__WXMAC__":
>             wx.App.SetMacExitMenuItemId(item.GetId())

I think you're going to need to put together a sample app. The
enclosed works. (I'd like to add the multiple-frame, app stays alive
thing, though...)

Oh, and you may want to try the "Widget Inspection Tool" -- it may
show you something.

-Chris


-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

[email protected]

_______________________________________________
Pythonmac-SIG maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/pythonmac-sig
unsubscribe: http://mail.python.org/mailman/options/Pythonmac-SIG
MacApp.py (application/octet-stream, 4.4 KB)
#!/usr/bin/env python

"""

MacApp.py

This is a small, simple app that tries to do all the right things on
OS-X -- putting standard menus in the right place, etc. It should work
just fine on other platforms as well : that's the beauty of wx!
"""

import wx

class DemoFrame(wx.Frame):
    """ This window displays a button """
    def __init__(self, title = "Micro App"):
        wx.Frame.__init__(self, None , -1, title)

        MenuBar = wx.MenuBar()

        FileMenu = wx.Menu()
        
        item = FileMenu.Append(wx.ID_EXIT, text = "&Exit")
        self.Bind(wx.EVT_MENU, self.OnQuit, item)

        item = FileMenu.Append(wx.ID_ANY, text = "&Open")
        self.Bind(wx.EVT_MENU, self.OnOpen, item)

        item = FileMenu.Append(wx.ID_PREFERENCES, text = "&Preferences")
        self.Bind(wx.EVT_MENU, self.OnPrefs, item)

        MenuBar.Append(FileMenu, "&File")
        
        HelpMenu = wx.Menu()

        item = HelpMenu.Append(wx.ID_HELP, "Test &Help",
                                "Help for this simple test")
        self.Bind(wx.EVT_MENU, self.OnHelp, item)

        ## this gets put in the App menu on OS-X
        item = HelpMenu.Append(wx.ID_ABOUT, "&About",
                                "More information About this program")
        self.Bind(wx.EVT_MENU, self.OnAbout, item)
        MenuBar.Append(HelpMenu, "&Help")

        self.SetMenuBar(MenuBar)

        btn = wx.Button(self, label = "Quit")

        btn.Bind(wx.EVT_BUTTON, self.OnQuit )

        self.Bind(wx.EVT_CLOSE, self.OnQuit)

        
    def OnQuit(self, Event):
        print "OnQuit called", Event
        self.Destroy()
        
    def OnAbout(self, event):
        dlg = wx.MessageDialog(self, "This is a small program to test\n"
                                     "the use of menus on Mac, etc.\n",
                                "About Me", wx.OK | wx.ICON_INFORMATION)
        dlg.ShowModal()
        dlg.Destroy()

    def OnHelp(self, event):
        dlg = wx.MessageDialog(self, "This would be help\n"
                                     "If there was any\n",
                                "Test Help", wx.OK | wx.ICON_INFORMATION)
        dlg.ShowModal()
        dlg.Destroy()

    def OnOpen(self, event):
        dlg = wx.MessageDialog(self, "This would be an open Dialog\n"
                                     "If there was anything to open\n",
                                "Open File", wx.OK | wx.ICON_INFORMATION)
        dlg.ShowModal()
        dlg.Destroy()

    def OnPrefs(self, event):
        dlg = wx.MessageDialog(self, "This would be an preferences Dialog\n"
                                     "If there were any preferences to set.\n",
                                "Preferences", wx.OK | wx.ICON_INFORMATION)
        dlg.ShowModal()
        dlg.Destroy()
        
class MyApp(wx.App):
    def __init__(self, *args, **kwargs):
        wx.App.__init__(self, *args, **kwargs)
        
        # This catches events when the app is asked to activate by some other
        # process
        self.Bind(wx.EVT_ACTIVATE_APP, self.OnActivate)

    def OnInit(self):

        frame = DemoFrame()
        frame.Show()

        import sys
        for f in  sys.argv[1:]:
            self.OpenFileMessage(f)

        return True

    def BringWindowToFront(self):
        try: # it's possible for this event to come when the frame is closed
            self.GetTopWindow().Raise()
        except:
            pass
        
    def OnActivate(self, event):
        # if this is an activate event, rather than something else, like iconize.
        if event.GetActive():
            self.BringWindowToFront()
        event.Skip()
    
    def OpenFileMessage(self, filename):
        dlg = wx.MessageDialog(None,
                               "This app was just asked to open:\n%s\n"%filename,
                               "File Dropped",
                               wx.OK|wx.ICON_INFORMATION)
        dlg.ShowModal()
        dlg.Destroy()

    def MacOpenFile(self, filename):
        """Called for files droped on dock icon, or opened via finders context menu"""
        print filename
        print "%s dropped on app"%(filename) #code to load filename goes here.
        self.OpenFileMessage(filename)
        
    def MacReopenApp(self):
        """Called when the doc icon is clicked, and ???"""
        self.BringWindowToFront()

    def MacNewFile(self):
        pass
    
    def MacPrintFile(self, file_path):
        pass
 


app = MyApp(False)
app.MainLoop()
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.