Window does not get focus on OS X with Python 3

Issue #203 new
Ian McCowan created an issue

A pygame window created using the code below on OS X using the latest pygame source:

import pygame

screen = pygame.display.set_mode((640, 480))

done = False
while not done:
    keys = pygame.key.get_pressed()
    if keys[pygame.K_ESCAPE]:
        done = True
    if keys[pygame.K_SPACE]:
        print("got here")
    events = pygame.event.get()
    for event in events:
        if event.type == pygame.MOUSEBUTTONDOWN:
            print("mouse at ", event.pos)

does not receive any keyboard input. It is not possible to trigger the "got here" message nor cause it to terminate using Escape; instead all keypresses show up in the terminal window from which the script was run. This happens even though the window appears to have focus.

Additionally, based on the mouse event handling, the window exhibits some combination of the incorrect mouse behavior described in these two issues: viz. all subsequent clicks register with the same position as the first one, and the only way to update its position is to click and drag to a new location.

Comments (33)

  1. Nick Monkman

    Just wanted to mention that I ran into this issue as well. I got around it by installing the MacPorts version (py27-game) which is an earlier version (1.9.1_8). It only addressed the keyboard input issue...I have not tried the mouse yet.

  2. hwmrocker

    I have the same issue. I couldnt build the 1.9.1 release, i tried a lot of versions between 1.9.1 and the most recent version in the repo but I found no version that worked. The latest version that compiled was 3019:252da58b4

    i used: gcc 4.2 python 3.4 (the official dmg) Mac OS X 10.6.8

    I cannot use a different python version because my project depends on asyncio. It works perfectly under linux, this is probably a OS X specific issue.

    Please tell me what i could do to help.

  3. Steve Holden

    "I cannot use a different python version because my project depends on asyncio." Think about using virtualenv, which aims to separate out the dependencies for multiple Python projects. It really makes a huge difference.

  4. Rob Collins

    We have found a workaround, described in this thread. It means substituting the system Framework Python for the virtualenv one that can't handle windowing properly on a Mac.

  5. Thomas Kluyver

    Thanks @arve0 and @agiledata for investigating this. Is there any hope of fixing it within pygame? I'm not familiar with OSX, but it seems weird that a Python virtualenv can affect window management.

    If we don't know of a fix, is there a way we can detect it and warn/error about it?

  6. René Dudfield

    This is likely to do with which SDL was used inside different python environments. We likely need to redo the way this is handled on OSX. Since installing system wide Frameworks is really not a good idea in modern OSX (and probably never was).

  7. Thomas Kluyver

    That's good to know. If it is the same and mpl hasn't fixed it, I suspect there's not much we can do other than documenting workarounds.

    The docs you linked to suggest that python3 -m venv works for mpl where virtualenv doesn't. Does using venv also make Pygame behave properly?

  8. Matthew Brett

    Yes it does:

    python3 -m venv venv  #  This makes a framework virtualenv
    pip install
    python -c 'import pygame.examples.aliens as p; p.main()'
  9. Aleksandr Motsjonov

    What should I do if I use conda? Mentioned article says Anaconda is framework build. I successfully installed it with pip being inside my conda environment. And code starts, but with now visible window.

  10. Thomas Kluyver

    Have you tried it in conda? If our suspicions are correct, and those docs are right that Anaconda is a framework build, it should work.

  11. Aleksandr Motsjonov

    I have Python 3.5 conda environment. Installed pygame:

    pip install pygame
    Collecting pygame
      Downloading pygame-1.9.2rc1-cp35-cp35m-macosx_10_9_intel.whl (5.0MB)
        100% |████████████████████████████████| 5.0MB 86kB/s
    Installing collected packages: pygame
    Successfully installed pygame-1.9.2rc1

    When run aliens (or anything else) console says:

    2016-12-06 04:48:20.471 python[45197:1243798] 04:48:20.471 WARNING:  140: This application, or a library it uses, is using the deprecated Carbon Component Manager for hosting Audio Units. Support for this will be removed in a future release. Also, this makes the host incompatible with version 3 audio units. Please transition to the API's in AudioComponent.h.

    And nothing opens up. When swipe see all the windows: Screen Shot 2016-12-06 at 4.48.22 AM.jpg

    I've also tried 1.9.2b12 version. didn't help.

  12. Thomas Kluyver

    From your screenshot it looks like it's opening but not getting focussed. If you switch to it before you die, can you move the car with the left/right arrows?

  13. Aleksandr Motsjonov

    @takluyver yes, window is created, but when I select it I see it for a split-second and then I see my console again. So, no, I can't move a car.

  14. Thomas Kluyver

    Other people: does Aleksandr's description match what you see? Is there a way to check if Python is a framework build?

  15. Aleksandr Motsjonov

    Also when I Cmd+Tab through windows - it's not there. That's why I have to swipe up to see to four-finger-swipe-up (Mission control is the name in system pref.)

  16. Aleksandr Motsjonov

    I've tried different example now. Since aliens closes itself too quickly. With other example I was able to get focus. Here is how:

    1. open some small window (like Activity Monitor)
    2. swipe up and click on pygame window in misson control view (screenshot above)
    3. It will open for a split second focus back on Activity Monitor
    4. Move Activity Monitor aside and click on pygame window behind it
    5. Profit. I have focused pygame window
  17. Aleksandr Motsjonov

    Using same trick I've got focus on aliens. Should arrow keys work? Because they don't. Pressing any key while being "focused" doesn't change anything (car doesn't move)

  18. Chris Dugan

    I tried @matthewbrett 's solution (adjusted for my game since the github download link doesn't work anymore) and OSX still doesn't give me focus of the window. Is there a documented best workaround yet for this?

  19. Paul Kristian LaFosse

    I want to echo Chris Dugan's question about a documented best workaround. I was testing a friend's code on my Mac OS system and ran into the issue. Matthew Brett's solution does not work for me, either.

  20. Alex LIBO

    I have same issue.
    My environments: masOS , pyenv , python3 pygame 1.9.3

    My solution: 1. remove python3 by pyenv pyenv uninstall python3 2. remove pyenv brew uninstall pyenv 3. reinstall python3 by homebrew brew install python3

    Then, everything is ok .

  21. Adam J Stewart

    I'm also seeing this issue. With Python 3.6.3 and pygame 1.9.3 installed with Anaconda, the window launches, but never gets focus, and arrow keys never register. With Python 2.7.13 and pygame 1.9.3 installed with Anaconda, everything works as expected. This is on macOS High Sierra.

    For now, I'll drop back to Python 2, but Python 3 support would be nice to have.

  22. jumpin2x

    same problem. window can't get focus and can't receive keyboard events. with python3.6(conda) and pygame 1.9.3 in mac10.13.

    i solved this by typing pythonw(anaconda provided) instead with python in terminal,like this


    more info:

    gui scripts should be called through pythonw

  23. Zwork101

    It would seem I'm having the same issue in a venv, however I'm on windows. I have anaconda installed, however I'm not using it.

  24. Alfredo Canziani

    Figured out a workaround. If my editor (and browser, on the other screen) is (are) in fullscreen mode, then PyGame's GUI will appear on top of everything. Even on top of your fullscreen terminal. It's odd, but it works like a charm. So, it's a nice bug which bugs a bug, making everything just fine.

  25. Log in to comment