MacOS window opens in the background and does not receive focus

Hi,

First of all, thank you for this fantastic toolkit! I’m using in ms windows from years!

Description of the problem:
The window opens in the background and does not receive focus. After running the program, I am able to type, and my input goes into the terminal window

Operating system: MacOS 13.6 (22G120)
wxPython version & source: pypi wxPython 4.2.1
Python version & source: Python 3.11.6

The code is:

import wx


class MyForm(wx.Frame):
    def __init__(self, *args, **kw):
        super(MyForm, self).__init__(*args, **kw)


if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm(None, title="Esempio Form in wxPython", size=(400, 300))
    frame.Show()
    app.MainLoop()

Is something related to this?
Why can’t I set focus to my wxMac application?

Thank you for support

well, that is not only on Mac that way :rofl:

import wx


class MyForm(wx.Frame):
    def __init__(self, *args, **kw):
        super(MyForm, self).__init__(*args, **kw)
        print(f'{self.IsFocusable()=}')


if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm(None, title="Esempio Form in wxPython", size=(400, 300))
    frame.Show()
    app.MainLoop()

a hint… I like games… :grin:

import wx

class MyForm(wx.Frame):
    def __init__(self, *args, **kw):
        super().__init__(*args, **kw)
        print(f'{self.IsFocusable()=}')
        self.SetFocus()
        print(f'{self.HasFocus()=}')
        wx.StaticText(self,
            label= 'please move cursor in && out of window')
        def evt_kill_focus(_):
            print(f'lost focus {self.HasFocus()=}')
        self.Bind(wx.EVT_KILL_FOCUS, evt_kill_focus)
        def evt_get_focus(_):
            print(f'got focus {self.HasFocus()=}')
        self.Bind(wx.EVT_SET_FOCUS, evt_get_focus)
        self.Centre()

if __name__ == "__main__":
    app = wx.App(False)
    MyForm(None,
        title="Esempio Form in wxPython", size=(400, 300)).Show()
    app.MainLoop()

Ok, on mac the frame cannot get the focus, on windows it can.
So, something on mac don’t let the frame to get the focus.
All the demos and samples have the same behaviour, all opened in background, interesting… :thinking:

TextCtrl can get the focus, but it get and lost immediately the focus, window yet opened in background, more interesting… :thinking: :thinking:

mmmmh… the app is not get activated, activating it with:
osascript -e 'activate application "Python"'

The frame get moved in foreground, more more interesting… :thinking: :thinking: :thinking:

Seems that I’m not alone:

@da-dada do you already have a solution? I’m enjoing the game but have also to complete the development :unamused:

fixed with:

import wx

from Cocoa import NSApp, NSApplication


class MyForm(wx.Frame):
    def __init__(self, *args, **kw):
        super().__init__(*args, **kw)
        print(f"{self.IsFocusable()=}")
        self.SetFocus()
        print(f"{self.HasFocus()=}")
        wx.StaticText(self, label="please move cursor in && out of window")

        def evt_kill_focus(_):
            print(f"lost focus {self.HasFocus()=}")

        self.Bind(wx.EVT_KILL_FOCUS, evt_kill_focus)

        def evt_get_focus(_):
            print(f"got focus {self.HasFocus()=}")

        self.Bind(wx.EVT_SET_FOCUS, evt_get_focus)
        self.Centre()


def go_foreground():
    NSApplication.sharedApplication()
    NSApp().activateIgnoringOtherApps_(True)


if __name__ == "__main__":
    app = wx.App(False)
    MyForm(None, title="Esempio Form in wxPython", size=(400, 300)).Show()

    go_foreground()

    app.MainLoop()

Question about this solution. What sort of memory impact (in terms of size of the application) does including Cocoa have?

Moreover, is there some way to use the osascript solution with a custom application name?

well, I haven’t got a Mac, but may be it’s not the Focus and more likely the Active state ? :face_with_head_bandage:
may be checked by

        def evt_activate(_):
            print(f'activated')
        self.Bind(wx.EVT_ACTIVATE, evt_activate)

and may be ‘initiated’ by

wx.UIActionSimulator().MouseClick()

I am watching this discussion

About memory impact, considering that my program is very small, it’s a high:
RSS without importing cocoa: 37056
RSS importing cocoa: 107360

About using osascript, I think should be possibile but execution time should be slower than using cocoa.
I’m using a global hotkey to put my app in foreground when pressed, with cocoa this is really immediate, using osascript should be slower, not tested.

Yes, confirmed. The “ACTIVATED” event is not triggered; it only occurs when the frame is manually clicked. Simulating a mouse click does not resolve.