lordmauve / pgzero

A zero-boilerplate games programming framework for Python 3, based on Pygame.
https://pygame-zero.readthedocs.io/
GNU Lesser General Public License v3.0
532 stars 190 forks source link

OSX pgzero window can't get focus when Pygame is installed in virtualenv #1

Closed lordmauve closed 9 years ago

lordmauve commented 9 years ago

Originally reported by: Matt Wheeler (Bitbucket: funkyhat, GitHub: funkyhat)


On OSX the pgzero window doesn't get focus.

This appears to be caused by https://bitbucket.org/pygame/pygame/issue/203/window-does-not-get-focus-on-os-x-with


lordmauve commented 9 years ago

Original comment by Daniel Pope (Bitbucket: lordmauve, GitHub: lordmauve):


Resolved by pull request #20

lordmauve commented 9 years ago

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


Further investigation shows that the problem is only when inside a virtualenv, though SDL2 works inside virtualenv. SDL2 is also a bit snappier than SDL, ~1 second load time vs ~6 seconds.

All examples works OK, except music.py (I'm missing a codec) and ptext.py. The tracebacks follows:

music.py (fails for both SDL and SDL2)

Traceback (most recent call last):
  File "/Users/arve/.virtualenvs/3.4/bin/pgzrun", line 9, in <module>
    load_entry_point('pgzero==1.1rc1', 'console_scripts', 'pgzrun')()
  File "/Users/arve/.virtualenvs/3.4/lib/python3.4/site-packages/pgzero/runner.py", line 45, in main
    PGZeroGame(mod).run()
  File "/Users/arve/.virtualenvs/3.4/lib/python3.4/site-packages/pgzero/game.py", line 195, in run
    self.dispatch_event(event)
  File "/Users/arve/.virtualenvs/3.4/lib/python3.4/site-packages/pgzero/game.py", line 131, in dispatch_event
    handler(event)
  File "/Users/arve/.virtualenvs/3.4/lib/python3.4/site-packages/pgzero/game.py", line 125, in <lambda>
    return lambda event: handler(**prep_args(event))
  File "music.py", line 28, in on_mouse_down
    music.play(t)
  File "/Users/arve/.virtualenvs/3.4/lib/python3.4/site-packages/pgzero/music.py", line 51, in play
    _play(name, -1)
  File "/Users/arve/.virtualenvs/3.4/lib/python3.4/site-packages/pgzero/music.py", line 40, in _play
    _music.load(path)
  File "pygame_sdl2/mixer_music.pyx", line 53, in pygame_sdl2.mixer_music.load (gen3/pygame_sdl2.mixer_music.c:1018)
pygame_sdl2.error.error: b'Unrecognized music format'

ptext.py

Traceback (most recent call last):
  File "/Users/arve/.virtualenvs/3.4/bin/pgzrun", line 9, in <module>
    load_entry_point('pgzero==1.1rc1', 'console_scripts', 'pgzrun')()
  File "/Users/arve/.virtualenvs/3.4/lib/python3.4/site-packages/pgzero/runner.py", line 45, in main
    PGZeroGame(mod).run()
  File "/Users/arve/.virtualenvs/3.4/lib/python3.4/site-packages/pgzero/game.py", line 204, in run
    draw()
  File "ptext.py", line 115, in draw
    gcolor="#442200"
  File "/Users/arve/.virtualenvs/3.4/lib/python3.4/site-packages/pgzero/screen.py", line 59, in text
    ptext.draw(*args, surf=self._surf, **kwargs)
  File "/Users/arve/.virtualenvs/3.4/lib/python3.4/site-packages/pgzero/ptext.py", line 320, in draw
    ocolor, owidth, scolor, shadow, gcolor, alpha, align, lineheight, angle, cache)
  File "/Users/arve/.virtualenvs/3.4/lib/python3.4/site-packages/pgzero/ptext.py", line 229, in getsurf
    lineheight=lineheight, cache=cache)
  File "/Users/arve/.virtualenvs/3.4/lib/python3.4/site-packages/pgzero/ptext.py", line 256, in getsurf
    array = pygame.surfarray.pixels3d(lsurf)
AttributeError: 'module' object has no attribute 'surfarray'

I'll leave it with this, as pgzero actually works as described if following the installation manual. My bad on the virtualenv.

lordmauve commented 9 years ago

Original comment by Daniel Pope (Bitbucket: lordmauve, GitHub: lordmauve):


Yes, that was what I was thinking.

lordmauve commented 9 years ago

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


What about this?

try:
    import pygame_sdl2
    pygame_sdl2.import_as_pygame()
except ImportError:
    pass

EDIT: in runner.py

lordmauve commented 9 years ago

Original comment by Daniel Pope (Bitbucket: lordmauve, GitHub: lordmauve):


Great news. Could you verify that all the examples work and do a pull request to add this as an extra section to the installation docs? If it's possible to install without virtualenv, even with some bootstrap in pgzero.runner, that would be even better. Or just as simple as you can make it.

lordmauve commented 9 years ago

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


Using pygame sdl2 works!

brew install python3 sdl2 sdl2_gfx sdl2_image sdl2_mixer sdl2_ttf
mkvirtualenv -p /usr/local/bin/python3 pygame_sdl2
pip install cython
git clone https://github.com/renpy/pygame_sdl2
cd pygame_sdl2/
python fix_virtualenv.py
pip install .
cd ..
hg clone https://bitbucket.org/lordmauve/pgzero
cd pgzero
wget https://gist.github.com/arve0/c1be371566a9099747a4/raw/3227645c10b28241b50ab35b1cc7006036ff391c/runner.py -O pgzero/runner.py
pip install .
wget https://gist.github.com/arve0/a22efe8f591610799ba6/raw/c61d4895afc87d9a304f83467db90e46cd97cbba/hangman.py
pgzrun hangman.py

pygame_sdl2_python3.png

lordmauve commented 9 years ago

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


Adding, pygame is not found in the open programs (Alt-Tab):

pygame-python3.png

Here is what is expected (python 2):

pygame-python2.png

lordmauve commented 9 years ago

Original comment by Daniel Pope (Bitbucket: lordmauve, GitHub: lordmauve):


Thank you.

lordmauve commented 9 years ago

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


Here is some debug info.

Code to reproduce is found here.

A more detailed description in images:

  1. pgzrun hangman_solution.py - opens pygame in background window: Skjermbilde 2015-07-01 kl. 18.50.52.png

  2. Focus pygame with mouse - brings window to front, but menubar shows "Terminal". Skjermbilde 2015-07-01 kl. 18.51.06.png

  3. Press abc characters - characters echoed in terminal, pygame is unaffected (see picture above).

lordmauve commented 9 years ago

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


@funkyhat Have you found a work around? A "fix" was proposed in pull request #13, but it was declined, as python 2 support is not wanted.