Skip to content

Instantly share code, notes, and snippets.

@themichaelyang
Created October 15, 2025 00:52
Show Gist options
  • Select an option

  • Save themichaelyang/5b3a3dec71d3ddaa911a6397580639fc to your computer and use it in GitHub Desktop.

Select an option

Save themichaelyang/5b3a3dec71d3ddaa911a6397580639fc to your computer and use it in GitHub Desktop.
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()
@themichaelyang
Copy link
Author

Tried NSWindow.level = NSFloatingWindowLevel but didn't work. Need to read more up on Cocoa.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment