Phoenix
Phoenix copied to clipboard
MacOS window opens in the background and does not receive focus
Operating system: MacOS 13.6 (22G120) wxPython version & source: pypi wxPython 4.2.1 Python version & source: Python 3.11.6
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
thank you for support
Code Example (click to expand)
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()
This issue has been mentioned on Discuss wxPython. There might be relevant details there:
https://discuss.wxpython.org/t/window-starts-hidden-in-vscode-on-osx/36791/2
I'm having the same issue. Doesn't look like the linked discussion was able to resolve the issue other than by including a new large dependency (Cocoa Python package). I have been able to get it to take focus on app startup (just using Show) using Miniconda's Python (Raise doesn't seem to do anything even with Miniconda). With all other methods I've tried (Homebrew Python, PyEnv Python, and PyEnv Python with --enable-framework), however, the app always starts unfocused in the background while the terminal retains focus.
Edit: It looks like this same issue happens with a minimal example in C++ wxWidgets if we build to myapp and run ./myapp, but if we place the myapp executable inside a macOS application bundle and then run the MyApp.app application the app is raised and focused on startup. In both cases programmatically calling Raise() seems to still not work (tested using a button and a timer). So the question is: How to get the expected behavior of raising and focusing the app on startup with non-Miniconda Python builds?
FWIW, this seems to be a regression with wxPython (or wx itself) 4.2.1 as in 4.2.0 it behaves as expected. I have python 3.10 and 3.12 locally with wxPython 4.2.0 and 4.2.1 respectively. The latter exhibits the bad behavior, but the former does not.
This also reproduces for me for Python 3.12 and wxPython 4.2.1.
FYI, C++ wxWidgets works correctly, even without the MacOS bundle (unless it's run from within VSCode, but that seems to be the editor's fault). Tested with wxWidgets 3.2.2 and 3.2.0.
Can you test with the latest wxWidgets please?
FWIW, I don't think there's a VSCode link here, aside from the fact that we all seem to be running it that way. I'm running from vscode with a custom build command. Further, if I run it from a terminal that is half-screen, it pops up behind my terminal and in front of vscode:
@kk7ds I was referring to C++ wxWidgets when I mentioned VSCode. I remember I had this problem before with my C++ wxWidgets applications when running from VSCode, but I checked just now, and it's no longer reproducible. The app's window shows up correctly in front, VSCode or not.
For wxPython, you're right. The window shows up in the background. I'm just saying that's not the case for the original wxWidgets library.
@swt2c Latest wxWidgets master branch works correctly (C++).
@swt2c Latest wxWidgets master branch works correctly (C++).
Sorry, can you check the latest wxWidgets release or the 3.2 branch? wxPython uses the 3.2 branch.
I suspect this might be the issue: https://github.com/wxWidgets/wxWidgets/issues/24056
@swt2c as I said, I've checked 3.2.0 and it works correctly.
If you have cmake and a C++ compiler, you can check it yourself. Here's your CMakeLists.txt:
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
project(myapp LANGUAGES CXX)
include(FetchContent)
set(wxBUILD_SHARED OFF)
message(STATUS "Fetching wxWidgets...")
FetchContent_Declare(
wxWidgets
GIT_REPOSITORY https://github.com/wxWidgets/wxWidgets.git
GIT_TAG 3.2
)
FetchContent_MakeAvailable(wxWidgets)
set(SRCS myapp.cpp)
add_executable(main ${SRCS})
target_link_libraries(main PRIVATE wxcore)
And myapp.cpp:
#include <wx/wx.h>
class MyApp : public wxApp
{
public:
virtual bool OnInit();
};
class MyFrame : public wxFrame
{
public:
MyFrame(const wxString &title, const wxPoint &pos, const wxSize &size);
};
wxIMPLEMENT_APP(MyApp);
bool MyApp::OnInit()
{
MyFrame *frame = new MyFrame("Hello Everyone!", wxDefaultPosition, wxDefaultSize);
frame->Show(true);
return true;
}
MyFrame::MyFrame(const wxString &title, const wxPoint &pos, const wxSize &size)
: wxFrame(NULL, wxID_ANY, title, pos, size)
{
new wxStaticText(this, wxID_ANY, "Good Morning!"); // no need to delete - the parent will do it automatically
}
You can build it and run with:
cmake -S. -Bbuild && cmake --build build -j && ./build/main
CMake automatically downloads wxWidgets form GitHub from the branch specified by GIT_TAG and compiles it (you can use tags or branches for GIT_TAG). No need to preinstall anything except a compiler and cmake.
@swt2c as I said, I've checked 3.2.0 and it works correctly.
As a wxPython and wxWidgets developer, I know how to test it. The problem is that I don't own a Mac. Testing 3.2.0 is unfortunately not helpful because it was released a year and a half ago.
@swt2c no problem, just give me the exact wxWidgets commit, and I will test it for you.
@swt2c no problem, just give me the exact wxWidgets commit, and I will test it for you.
If the problem is in wxWidgets, as I believe it is, can you please test: https://github.com/wxWidgets/wxWidgets/tree/a812fffda3fe686c94e24bff27e8effd96e4de64 (which I think should show the problem) and https://github.com/wxWidgets/wxWidgets/commit/b97aee419af105e5c724ead2917e1b04b9abb26a (which I think should not show the problem)
@swt2c On my machine, C++ apps compiled with a812fffda3fe686c94e24bff27e8effd96e4de64 or b97aee4 both work correctly (the window shows up on top when the app is run from the terminal).
Python apps with Python 3.12 and wxPython 4.2.1 do not work correctly when run from the terminal (the window shows up in the background without getting focus).
Thanks for testing. I was hoping this was a wxWidgets issue, but your testing seems to indicate it is now.
After looking at the changes between 4.2.0 and 4.2.1, I suspect this might be the culprit: https://github.com/wxWidgets/Phoenix/commit/a789e49000cf8069450e014ee5d8cecd16d10e31