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
da-dada
December 21, 2023, 3:28pm
2
well, that is not only on Mac that way
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()
da-dada
December 21, 2023, 9:39pm
4
Samuele_Barzaghi:
a hint…
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…
TextCtrl can get the focus, but it get and lost immediately the focus, window yet opened in background, more interesting…
mmmmh… the app is not get activated, activating it with:
osascript -e 'activate application "Python"'
The frame get moved in foreground, more more interesting…
Seems that I’m not alone:
When I launch a wxPython program on macOS (Apple M2), it stays in background, and come to foreground (and get wx.EVT_ACTIVATE event) only when I manually click on its icon on the Dock.
There is no such issue with the GUI programs written with Tkinter or PyQt.
I tried googling for solutions, and only got a trick of manually bring the program to foreground by calling NSApp().activateIgnoringOtherApps_(True) through the AppKit module – but this should be a temporary workaround.
Here is the test …
@da-dada do you already have a solution? I’m enjoing the game but have also to complete the development
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()
1 Like
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?
da-dada
December 22, 2023, 6:40pm
12
well, I haven’t got a Mac, but may be it’s not the Focus and more likely the Active state ?
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.