bdesham
bdesham

Reputation: 16089

Getting the frontmost window in a WX application

I’m using wxPython to write an app that will run under OS X, Windows, and Linux. I’m trying to implement the standard “Close Window” menu item, but I’m not sure how to find out which window is frontmost. WX has a GetActiveWindow function, but apparently this only works under Windows and GTK. Is there a built-in way to do this on OS X? (And if not, why not?)

Upvotes: 3

Views: 759

Answers (2)

Tatarize
Tatarize

Reputation: 10786

Add close window but only for mac.

        from sys import platform

        if platform == "darwin":
            self.file_menu.Append(wx.ID_CLOSE, _("&Close Window\tCtrl-W"), "")

Bind that.

self.Bind(wx.EVT_MENU, self.on_click_close, id=wx.ID_CLOSE)

This is what I ended up using for a bigger mac relevant routine. It needs to find the focused window. That is within the top level parent. But, this command is triggered on the menubar acceleration_table so we call the close only really in mac. I don't close the main window with the control+w

    def on_click_close(self, event=None):
        try:
            window = app.GetTopWindow().FindFocus().GetTopLevelParent()
            if window is self:
                return
            window.Close(False)
        except RuntimeError:
            pass

Basically the whatever has focus and the top level parent of that thing is the window in the foreground. So go ahead and close that unless that window is the main window.

Upvotes: 1

Werner
Werner

Reputation: 2096

What about having a Close/Exit app menu item and use: http://wxpython.org/Phoenix/docs/html/PyApp.html?highlight=gettopwindow#PyApp.GetTopWindow

to call its Close method.

Werner

Upvotes: 1

Related Questions