Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Window does not get focus on OS X with Python 3 #203

Closed
illume opened this issue Jun 30, 2014 · 52 comments
Closed

Window does not get focus on OS X with Python 3 #203

illume opened this issue Jun 30, 2014 · 52 comments
Milestone

Comments

@illume
Copy link
Member

illume commented Jun 30, 2014

Originally reported by: Ian McCowan (Bitbucket: valrus, GitHub: valrus)


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

#!python

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:
#79/pygame-incompatible-with-osx-lion-trackpad
#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.


@illume
Copy link
Member Author

illume commented Sep 30, 2014

Original comment by Nick Monkman (Bitbucket: nmonkman, GitHub: nmonkman):


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.

@illume
Copy link
Member Author

illume commented Oct 31, 2014

Original comment by hwmrocker (Bitbucket: hwmrocker, GitHub: 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.

@illume
Copy link
Member Author

illume commented Jun 7, 2015

Original comment by Steve Holden (Bitbucket: holdenweb, GitHub: holdenweb):


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

@illume
Copy link
Member Author

illume commented Jun 29, 2015

Original comment by Arve Seljebu (Bitbucket: arve0, GitHub: arve0):


Any workarounds on this (without using python 2.7)?

@illume
Copy link
Member Author

illume commented Jul 1, 2015

Original comment by Arve Seljebu (Bitbucket: arve0, GitHub: arve0):


After some debugging, I found that I was only affected by this if inside a virtualenv. Installing pygame systemwide works fine on OS X 10.10.3 with python3, both from python.org or homebrew.

If one can cope without all the SDL-features, one can get pygame working inside virtualenv by using pygame_sdl2.

More detail is found in this thread:
https://bitbucket.org/lordmauve/pgzero/issue/1/osx-pgzero-window-cant-get-focus

@illume
Copy link
Member Author

illume commented Jul 26, 2015

Original comment by Rob Collins (Bitbucket: agiledata, GitHub: agiledata):


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.

@illume
Copy link
Member Author

illume commented May 6, 2016

Original comment by Thomas Kluyver (Bitbucket: takluyver, GitHub: takluyver):


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?

@illume
Copy link
Member Author

illume commented May 8, 2016

Original comment by René Dudfield (Bitbucket: illume, GitHub: illume):


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

@illume
Copy link
Member Author

illume commented Jun 1, 2016

Original comment by Thomas Kluyver (Bitbucket: takluyver, GitHub: takluyver):


Issue #244 was marked as a duplicate of this issue.

@illume
Copy link
Member Author

illume commented Dec 4, 2016

Original comment by Matthew Brett (Bitbucket: matthewbrett, GitHub: matthewbrett):


I think this is the same issue as noticed by matplotlib : http://matplotlib.org/faq/virtualenv_faq.html#osx

The problem is to do with the difference between framework and not-framework builds on OSX.

@illume
Copy link
Member Author

illume commented Dec 4, 2016

Original comment by Thomas Kluyver (Bitbucket: takluyver, GitHub: takluyver):


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?

@illume
Copy link
Member Author

illume commented Dec 4, 2016

Original comment by Matthew Brett (Bitbucket: matthewbrett, GitHub: matthewbrett):


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()'

@illume
Copy link
Member Author

illume commented Dec 5, 2016

Original comment by Aleksandr Motsjonov (Bitbucket: soswow, GitHub: soswow):


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.

@illume
Copy link
Member Author

illume commented Dec 5, 2016

Original comment by Thomas Kluyver (Bitbucket: takluyver, GitHub: takluyver):


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.

@illume
Copy link
Member Author

illume commented Dec 5, 2016

Original comment by Aleksandr Motsjonov (Bitbucket: soswow, GitHub: soswow):


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.

@illume
Copy link
Member Author

illume commented Dec 5, 2016

Original comment by Thomas Kluyver (Bitbucket: takluyver, GitHub: takluyver):


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?

@illume
Copy link
Member Author

illume commented Dec 5, 2016

Original comment by Aleksandr Motsjonov (Bitbucket: soswow, GitHub: soswow):


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

@illume
Copy link
Member Author

illume commented Dec 5, 2016

Original comment by Thomas Kluyver (Bitbucket: takluyver, GitHub: takluyver):


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

@illume
Copy link
Member Author

illume commented Dec 5, 2016

Original comment by Aleksandr Motsjonov (Bitbucket: soswow, GitHub: soswow):


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

@illume
Copy link
Member Author

illume commented Dec 5, 2016

Original comment by Aleksandr Motsjonov (Bitbucket: soswow, GitHub: soswow):


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

@illume
Copy link
Member Author

illume commented Dec 5, 2016

Original comment by Aleksandr Motsjonov (Bitbucket: soswow, GitHub: soswow):


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)

@illume
Copy link
Member Author

illume commented Mar 15, 2017

Original comment by Chris Dugan (Bitbucket: duganc, GitHub: duganc):


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?

@illume illume modified the milestones: 1.9.3, 2.0 Feb 17, 2018
@illume illume added the 2.0 label Feb 17, 2018
@kainarchai
Copy link

That workaround doesn't works everytime, venvdotapp works if i code something myself and import it in code, but it don't work if i want to try examples. Can we expect fix in soon future releases? Or we should wait for 2.0?

@takluyver
Copy link
Member

AFAIK, we don't know how to fix it. Glyph understands it better than any of us appear to, and venvdotapp is his fix. @illume found that it seemed to work with SDL 2, so if that's consistent, pygame 2.0 ought to resolve it.

@illume
Copy link
Member Author

illume commented Mar 12, 2018

I committed a fix in #415.

If you Compile from source you can try it out.

pip install https://github.com/pygame/pygame/archive/master.zip

Otherwise, it should be in pygame 1.9.4.

@takluyver
Copy link
Member

Thanks @illume , I underestimated how much you knew about this. :-)

@illume
Copy link
Member Author

illume commented Mar 12, 2018

I think you estimated it about right ;)

@kainarchai
Copy link

Thanks a lot for work, @illume! But i think i should wait for 1.9.4, because after i installed it from source as you mentioned, it says that i am on 1.9.4.dev0. But my pygame window still can't get focus.

@illume
Copy link
Member Author

illume commented Mar 12, 2018

@ExarKunFan sorry, I just merged the PR with the fix in. Would you mind trying it again? You'll probably need the -U upgrade flag for pip.

pip install -U https://github.com/pygame/pygame/archive/master.zip

@kainarchai
Copy link

@illume Nice! Now it works like a charm! Thanks a lot!

@illume
Copy link
Member Author

illume commented Mar 12, 2018

Thanks a lot @ExarKunFan for writing back.

@illume illume closed this as completed Mar 12, 2018
@illume illume added 1.9.4 and removed 1.9.2 labels Mar 12, 2018
@illume illume modified the milestones: 2.0, 1.9.4 Mar 12, 2018
@glyph
Copy link

glyph commented Mar 17, 2018

Sorry for the long delay; venvdotapp had a few bugs in it and I did a new release a little while ago which should have fixed many of them. Please go ahead and report bugs on it if you find more!

@ReblochonMasque
Copy link
Contributor

For some reasons, the command pip install -U https://github.com/pygame/pygame/archive/master.zip does not compile and leads to a roll back of previous version of pygame.

OSX Sierra 10.12.5

    (...)
    error: command 'gcc' failed with exit status 1
    
    ----------------------------------------
  Rolling back uninstall of pygame 

@billtubbs
Copy link

billtubbs commented Apr 5, 2018

I also have this problem. Installed pygame in an existing Anaconda virtual environment with Python 3.4 on Mac OS X 10.13.3:

$ source activate py34
(py34) $ pip install -U pygame --user
(py34) $ python my_game.py

(Cannot focus on pygame window. Keyboard input goes to terminal.)

But using pythonw my_game.py works (thanks @illume).

@spadix0
Copy link

spadix0 commented Apr 23, 2018

I also just struggled with this problem: mouse clicks going to pygame, but keypresses and mouse motion going to the window behind:

  • OSX 10.12.6
  • Python 3.6.5
  • pygame 1.9.3
    (I'm not sure what is pythonw, but the one available here is 2.7.10, which isn't going to work for me)

Based on my research, the way to solve this problem since 10.6 is to change the app activationPolicy to Regular.

[NSApp setActivationPolicy: NSApplicationActivationPolicyRegular]

As explained in that doc the default policy for unbundled apps is Prohibited (which "may not create windows"?)

Edit: If you install pyobjc, you can test/workaround this easily from python:

    from AppKit import NSApplication
    NSApplication.sharedApplication().setActivationPolicy_(0)

...which shows the app in the dock, puts the window in the foreground when it opens and fixes the input problems.

(original workaround using ctypes to do the same thing)
    from ctypes import cdll, c_int, c_void_p, c_char_p
    from ctypes.util import find_library
    objc = cdll.LoadLibrary(find_library("objc"))
    AppKit = cdll.LoadLibrary(find_library("AppKit"))
    assert objc
    assert AppKit

    getClass = objc.objc_getRequiredClass
    getClass.restype = c_void_p
    getClass.argtypes = (c_char_p,)

    registerName = objc.sel_registerName
    registerName.restype = c_void_p
    registerName.argtypes = (c_char_p,)

    msgSend = objc.objc_msgSend
    msgSend.restype = c_void_p
    msgSend.argtypes = (c_void_p, c_void_p)

    NSAutoreleasePool = getClass(b"NSAutoreleasePool")
    pool = msgSend(NSAutoreleasePool, registerName(b"alloc"))
    pool = msgSend(pool, registerName(b"init"))
    assert pool

    NSApplication = getClass(b"NSApplication")
    app = msgSend(NSApplication, registerName(b"sharedApplication"))
    assert app

    msgSend.restype = c_int
    msgSend.argtypes = (c_void_p, c_void_p, c_int)
    # request regular activation
    rc = msgSend(app, registerName(b"setActivationPolicy:"), 0)
    assert rc

    msgSend.argtypes = (c_void_p, c_void_p)
    msgSend(pool, registerName(b"release"))

@illume
Copy link
Member Author

illume commented Apr 23, 2018

Hellos,

Note: this issue is closed, and has been fixed in git. You can either install via source, or wait for the 1.9.4 release. https://www.pygame.org/wiki/MacCompile#Installing%20from%20source%20with%20homebrew

@illume
Copy link
Member Author

illume commented Apr 23, 2018

@ReblochonMasque Seems the full stack trace isn't there, so I can't see what the error was for you unfortunately.

@Waldleufer
Copy link

Waldleufer commented Jun 6, 2018

FYI:

brew upgrade sdl sdl_image sdl_mixer sdl_ttf portmidi

Error: sdl not installed
Error: sdl_image not installed
Error: sdl_mixer not installed
Error: sdl_ttf not installed
Error: portmidi not installed

These need to be installed

brew install sdl sdl_image sdl_mixer sdl_ttf portmidi

to be able to do

pip install -U https://github.com/pygame/pygame/archive/master.zip

For some reason pip was not able to execute the command before I installed these.

I found the information here

@parmentelat
Copy link

parmentelat commented Sep 26, 2019

Hey there

EDIT
It turns out the code in the first post on this thread works fine on my computer, and I hastily blamed this bug instead of my own code; sorry for the confusion

INITIAL POST

I'm still running into this bug under the following environment

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

No branches or pull requests