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

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

done = False
while not done:
    pygame.event.pump()
    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: https://bitbucket.org/pygame/pygame/issue/79/pygame-incompatible-with-osx-lion-trackpad https://bitbucket.org/pygame/pygame/issue/163/no-mouse-move-events-unless-mouse-button 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 (27)

  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. Thomas Kluyver

    Thanks @Arve Seljebu and @Rob Collins 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?

  5. 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).

  6. 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?

  7. Matthew Brett

    Yes it does:

    python3 -m venv venv  #  This makes a framework virtualenv
    pip install https://github.com/takluyver/pygame/releases/download/1.9.2b12/pygame-1.9.2b12-cp35-cp35m-macosx_10_9_x86_64.whl
    python -c 'import pygame.examples.aliens as p; p.main()'
    
  8. 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.

  9. 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.

  10. 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
  11. 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.

  12. 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 .

  13. Log in to comment