Created
October 15, 2025 00:52
-
-
Save themichaelyang/5b3a3dec71d3ddaa911a6397580639fc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import wx | |
| from wx.core import EVT_TIMER | |
| from Cocoa import NSApp, NSApplication | |
| # Adapted from https://discuss.wxpython.org/t/macos-window-opens-in-the-background-and-does-not-receive-focus/36763/2 | |
| class Frame(wx.Frame): | |
| def __init__(self, *args, **kw): | |
| super().__init__(*args, **kw) | |
| print(f"{self.IsFocusable()=}") | |
| self.SetFocus() | |
| print(f"{self.HasFocus()=}") | |
| # 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) | |
| form = Frame(None, title="Stays focused!", size=(400, 300)) | |
| form.Show() | |
| go_foreground() | |
| handler = wx.EvtHandler() | |
| handler.Bind(EVT_TIMER, lambda evt: go_foreground()) | |
| timer = wx.Timer(handler) | |
| timer.Start(100) | |
| app.MainLoop() |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tried
NSWindow.level = NSFloatingWindowLevelbut didn't work. Need to read more up on Cocoa.